• Advertisement
Sign in to follow this  

[SDL] Moving right and left

This topic is 2074 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts



while (running) {
SDL_PollEvent(&event);

if (event.type == SDL_QUIT) {
SDL_Quit();
return 0;
} else if (event.type == SDL_KEYDOWN) {
int key_code = event.key.keysym.sym;

if (key_code == SDLK_ESCAPE) {
SDL_Quit();
return 0;
}

if (key_code == SDLK_RIGHT && player.x < SCREEN_WIDTH - 5 - player.width) player.x++;
if (key_code == SDLK_LEFT && player.x > 0 + 5) player.x--;
}

draw_rect(screen, player.x, player.y, player.width, player.height, 0xFF);

SDL_Flip(screen);
clear_screen(screen, 0, 0, 0);
}


This is my main code loop, and it's where I make my player move right or left. However, when I press right, and then release right and press left (fairly quick movement) again, the player will move a bit to the left a bit and then stops, requiring me to release LEFT and repress it to keep moving.

The player gets stuck all the time, it's hard to explain what's going on.

Any ideas on how can I fix this? Thank you in advance. Edited by munchor

Share this post


Link to post
Share on other sites
Advertisement
There are three (minor!) mistakes in your code that are working together to produce the buggy results.

The first mistake: You aren't separating out *reading input* from *updating the game*. You really should be.
SDL doesn't send you KEYDOWN events every frame, you just receive it once when the key first goes down... so your current code (If it weren't for other bugs working with it) would only move the character once each keypress, which is not what you want (Keypress -> Move 5 pixels. Keypress -> Move 5 pixels). What you want is (While the keypress is down, keep moving five pixels each frame*).

So you need to separate your "Update every frame" code from your "Read and react to input" code. This means, check for input first (if there is input), at the top of the "while (running)" loop, based off of the input change the internal state of the game (PlayerMovingLeft = true, PlayerMovingRight = false), and regardless of whether there is input or not, every frame react to the internal state of the game ( if(PlayerMovingLeft) {...move the player five pixels...} ).

*[size=2]Actually, what you really want is, "Each frame, depending on how much time is passed, move (PER_SECOND_MOVEMENT * fractionOfASecondThatPassed), but that's just complicating your current problem, and you can always learn about that some other time.

So that's the first issue, and your code should now look like this:

bool playerMovingLeft = false;
bool playerMovingRight = false;
while (running)
{
//-------------------------------------
//Check input, and change the internal state of the game.
//-------------------------------------

SDL_PollEvent(&event);

if (event.type == SDL_QUIT)
{
SDL_Quit();
return 0;
}
else if (event.type == SDL_KEYDOWN)
{
int key_code = event.key.keysym.sym;

if (key_code == SDLK_ESCAPE)
{
SDL_Quit();
return 0;
}

if (key_code == SDLK_RIGHT) playerMovingLeft = true;
if (key_code == SDLK_LEFT) playerMovingRight = true;
}
else if (event.type == SDL_KEYUP)
{
int key_code = event.key.keysym.sym;

if (key_code == SDLK_RIGHT) playerMovingLeft = false;
if (key_code == SDLK_LEFT) playerMovingRight = false;
}

//-------------------------------------
//Update the game data, based off of the internal state of the game.
//-------------------------------------

if (playerMovingLeft == true && player.x < SCREEN_WIDTH - 5 - player.width) player.x++;
if (playerMovingRight == true && player.x > 0 + 5) player.x--;

//-------------------------------------
//Draw the game, based off of the game data.
//-------------------------------------

draw_rect(screen, player.x, player.y, player.width, player.height, 0xFF);

SDL_Flip(screen);
clear_screen(screen, 0, 0, 0);
}



The second issue, is that you aren't checking the return value of SDL_PollEvent(). It returns true if there is input, otherwise it returns false and you're not supposed to use the [color=#000088][size=2]'event' variable for anything.



So your entire input should be wrapped in an if(...) statement, like this:


SDL_Event event;


bool gotSomeEventsToProcess = SDL_PollEvent(&event);
if( gotSomeEventsToProcess ) //Or just: if( SDL_PollEvent(&event) )
{
if (event.type == SDL_QUIT)
{
...
}
else if (event.type == SDL_KEYDOWN)
{
...
}
else if (event.type == SDL_KEYUP)
{
...
}
}



The third issue is this: You might get more than one input event each frame! So the "if( SDL_PollEvent(&event) )" should actually be a "while( SDL_PollEvent(&event) )", otherwise if you get too many input events in one frame, it'll wait for the next frame before dealing with them, and your game will eventually start lagging where you click on something and it only processes the click many frames later.

So your actual code should look like this:
bool playerMovingLeft = false;
bool playerMovingRight = false;

while (running)
{
//-------------------------------------
//Check input, and change the internal state of the game.
//-------------------------------------

SDL_Event event;
while( SDL_PollEvent(&event) )
{
if (event.type == SDL_QUIT)
{
SDL_Quit();
return 0;
}
else if (event.type == SDL_KEYDOWN)
{
int key_code = event.key.keysym.sym;

if (key_code == SDLK_ESCAPE)
{
SDL_Quit();
return 0;
}

if (key_code == SDLK_RIGHT) playerMovingLeft = true;
if (key_code == SDLK_LEFT) playerMovingRight = true;
}
else if (event.type == SDL_KEYUP)
{
int key_code = event.key.keysym.sym;

if (key_code == SDLK_RIGHT) playerMovingLeft = false;
if (key_code == SDLK_LEFT) playerMovingRight = false;
}
}

//-------------------------------------
//Update the game data, based off of the internal state of the game.
//-------------------------------------

if (playerMovingLeft == true && player.x < SCREEN_WIDTH - 5 - player.width) player.x++;
if (playerMovingRight == true && player.x > 0 + 5) player.x--;

//-------------------------------------
//Draw the game, based off of the game data.
//-------------------------------------

draw_rect(screen, player.x, player.y, player.width, player.height, 0xFF);

SDL_Flip(screen);
clear_screen(screen, 0, 0, 0);
}


This should completely fix your problem - these are common mistakes, ones which have burned me a couple of times when starting out. smile.png

You might even want to break up your "drawing" code, your "input code", and your "update" code into three separate functions for organization, but that's up to your own preferences. Good luck with your game! Edited by Servant of the Lord

Share this post


Link to post
Share on other sites
You can also use SDL_GetKeyState() to read keypresses, like this:


Uint8 *keystates = SDL_GetKeyState(NULL);
if (keystate[SDLK_LEFT] && player.x < SCREEN_WIDTH - 5 - player.width) player.x++;
if (keystate[SDLK_RIGHT] && player.x > 0 + 5) player.x--;


...and thus get rid of the 'playerMovingLeft' and 'playerMovingRight' variables, since SDL does that internally, making your code cleaner. But it's important that you understood what is actually happening before using SDL_GetKeyState() as a magic bullet. Edited by Servant of the Lord

Share this post


Link to post
Share on other sites
Servant Of The Lord, I actually read what you said twice, very nice explanation, I shall implement it right away and see how it works :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement