Hi!
I'm making a game, just to learn SDL. But i seem to have some trouble with my code. If i cpoy and paste my code from the while into the switch it works. It is the same code. But if i do like that, the piece wouldn't move if you hold down the button. It would only move when you press the button.
How can i make this work?
Code://While the user hasn't quit
while( quit == false )
{
//If there's an event to handle
if( SDL_PollEvent( &event ) )
{
//If a key was pressed
if( event.type == SDL_KEYDOWN )
{
//Set the proper holder surface
switch( event.key.keysym.sym )
{
case SDLK_LEFT:
left = true;
break;
case SDLK_RIGHT:
right = true;
break;
case SDLK_ESCAPE:
quit = true;
break;
}
}
//Check if the right is pressed
while (right == true)
{
x = x + 10;
SDL_FillRect( screen, NULL, NULL );
apply_surface( 0, 0, background, screen );
piece = load_image( "piece-right.png" );
apply_surface( x, y, piece, screen );
}
//Check if the left is pressed
while (left == true)
{
x = x - 10;
SDL_FillRect( screen, NULL, NULL );
apply_surface( 0, 0, background, screen );
piece = load_image( "piece-left.png" );
apply_surface( x, y, piece, screen );
}
}
//If the user has Xed out the window
else if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
}