Reputation: 151
Well, I've already started a graphic project on c++ and now I need some help. This is a ping-pong game which has 2 rackets; one goes up and down via UP and DOWN keys, and the other moves by S and W keys.
I need to do something that it both players will be able to move their rackets at the same time.
sorry for my bad English!
#include <iostream>
#include "SDL/SDL.h"
#include <SDL/SDL_gfxPrimitives.h>
using namespace std;
int main()
{
SDL_Surface* screen = SDL_SetVideoMode(1200, 800 ,32, 0);
int i=0,j = 0;
bool a = 0 ;
while(true)
{
boxRGBA(screen, 1000, 200+j, 1050, 350+j, 0, 0, 0, 255);
boxRGBA(screen, 100, 200+i,150,350+i,0,0,0,255);
SDL_Event event;
if(!SDL_PollEvent(&event));
{
if(event.type == SDL_QUIT)
return 0;
if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_UP)
j -=10;
if(event.key.keysym.sym == SDLK_DOWN)
j +=10;
if(event.key.keysym.sym == SDLK_w)
i-=10;
if(event.key.keysym.sym == SDLK_s)
i+=10;
}
else if (event.type == SDL_KEYUP)
a=1;
}
//if(a)
//j += b;
boxRGBA(screen, 1000, 200+j, 1050, 350+j, 255, 50, 0, 255);
boxRGBA(screen, 100, 200+i,150,350+i,50,0,255,255);
SDL_Flip(screen);
SDL_Delay(2);
}
SDL_Delay(5000);
return 0;
}
Upvotes: 0
Views: 113
Reputation: 211166
SDL_GetKeyboardState
givs you a snapshot of the current state of the keyboard.
const Uint8 *state = SDL_GetKeyboardState(NULL);
if (state[SDL_SCANCODE_UP])
j -=10;
if (state[SDL_SCANCODE_DOWN])
j +=10;
if (state[SDL_SCANCODE_W])
i -=10;
if (state[SDL_SCANCODE_S])
i +=10;
Upvotes: 1
Reputation: 19617
if(!SDL_PollEvent(&event)); // what's ; doing here? Why are you negating the return value?
Try and change this to while
:
while(SDL_PollEvent(&event)) { ... }
That way it will poll all available events in the queue.
Upvotes: 2