Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Slow down game without altering user experience  (Read 4262 times)

0 Members and 1 Guest are viewing this topic.

meto28

  • Newbie
  • *
  • Posts: 8
    • View Profile
Slow down game without altering user experience
« on: March 12, 2011, 06:36:46 pm »
Hello,

I'm making a snake game and I want it to move on a grid tile by tile but I'm using 32pixels tiles so the snake move too fast.
I tried to set a fps limit and then a sleep but both make the game impossible to play because it's slows the inputs too.

Is there any way to slow down the snake but not the all game ??

devlin

  • Full Member
  • ***
  • Posts: 128
    • View Profile
Slow down game without altering user experience
« Reply #1 on: March 12, 2011, 06:53:53 pm »
Use time-based movement, not frame based.

I.e. use sf::Window::GetFrameTime and scale your "movement amount per second" by that; instead of "x += 32;" or whatever you might be using.

meto28

  • Newbie
  • *
  • Posts: 8
    • View Profile
Slow down game without altering user experience
« Reply #2 on: March 12, 2011, 07:26:30 pm »
Well I tried
Code: [Select]

      float             ElapsedTime = App.GetFrameTime();

      if (direction == "D")    

        {
          Sprite.SetPosition(X, Y);
          Y += TileSize * ElapsedTime;
        }


But it won't move

devlin

  • Full Member
  • ***
  • Posts: 128
    • View Profile
Slow down game without altering user experience
« Reply #3 on: March 12, 2011, 10:11:36 pm »
and Y is?  float? int? if the latter, then your end result will be Y += 32 * 0 = Y - so, no movement.

It's very difficult to help without seeing the full picture.

meto28

  • Newbie
  • *
  • Posts: 8
    • View Profile
Slow down game without altering user experience
« Reply #4 on: March 13, 2011, 03:57:12 pm »
Well I've sent it to you on PM

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Slow down game without altering user experience
« Reply #5 on: March 13, 2011, 04:08:50 pm »
Why PM? If you post it here, other people can try to help you, too.

Try to show us a minimal and complete example ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

meto28

  • Newbie
  • *
  • Posts: 8
    • View Profile
Slow down game without altering user experience
« Reply #6 on: March 13, 2011, 05:12:43 pm »

TehKyle

  • Newbie
  • *
  • Posts: 8
    • View Profile
Slow down game without altering user experience
« Reply #7 on: March 13, 2011, 05:25:56 pm »
use RenderWindow GetInput function instead of Event KeyDown

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Slow down game without altering user experience
« Reply #8 on: March 13, 2011, 05:35:47 pm »
Quote from: "Nexus"
Try to show us a minimal and complete example
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

devlin

  • Full Member
  • ***
  • Posts: 128
    • View Profile
Slow down game without altering user experience
« Reply #9 on: March 13, 2011, 05:57:18 pm »
Well - to get the snake clamped to the tiles - I would suggest some good old fashioned bitshifting.

int Xi = (int)X;
Xi = (Xi >> 5) << 5;

And use Xi to render the sprite, rather than X.

What this does is first turn an X position of say 145.23 into an integer of 145. (if you're worried about proper rounding, do ((int)X+0.5f).

Then it takes the value 145 and divides it by 32. (shift the value right 5 times - use /32 instead if you don't know what it is and don't trust me.) You now end up with the value 4,53125 which will be rounded down to 4.

The next part of that line multiplies by 32 (shifts the value left 5 times - use *32 instead for the same reasons as above). You now end up with the value 128.

What all this does is make sure your snake will "jump" one tile at a time - while still maintaining float positioning internally.

meto28

  • Newbie
  • *
  • Posts: 8
    • View Profile
Slow down game without altering user experience
« Reply #10 on: March 13, 2011, 05:58:29 pm »
Quote from: "Nexus"
Quote from: "Nexus"
Try to show us a minimal and complete example


Code: [Select]

  std::string           direction = "D";
  float                 TileSize = 32;
  float             ElapsedTime = App.GetFrameTime();

          if (direction == "D")
            {
              Sprite.SetPosition(X, Y);
              Y += ElapsedTime * 200;
            }

/* This should be the best to have my snake moving in the center of a tile will moving but it make it go too fast*/
 
           if (direction == "D")
            {
              Sprite.SetPosition(X, Y);
              Y += TileSize;
            }


TehKyle: You are talking about those lines ??
Code: [Select]
     if (App.GetInput().IsKeyDown(sf::Key::Down))