Jump to content
  • Advertisement
Sign in to follow this  
MPG

Making a ship fire(Space Invaders).

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

;SDL_Rect ship;
ship.x = 500;
ship.y = 579;
SDL_Rect laser;
  laser.x = ship.x;
laser.y = ship.y - 5;
SDL_Event global;
int go = 1;
while(k==true)
{
	
    
SDL_BlitSurface(hero,NULL,screen,&ship);
SDL_Flip(screen);
SDL_PollEvent(&global);

	

SDL_PollEvent(&global);
if ( global.type == SDL_QUIT )  
	  {  
		  k = false;
		  SDL_Quit();
	  }
if ( global.type == SDL_KEYDOWN )
      {
        if ( global.key.keysym.sym == SDLK_ESCAPE ) 
		{ 
			k = false; 
			SDL_Quit();
		}
		if ( global.key.keysym.sym == SDLK_UP)
		{
		     ship.y -=1;
           SDL_FillRect(screen,0,0);           
SDL_BlitSurface(hero,NULL,screen,&ship);
		}
if ( global.key.keysym.sym == SDLK_DOWN)
		{
		     ship.y +=1;
           SDL_FillRect(screen,0,0);           
SDL_BlitSurface(hero,NULL,screen,&ship);
		}
if ( global.key.keysym.sym == SDLK_LEFT)
		{
		     ship.x -=1;
           SDL_FillRect(screen,0,0);           
SDL_BlitSurface(hero,NULL,screen,&ship);
		}
if ( global.key.keysym.sym == SDLK_RIGHT)
		{
		     ship.x +=1;
           SDL_FillRect(screen,0,0);           
SDL_BlitSurface(hero,NULL,screen,&ship);
		}

if ( global.key.keysym.sym == SDLK_SPACE) //fire
		{
		   

           SDL_FillRect(screen,0,0);           
SDL_BlitSurface(helaser,NULL,screen,&laser);
go = 2;
		}
if ( global.key.keysym.sym == SDLK_SPACE)
		{
        if ( global.type == SDLK_UP )
      {

	laser.y -=6;

           SDL_FillRect(screen,0,0);           
SDL_BlitSurface(helaser,NULL,screen,&laser);

}
		}
	  
 SDL_Flip(screen);;
}
}
return (0);
} 
As the game stands now,I have to hold down the space bar to make the ship fire. How do I fix it so that when I press the space bar it goes forward automaticaly? [Edited by - MPG on September 15, 2005 9:01:06 PM]

Share this post


Link to post
Share on other sites
Advertisement
Couple of things. You may be editing this as we speak, but the tags use [ ] instead of < >, and without proper indentation the code is difficult to read.

Another tip is to only include relevant code in your post. The less code there is to dig through, the more likely people are to look at it.

Finally, I'm guessing you just need some very simple entity management. Only the action of firing the laser should be tied to pressing the space bar; the action of updating the laser should be tied to whether or not a laser object is currently active. Setting aside the issue of multiple projectiles (which you'll probably want eventually), you can do something like this:

1. Have a laserIsActive flag, initially set to false
2. If the space bar is pressed and the flag is currently false, set the laser's position and set the flag to true
3. Any frame that the flag is true, update the laser (move, draw, check for collision with aliens, and so on)
4. If the laser hits an alien or reaches the top of the screen, set the flag to false

To support multiple projectiles, you'll need an array of projectiles, each with its own 'active' flag. When the space bar is pressed, find one that's inactive, and initialize and activate it.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Use square brackets for "["source"]"

Share this post


Link to post
Share on other sites
another option is to give the laser an x,y velocity and just change the velocity depepnding on whether it was fired or not, once the collsion or bounds check takes effect, set the velocity, starting position back to defaults and hide the image until the fire button is pressed again, i'm unsure if your familiar with classes since it doesn't seem you used any, but they can be very helpful, and are much more practical when designing game objects than simple structures

Share this post


Link to post
Share on other sites
What I see is that the Check on the "Up" key is inside the if statement that checks the Space Bar. So the up only applies if the space does too. That might would fix it. The other thing is that you should try separating the objects into classes like suggested in other posts. Each object would take care of itself without being affected by any other random if statements, like in this code.

