# SDL game trouble!!

This topic is 3653 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello everyone, I am having trouble with a game I am making, my personal "space invaders" version, using SDL. I dont know how to make the blast of the spaceship flow upwards... I tried in several different ways and none of them were perfect! Here, there is some examples of what I have been trying to do:
-------------------------------------
/* This one creates the whole move, but it doesnt flow... the blast is drawn on the screen but it appears at once from bottom to top without flowing through it... */

if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_ESCAPE)
done = 1;

if(event.key.keysym.sym == SDLK_SPACE)
{
for(bulletpos=720; bulletpos>0;)
{
bulletpos -= 30;
blastBlit(blast, xpos+50, bulletpos-25);
}
SDL_Flip(screen);
}
}

/* In this I have to press the spacebar several times to make it goes from bottom to top:  */

if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_ESCAPE)
done = 1;

if(event.key.keysym.sym == SDLK_SPACE)
{
for(bulletpos=720; bulletpos>0;)
{
bulletpos -= 30;
blastBlit(blast, xpos+50, bulletpos-25);
}
SDL_Flip(screen);
}
}

/* And in this one it creates the same effect as the first one but if I let the spacebar pressed it keeps blasting and doesnt not stop! */

UserInput = SDL_GetKeyState(NULL);
if (UserInput[SDLK_SPACE])
{
for(bulletpos=720; bulletpos>0;)
{
bulletpos -= 30;
blastBlit(blast, xpos+50, bulletpos-25);
}
SDL_Flip(screen);
}
-------------------------------------


I feel that there is a much simpler way that would mix all those codes up and create the blast going up with no trace behind and flowing nicely!! I hope you guys understood what I meant and please ask if there is any question about the rest of the code... thanks :D [Edited by - Kurama_gamer on August 19, 2008 5:41:04 PM]

##### Share on other sites
A comprehensive answer to your question would be somewhat lengthy, but I'll try at least to get you pointed in the right direction.

First of all, I'll mention that SDL has some 'key repeat' options. In your case, you probably want to turn 'key repeat' off so that there will only be one projectile fired per key press (see the SDL docs for how to do this).

As for the problems regarding the movement of your projectiles, you'll need to take a different approach to your game loop to get the results you're after.

Although there are various ways to approach the game loop, it will usually break down to something like the following:

1. Compute the amount of time that has passed since the beginning of the last update. This will be your 'time step'.

2. Process any user input and respond accordingly.

3. Update all game objects using the time step.

4. Render the game objects.

5. Go back to 1.

When the user presses the 'fire' key, the only thing you will do at that point is create a new projectile entity and set its initial state (the details of how you manage this are up to you). It is in step 3 that you'll actually move the projectile. Once the projectile has expired (e.g. as a result of colliding with something or moving past the top of the screen), you're done with it, and can destroy the object that represents it (e.g. flag it as 'inactive', remove it from the container in which it resides, etc.).

This may all sound a bit complicated, but once you have this basic architecture in place, things will proceed much more smoothly.

Post back if you need further details.

##### Share on other sites
jyk, thanks for your reply .... and I am glad to post here my resolution!! =D

You asked about my game loop and here it is (it is just missing the time step that I intend to look up at the SDL timing functions and prepare one)

It is somewhat like this
-------------------------------------int main(int argc, char *args[]){/* SDL_Init() is put in here *///Load my imagesLoadImages();while(running == 1){	SDL_Event event;//Check for events	while(SDL_PollEvent(&event))	{	    if(event.type == SDL_QUIT)		running = 0;	    if(event.type == SDL_KEYDOWN)		{			if(event.key.keysym.sym == SDLK_ESCAPE)				running = 0;                }        }	//Render my objects      DrawScene()}// go back to stage 0return 0;}------------------------------------------

and now my resolution .... I passed the past 2 days trying to solve the problem I had, a little every now and then, and just now a light lit on my mind and I tried another thing.... the one that worked =D

I was trying to do the whole logical process and rendering in my "events" function, which was my mistake.... i just had to put the logical process (a little better elaborated) there, and put my rendering in my normal 'render function' (duhhhh what simple solution =D)

A little of the code:
--------------------------------------void DrawScene(){	DrawIMG(background);	DrawIMG(spaceship, xpos, ypos, NULL);	if(playerFire)	{		DrawIMG(blast, xpos+50, bullety, NULL);		bullety -= 20;	}	SDL_Flip(screen);}int main(int argc, char *args[]){(...)if(event.key.keysym.sym == SDLK_SPACE){      playerFire = true;	if(bullety < 1)        {		playerFire = false;		bullety = 675;	}}}-----------------------------------

P.S.: This is my first game and I am only a teenager, that is why I am such a excited, persistent newbie =D ... I will get better with time (I hope =D)
P.P.S: I just have to fix now the 'xpos' thing and declare a specific one (different from the spaceship one) to the bullet.

Thanks again!

[Edited by - Kurama_gamer on August 19, 2008 5:42:46 PM]

1. 1
2. 2
JoeJ
17
3. 3
4. 4
5. 5
frob
11

• 13
• 16
• 13
• 20
• 12
• ### Forum Statistics

• Total Topics
632178
• Total Posts
3004608

×