Game control

I’m working on a space shooter and have a problem with control of my
spaceship. I’m useing SDL_KEYDOWN to control the input, but this don’t works,
if player press two keys (SDLK_UP and SDLK_LEFT) the player loose the
controll over the ship… it can move up and left (it should do so), but it
can also start moveing down and left or something else strange…

-------CODE------
//this is the main game loop.

while( game_state != GAME_EXIT )
{
switch(game_state)
{
case GAME_INIT:
{
Init_Game();
game_state=GAME_SPLASHUP;
} break;

		case GAME_SPLASHUP:
		{
			Key_pressed();
			Draw_Splashup();
			PlaySound();
		} break;

		case GAME_MENU:
		{
		} break;

		case GAME_STARTING:
		{
			InitGameGraphics();
			game_state=GAME_RESTART;
		} break;
		case GAME_RUN:
		{
			Key_pressed();
			myShip.move();

			if(fireing){
				if(lastfire+firerate <= SDL_GetTicks()){
					fire();
					lastfire=SDL_GetTicks();
				}
			}
			koll();
			DrawGameGraphics();
			PlaySound();
		} break;

		case GAME_RESTART:
		{
			game_state=GAME_RUN;
		} break;

		case GAME_OVER:
		{
			Key_pressed();
			DrawGameOver();
		} break;

		case GAME_EXIT:
		{
		} break;
	}
}

— end of main gameloop----

//and this is my keypressed function.
void Key_pressed()
{
while(SDL_PollEvent(&event)){
if(event.type == SDL_QUIT){
game_state=GAME_EXIT;
}
if(event.type == SDL_KEYDOWN){
if(game_state == GAME_SPLASHUP || game_state == GAME_OVER){
if(event.key.keysym.sym == SDLK_SPACE){
Restart_Game();
game_state=GAME_STARTING;
}
}
//gamekontrollen buggar utav bara fan h?r, ett skepp som flyger
// p? diagonalen tycks inte f?lja n?gra f?rnuftiga regler
// ?r det SDL som strular?
if(event.key.keysym.sym == SDLK_ESCAPE){
game_state=GAME_EXIT;
}
if(event.key.keysym.sym == SDLK_RCTRL){
fireing=true;
}
if(event.key.keysym.sym == SDLK_UP){
myShip.North=true;
myShip.South=false;
}
if(event.key.keysym.sym == SDLK_RIGHT){
myShip.West=true;
myShip.East=false;
}
if(event.key.keysym.sym == SDLK_LEFT){
myShip.East=true;
myShip.West=false;
}
if(event.key.keysym.sym == SDLK_DOWN){
myShip.South=true;
myShip.North=false;
}
}

	if(event.type == SDL_KEYUP){
		if(event.key.keysym.sym == SDLK_RCTRL){
			fireing=false;
		}
		if(event.key.keysym.sym == SDLK_UP){
			myShip.North=false;
		}
		if(event.key.keysym.sym == SDLK_RIGHT){
			myShip.West=false;
		}
		if(event.key.keysym.sym == SDLK_LEFT){
			myShip.East=false;
		}
		if(event.key.keysym.sym == SDLK_DOWN){
			myShip.South=false;
		}
	}

}

}
----END OF CODE-------
Recursive, adj.; see Recursive
//Perra

Hey Fredrik,

2 suggestions for you, either one will get you where you want.

1 is to check out this page from the docs for how to do proper game
movement:
http://sdldoc.csn.ul.ie/guideinputkeyboard.php

the 2nd it to use SDL_GetKeyState which returns an array of the states of
the keys!
so you would do something like this:==================================
unsigned char *Keys;
Keys=SDL_GetKeyState(0);

if(Keys[SDLK_UP])
PlayerY–;

if(Keys[SDLK_DOWN])
PlayerY++;

if(Keys[SDLK_LEFT])
PlayerX–;

if(Keys[SDLK_RIGHT])
PlayerX++

I personaly prefer the second method since it takes less code and looks
cleaner.

hope that helps!

