I almsot got it now! When i move the mouse and hold down left or right, it moves! I think it is because the mouse gives the program a new event. Right? So if i would like to do this without moving the mouse. How should i do?
Code:
Code:
//While the user hasn't quit
while( quit == false )
{
//If there's an event to handle
while (SDL_PollEvent(&event))
{
if (event.type == SDL_KEYDOWN)
{
switch(event.key.keysym.sym)
{
case SDLK_LEFT:
left = true;
break;
case SDLK_RIGHT:
right = true;
break;
case SDLK_ESCAPE:
quit = true;
break;
} }
if (event.type == SDL_KEYUP)
{
switch(event.key.keysym.sym)
{
case SDLK_LEFT:
left = false;
SDL_FillRect( screen, NULL, NULL );
apply_surface( 0, 0, background, screen );
piece = load_image( "piece.png" );
apply_surface( x, y, piece, screen );
break;
case SDLK_RIGHT:
right = false;
SDL_FillRect( screen, NULL, NULL );
apply_surface( 0, 0, background, screen );
piece = load_image( "piece.png" );
apply_surface( x, y, piece, screen );
break;
}
}
//Check if the right is pressed
if (right == true)
{
x = x + 1;
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
if (left == true)
{
x = x - 1;
SDL_FillRect( screen, NULL, NULL );
apply_surface( 0, 0, background, screen );
piece = load_image( "piece-left.png" );
apply_surface( x, y, piece, screen );
}
//Apply the square
apply_surface( 400, 150, square, screen );
}
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
}