Hello Folks,
I try to make a little game Pong. Now i have a situation: i have 4 keys defined;
for player 1: keys w and s. w = UP, S = DOWN.
for player 2: Keys down arrow, up arrow.
the problem is that i want the bats of the player keep moving if the player hold their the down/up keys. but it quickly turned out that if both players hold or push their up/down keys only one of them can move (because my SDL_Event Object can only hold one of the pushed keys)
So i wondered if their is a way to make 2 separated SDL_Event Objects that only handles specific keys of the player so:
SDL_Event EventsPlayer1 handles s and w keys.
SDL_Event EventsPlayer2 handles UP and DOWN arrow keys.
So is their a way to handle multiple keys with SDL?
EDIT:
I already tried the SDL function SDL_EnableKeyRepeat(int delay, int interval), but this solves only a part of the problem. it can repeat a key correctly but, it cant manage multiple keys at the time).
Thanks for helping!
Jelte.
if somebody is curious about how i tried to handle this problem, check this code:
Code:
while(SDL_PollEvent(&Event))
{
if(Event.type == SDL_QUIT)
{
Quit = true;
}
else if(Event.type == SDL_KEYDOWN)
{
if(Event.key.keysym.sym == SDLK_ESCAPE)
{
Quit = true;
}
else if(Event.key.keysym.sym == SDLK_w || Event.key.keysym.sym == SDLK_UP || Player1HoldKeys == true || Player2HoldKeys == true)
{
if(Event.key.keysym.sym == SDLK_w || (Player1HoldKeys == true &&
this->LastKeyPressed == 2))
{
this->Player1HoldKeys = true;
this->LastKeyPressed = 2;
this->ObjPlayer1->MoveBat(true,this->ObjGraphics->ReturnWindowHeigth());
}
else if(Event.key.keysym.sym == SDLK_UP || (Player2HoldKeys == true &&
this->LastKeyPressed == 4))
{
this->Player2HoldKeys = true;
this->LastKeyPressed = 4;
this->ObjPlayer2->MoveBat(true,this->ObjGraphics->ReturnWindowHeigth());
}
}
else if(Event.key.keysym.sym == SDLK_s || Event.key.keysym.sym == SDLK_DOWN ||
Player1HoldKeys == true || Player2HoldKeys == true)
{
if(Event.key.keysym.sym == SDLK_s || (Player1HoldKeys == true &&
this->LastKeyPressed == 1))
{
this->Player1HoldKeys = true;
this->LastKeyPressed = 1;
this->ObjPlayer1->MoveBat(false,this->ObjGraphics->ReturnWindowHeigth());
}
else if(Event.key.keysym.sym == SDLK_DOWN || (Player2HoldKeys == true &&
this->LastKeyPressed == 3))
{
this->Player2HoldKeys = true;
this->LastKeyPressed = 3;
this->ObjPlayer2->MoveBat(false,this->ObjGraphics->ReturnWindowHeigth());
}
}
}
else if(Event.type == SDL_KEYUP)
{
if(this->LastKeyPressed == 1 || this->LastKeyPressed == 2)
{
this->Player1HoldKeys = false;
}
else if(this->LastKeyPressed == 3 || this->LastKeyPressed == 4)
{
this->Player2HoldKeys = false;
}
this->LastKeyPressed = 0;
}
}
this->ObjGraphics->MoveBall();
this->ObjGraphics->Draw(this->ObjPlayer1,this->ObjPlayer2);