----- Original Message -----
From: perrascout@linux.se (Fredrik Persson)
To:
Sent: Monday, March 15, 2004 3:41 PM
Subject: [SDL] Game control

I’m working on a space shooter and have a problem with control of my
spaceship. I’m useing SDL_KEYDOWN to control the input, but this don’t
works,
if player press two keys (SDLK_UP and SDLK_LEFT) the player loose the
controll over the ship… it can move up and left (it should do so), but it
can also start moveing down and left or something else strange…

-------CODE------
//this is the main game loop.

while( game_state != GAME_EXIT )
{
switch(game_state)
{
case GAME_INIT:
{
Init_Game();
game_state=GAME_SPLASHUP;
} break;

case GAME_SPLASHUP:
{
Key_pressed();
Draw_Splashup();
PlaySound();
} break;

case GAME_MENU:
{
} break;

case GAME_STARTING:
{
InitGameGraphics();
game_state=GAME_RESTART;
} break;
case GAME_RUN:
{
Key_pressed();
myShip.move();

if(fireing){
if(lastfire+firerate <= SDL_GetTicks()){
fire();
lastfire=SDL_GetTicks();
}
}
koll();
DrawGameGraphics();
PlaySound();
} break;

case GAME_RESTART:
{
game_state=GAME_RUN;
} break;

case GAME_OVER:
{
Key_pressed();
DrawGameOver();
} break;

case GAME_EXIT:
{
} break;
}
}
— end of main gameloop----

//and this is my keypressed function.
void Key_pressed()
{
while(SDL_PollEvent(&event)){
if(event.type == SDL_QUIT){
game_state=GAME_EXIT;
}
if(event.type == SDL_KEYDOWN){
if(game_state == GAME_SPLASHUP || game_state == GAME_OVER){
if(event.key.keysym.sym == SDLK_SPACE){
Restart_Game();
game_state=GAME_STARTING;
}
}
//gamekontrollen buggar utav bara fan h?r, ett skepp som flyger
// p? diagonalen tycks inte f?lja n?gra f?rnuftiga regler
// ?r det SDL som strular?
if(event.key.keysym.sym == SDLK_ESCAPE){
game_state=GAME_EXIT;
}
if(event.key.keysym.sym == SDLK_RCTRL){
fireing=true;
}
if(event.key.keysym.sym == SDLK_UP){
myShip.North=true;
myShip.South=false;
}
if(event.key.keysym.sym == SDLK_RIGHT){
myShip.West=true;
myShip.East=false;
}
if(event.key.keysym.sym == SDLK_LEFT){
myShip.East=true;
myShip.West=false;
}
if(event.key.keysym.sym == SDLK_DOWN){
myShip.South=true;
myShip.North=false;
}
}

if(event.type == SDL_KEYUP){
if(event.key.keysym.sym == SDLK_RCTRL){
fireing=false;
}
if(event.key.keysym.sym == SDLK_UP){
myShip.North=false;
}
if(event.key.keysym.sym == SDLK_RIGHT){
myShip.West=false;
}
if(event.key.keysym.sym == SDLK_LEFT){
myShip.East=false;
}
if(event.key.keysym.sym == SDLK_DOWN){
myShip.South=false;
}
}

}
}
----END OF CODE-----


Recursive, adj.; see Recursive
//Perra


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

— Fredrik Persson wrote:

I’m working on a space shooter and have a problem
with control of my
spaceship. I’m useing SDL_KEYDOWN to control the
input, but this don’t works,
if player press two keys (SDLK_UP and SDLK_LEFT) the
player loose the
controll over the ship… it can move up and left
(it should do so), but it
can also start moveing down and left or something
else strange…

Well I did notice that pressing the left key makes it
go east, which I usually think of as to the right…
and the right key makes it go west, which I usually
think of as to the left…

And unless we’re looking up from underneath the ship,
either north/south or east/west have been flipped in
your key down handler.

Not sure if this is helping to cause the problem? at
school right now on a compilerless machine, I can
check out what it does when I get home.__________________________________
Do you Yahoo!?
Yahoo! Mail - More reliable, more storage, less spam
http://mail.yahoo.com