Share this post


Link to post
Share on other sites
Heres what I got:

while(k==true)
{


SDL_BlitSurface(hero,NULL,screen,&ship);
SDL_Flip(screen);
SDL_PollEvent(&global);



SDL_PollEvent(&global);
if ( global.type == SDL_QUIT )
{
k = false;
SDL_Quit();
}
if ( global.type == SDL_KEYDOWN )
{
if ( global.key.keysym.sym == SDLK_ESCAPE )
{
k = false;
SDL_Quit();
}
if ( global.key.keysym.sym == SDLK_UP)
{
ship.y -=1;
SDL_FillRect(screen,0,0);
SDL_BlitSurface(hero,NULL,screen,&ship);
}
if ( global.key.keysym.sym == SDLK_DOWN)
{
ship.y +=1;
SDL_FillRect(screen,0,0);
SDL_BlitSurface(hero,NULL,screen,&ship);
}
if ( global.key.keysym.sym == SDLK_LEFT)
{
ship.x -=1;
SDL_FillRect(screen,0,0);
SDL_BlitSurface(hero,NULL,screen,&ship);
}
if ( global.key.keysym.sym == SDLK_RIGHT)
{
ship.x +=1;
SDL_FillRect(screen,0,0);
SDL_BlitSurface(hero,NULL,screen,&ship);
}

if ( global.key.keysym.sym == SDLK_SPACE)
{



SDL_BlitSurface(helaser,NULL,screen,&laser);

}

}
}
if(fire == UP)
{

laser.y -=6;

SDL_FillRect(screen,0,0);//error-

SDL_BlitSurface(helaser,NULL,screen,&laser);

}
SDL_Flip(screen);
return(0);
Unhandled exception at 0x1002287d in Zero gravity.exe: 0xC0000005: Access violation reading location 0x00000004.

Share this post


Link to post
Share on other sites
One thing you should do just to make things more readable and easier to keep track of is to change all those 'if' statements checking for various keypresses into a switch. Something like below. With all those 'if's you're checking for keypresses even if one has already been found and the appropriate action already taken. Ex:If escape is pressed, there is no reason to check for up and down.

Also
"int SDL_FillRect(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color);"
In yours. why is SDL_Rect '0'? Shouldnt it be NULL? Perhaps both work the same anyways.


//if SDL_KEYDOWN
switch(global.key.keysym.sym)
{
case SDLK_ESCAPE:
k = false;
SDL_Quit();
break;
case SDLK_UP:
ship.y -=1;
SDL_FillRect(screen,0,0);
SDL_BlitSurface(hero,NULL,screen,&ship);
break;
case SDLK_DOWN:
ship.y +=1;
SDL_FillRect(screen,0,0);
SDL_BlitSurface(hero,NULL,screen,&ship);
break;
default:
};

Share this post


Link to post
Share on other sites
Is there somthing wrong with this.
if ( global.type == SDL_KEYDOWN )
{
switch ( global.key.keysym.sym)
{
case SDLK_ESCAPE:
k = false;
SDL_Quit();
break:

case SDLK_UP:
ship.y -=1;
SDL_FillRect(screen,0,0);
SDL_BlitSurface(hero,NULL,screen,&ship);
break:


case SDLK_DOWN:
ship.y +=1;
SDL_FillRect(screen,0,0);
SDL_BlitSurface(hero,NULL,screen,&ship);
break:


case SDLK_LEFT:

ship.x -=1;
SDL_FillRect(screen,0,0);
SDL_BlitSurface(hero,NULL,screen,&ship);
break:


case SDLK_RIGHT:
ship.x +=1;
SDL_FillRect(screen,0,0);
SDL_BlitSurface(hero,NULL,screen,&ship);
break;


case SDLK_SPACE:
SDL_BlitSurface(helaser,NULL,screen,&laser);
break;
default:
};

The reason I ask this is because,I get a lot of missing ; before : errors.
Thanks
MPG

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!