I'm currently working on my first space shooter, and I'm in the process of making my ship shoot some bullets/lasers. Unfortunately, I'm having a hard time getting the bullets to fly vertically. I'm a total noob when it comes to this so you might have a hard time understanding my code :/
// Position Bullet Function
projectilex = x + 17;
projectiley = y + -20;
if(keystates[SDLK_SPACE])
{
alive = true;
}
And here's my show function
if(alive)
{
if(frame == 2)
{
frame = 0;
}
apply_surface(projectilex,projectiley,ShootStuff,screen,&lazers[frame]);
frame++;
projectiley + 1;
}
I'm trying to get the bullet to fly vertically... and I have no clue how to do that. I've tried messing with the y coordinate but that makes things worse. The laser/bullet just follows the ship :(
How would I get it to fire at the starting position and keep going in a vertical line without it following the ship?
int main( int argc, char* args[] )
{
Player p;
Timer fps;
bool quit = false;
if( init() == false )
{
return 1;
}
//Load the files
if( load_files() == false )
{
return 1;
}
clip[ 0 ].x = 0;
clip[ 0 ].y = 0;
clip[ 0 ].w = 30;
clip[ 0 ].h = 36;
clip[ 1 ].x = 31;
clip[ 1 ].y = 0;
clip[ 1 ].w = 39;
clip[ 1 ].h = 36;
clip[ 2 ].x = 71;
clip[ 2 ].y = 0;
clip[ 2 ].w = 29;
clip[ 2 ].h = 36;
lazers [ 0 ].x = 0;
lazers [ 0 ].y = 0;
lazers [ 0 ].w = 3;
lazers [ 0 ].h = 9;
lazers [ 1 ].x = 5;
lazers [ 1 ].y = 0;
lazers [ 1 ].w = 3;
lazers [ 1 ].h = 7;
while( quit == false )
{
fps.start();
//While there's an event to handle
while( SDL_PollEvent( &event ) )
{
p.handle_input();
//If a key was pressed
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
}
//Scroll background
bgX -= 8;
//If the background has gone too far
if( bgX <= -GameBackground->w )
{
//Reset the offset
bgX = 0;
}
p.move();
apply_surface( bgX, bgY,GameBackground, screen );
apply_surface( bgX + GameBackground->w, bgY, GameBackground, screen );
apply_surface(0,0, FullHealthBar,screen);
p.shoot();
p.show();
//Apply the message
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
SDL_Flip(GameBackground);
if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
{
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
}
}
//Clean up
clean_up();
return 0;
}