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;
}
Answer
Well i was about to write a formerly answer, but seeing your code, it appears that you dont have any Entity system. So it may complicate you, but lets try to solve this.
We need a class to follow what are those bullets, and where are they, dont we?
So lets create something!
class Bullet
{
Vector2 position;
Vector2 speed;
};
If you have entities, probably you already had something like this. So just adapt yourself (:
What must we do? Lets try to describe in plain english what will the code do to shoot bullets.
Every frame, check if the space bar is pressed. If it is pressed, and wasnt before, create a new bullet, going to where the player is faced. If the bullet hits something, delete it, make some nice explode animation and take hp of the object that took the hit. If the bullet just goes after the screen boundaries, just delete it.
Lets try to pass it to C++.
First of all, you have to have some kind of container to storage your bullets. Of course you'll have more than one, so thats why.
Some kind like this on your initialization should do:
std::list bullets;
I dont recommend lists for a game, but they're easier to deal for now.
Now, the code to shoot:
if(event.type == SDL_KEYDOWN)
{
if(event.key == SDLK_SPACE)
{
Bullet* toShoot = new Bullet();
toShoot.speed = player.direction * 5;
toShoot.position = player.position + toShoot.speed;
bullets.add(toShoot);
}
}
Adapt it to your needs and current usages.
The most tricky part here is the player.direction * 5; Well, probably, you have a vector pointing where the player is facing, if you dont have, you SHOULD! When you multiply a vector by a number, you can get a nice speed direction going to that direction! so you'll have a 5px going into that direction. That may not be too right, but should work as a basis for you. Study distance and the pitagoras's formula d = sqrt(x^2 + y^2); If you just want the bullet to go up, change toShoot.speed = player.direction * 5;
with toShoot.speed = Vector(0, 5);
To update your code, try:
for(list::iterator it = bullets.begin(); it != bullets.end();)
{
(*it)->position += (*it)->speed;
if(isOutOfBound((*it)->position))
{
delete (*it);
bullets.erase(it++); //remove from the list and take next
}
else if(collideWithSomething((*it)->position))
{
Entity collisor = getCollisor((*it)->position);
collisor.takeHit();
playSound(Explosion);
makeAnimation(Explosion);
delete (*it);
bullets.erase(it++);
}
else
{
++it; //take next object
}
}
This should do :D
Remembering, the most of these classes you maybe dont have, but you should think about creating something similar.
No comments:
Post a Comment