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

Author Topic: Keep sprite in movement  (Read 3271 times)

0 Members and 1 Guest are viewing this topic.

meto28

  • Newbie
  • *
  • Posts: 8
    • View Profile
Keep sprite in movement
« on: March 09, 2011, 04:40:23 pm »
Hi,

I'm doing a Snake game and I would like to know if there is a "clean" way to make it continue moving after pressing a key and not only at the press of the key

Lupinius

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
Keep sprite in movement
« Reply #1 on: March 09, 2011, 04:41:44 pm »
You could store the direction of the last key pressed and move your sprite in the according direction every frame.

meto28

  • Newbie
  • *
  • Posts: 8
    • View Profile
Keep sprite in movement
« Reply #2 on: March 09, 2011, 05:09:59 pm »
You mean like this:
Code: [Select]

if (direction = "L")
{
Sprite.Move(-x, y);
}
if (direction = "U")
{
Sprite.Move(x, -y);
}

etc...

With Events instead of Input
Is that really a right way to do it ??

Lupinius

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
Keep sprite in movement
« Reply #3 on: March 09, 2011, 06:41:45 pm »
There is no "wrong" way. As long as you know what you're doing your code will probably be good (or at least understandable).

devlin

  • Full Member
  • ***
  • Posts: 128
    • View Profile
Keep sprite in movement
« Reply #4 on: March 09, 2011, 07:00:26 pm »
Code: [Select]
namespace SnakeDirection
{
  enum Value
  {
    UP, DOWN, LEFT, RIGHT
  };
}

// and then in your app class:
SnakeDirection::Value m_direction;


Then when you get a button press:
Code: [Select]
switch (key)
{
case sf::Key::Up:
   m_direction = SnakeDirection::UP;
   break;

case sf::Key::Left:
   m_direction = SnakeDirection::LEFT;
   break;

//etc
}


And in your update function:

Code: [Select]
switch (m_direction)
{
case SnakeDirection::LEFT:
   //moveLeft
break;

case SnakeDirection::RIGHT:
   //moveRight
break;

//etc
}

bastien

  • Full Member
  • ***
  • Posts: 231
    • View Profile
    • http://bastien-leonard.alwaysdata.net
Keep sprite in movement
« Reply #5 on: March 10, 2011, 12:38:49 am »
You can use the Input class to know whether a key is currently pressed. Obtain the Input instance with RenderWindow.GetInput().
Check out pysfml-cython, an up to date Python 2/3 binding for SFML 2: https://github.com/bastienleonard/pysfml-cython

devlin

  • Full Member
  • ***
  • Posts: 128
    • View Profile
Keep sprite in movement
« Reply #6 on: March 10, 2011, 08:36:51 am »
Quote from: "bastien"
You can use the Input class to know whether a key is currently pressed. Obtain the Input instance with RenderWindow.GetInput().

That's not what the OP wanted though:

"after pressing a key and not only at the press of the key"

:)

meto28

  • Newbie
  • *
  • Posts: 8
    • View Profile
Keep sprite in movement
« Reply #7 on: March 10, 2011, 04:00:02 pm »
Thanks for your answers, now I have another issues.
I want my snake to always move on a distance of 1 tile
So I tried instead of Move the SetPosition but it still moving the same way (not "jumping" from a square to another)

The other one is that the snake only moves up or left if I use GetFrameTime:
Code: [Select]
     if (direction == "D")
        {
          Sprite.SetPosition(X, Y);
          Y += TileSize * ElapsedTime; //Doesn't work
        }
      if (direction == "U")
        {
          Sprite.SetPosition(X, Y);
          Y -= TileSize * ElapsedTime; //Works
        }
      if (direction == "L")
        {
          Sprite.SetPosition(X, Y);
          X -= TileSize * ElapsedTime; //Works
        }
      if (direction == "R")
        {
          Sprite.SetPosition(X, Y);
          X += TileSize; //Works
        }