while(quit != true)
{
if (SDL_Flip(screen) == -1)
{
return 1;
}
applySurface(0, 0, background, screen);
applySurface(playerX, playerY, player, screen);
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
// Quits the game
quit = true;
}
if(event.type == SDL_KEYDOWN)
{
switch(event.key.keysym.sym)
{
case SDLK_UP:
playerY -= 6;
break;
case SDLK_DOWN:
playerY += 6;
break;
case SDLK_LEFT:
playerX -= 6;
break;
case SDLK_RIGHT:
playerX += 6;
break;
}
}
}
}
i don't know if this is simple but thanks in advance :D
SDL Question
Hello, i have been learning SDL with Lazy Foo's tutorials and now i am trying to make a simple game where the character can walk around. i have made it so the player moves when one of the arrow keys is pressed but you have to repeatedly press it.
So my question is how do you make it so you just hold down the button?
this is my code for drawing the player, background:
EDIT: forgot to mention applySurface() is the function in the Lazy Foo tutorials for blitting the surfaces to the screen.
All of this is mentioned in tutorial 16. Don't just download the source code, the tutorials only work if you actually read them
Also read article 4 after you're done. Your game loop needs better structure.
Also read article 4 after you're done. Your game loop needs better structure.
simple way of doing it - though not the most efficent
create 3 global ints
int xVel =0;
int yVel =0;
int speed =6;
in your KEYDOWN switch
have RIGHT
set xVel = speed;
in LEFT
set xVel = -speed;
in UP
set yVel = -speed;
in DOWN
set yVel = speed;
create another if
if(event.type == SDL_KEYUP)
use same switch
set the xVel and yVel back to 0
then under your input
move character based on xVel and yVel
pseudo code
create 3 global ints
int xVel =0;
int yVel =0;
int speed =6;
in your KEYDOWN switch
have RIGHT
set xVel = speed;
in LEFT
set xVel = -speed;
in UP
set yVel = -speed;
in DOWN
set yVel = speed;
create another if
if(event.type == SDL_KEYUP)
use same switch
set the xVel and yVel back to 0
then under your input
move character based on xVel and yVel
pseudo code
//load stuff//draw stuff//inputif(keydown) set x and y velocities to speed or -speed based on directionif(keyup) set x and y velocities to zero based on need//update charactercharacter.xPosition += xVel;character.yPosition += yVel;
This way has always done the job for me
Ive put only the input related shivers to save forum bytes, ask for further if needed =)
bool keys[323] = {false}; // among globals, holds keyboard buttons statesbool btns[3] = {false}; // among globals, holds mouse button statesSDL_Event event; // among globals, key to the users handsvoid updateinput(); // among prototypesvoid handleinput(); // among prototypesint main (int argc, char*argv[]){ init(); // initializes sub/systems loadobjects(); // loads objs,graphics,fonts,sounds while (app){ // main loop ticks1 = SDL_GetTicks(); updateinput(); // the name is self-explainatory handleinput(); // same here i believe //renderscene(); and all you need ticks2 = SDL_GetTicks(); if ((ticks2 - ticks1) < FRAME_TIME) SDL_Delay(FRAME_TIME - (ticks2 - ticks1)); } freeobjects(); // frees space; counter part of loadobjects() deinit(); // closes sub/systems; counter part of init() return 0;}void updateinput(){ while (SDL_PollEvent(&event)){ if (event.type == SDL_KEYDOWN) // if a button is pressed keys[event.key.keysym.sym] = true; // store its state if (event.type == SDL_KEYUP) // and only when released keys[event.key.keysym.sym] = false;// change it if (event.type == SDL_MOUSEBUTTONDOWN) // same as above btns[event.button.button] = true; if (event.type == SDL_MOUSEBUTTONUP) btns[event.button.button] = false; }}/////////////////////////////////////////////////////////////////////////////////////////////////void handleinput(){ if(keys[SDLK_LEFT]) buddy_x -= 3; if(keys[SDLK_DOWN]) buddy_y += 3; if(keys[SDLK_UP]) buddy_y -= 3; if(keys[SDLK_RIGHT]) buddy_x += 3; // a couple ways of quittin if ( btns[SDL_BUTTON_LEFT] || btns[SDL_BUTTON_RIGHT] || btns[SDL_BUTTON_MIDDLE]) app = false; if(event.type == SDL_QUIT) app = false ;}
Ive put only the input related shivers to save forum bytes, ask for further if needed =)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement