How to code bullets in side scroller

Started by
3 comments, last by Neometron 11 years, 9 months ago
So i have an R-type like game where the player controls the ship with WASD and shoots with spacebar. I am kind of stuck on how to implement the bullets though. I want the bullet to have the starting point of the current position of the player spaceship. How would the bullet objects work? If spacebar is pressed, would it create a bullet object and be destroyed if it came in contact with an alien ship or is off the screen? How would each bullet object differ from the previous bullets?
[source lang="cpp"]struct pShip
{
float posX;
float posY;

pShip(float x=0,float y=400)
{
posX = x;
posY = y;
}

float playerMovementForward(void)
{
posX += .3;
return posX;
};

float playerMovementBack(void)
{
posX -=.3;
return posX;
};

float playerMovementUp(void)
{
posY -=.3;
return posY;
};

float playerMovementDown(void)
{
posY +=.3;
return posY;
};

};
//Create PlayerShip object
pShip player;

float bulletX = player.posX;
float bulletY = player.posY;
//Create bullet logic
struct Bullets
{
float posX;
float posY;

Bullets(float x = bulletX, float y = bulletY)
{
posX = x;
posY = y;
};
//Fire, return x position of bullet

};


//////////////////////////////////////////////////////////////////////////////////////////////////
// GAME LOGIC //


//Player Controls
if (Key_Down(DIK_D) && player.posX < SCREENW - 100) {
Sprite_Draw_Frame(playerShip,player.playerMovementForward(),player.posY,1,100,100,2);
}

if (Key_Down(DIK_A) && player.posX > 5) {
Sprite_Draw_Frame(playerShip,player.playerMovementBack(),player.posY,frame,100,100,2);
}

if (Key_Down(DIK_W) && player.posY > 10 ){
Sprite_Draw_Frame(playerShip,player.posX,player.playerMovementUp(),frame,100,100,2);
}

if (Key_Down(DIK_S) && player.posY < SCREENH - 50){
Sprite_Draw_Frame(playerShip,player.posX,player.playerMovementDown(),frame,100,100,2);
}
//Firing bullets
Bullets bullet1;

if (Key_Down(DIK_SPACE)){
Sprite_Draw_Frame(bullet,bullet1.posX,bullet1.posY,frame,100,100,2);
}
//////////////////////////////////////////////////////////////////////////////////////////////////

[/source]
I only posted the relevant structures and functions, but when i press space bar, the bullet just appears where the ORIGINAL starting of the spaceship was (before moving the player ship), and it does not move. I've been messing around with it all day but can not find the solution. Thanks in advance.
Advertisement
Try implementing a bullet movement function and call it per-frame after user input and before collision detection such as:

[source lang="cpp"]float bulletMovementFoward(void)
{
posX += .5; //Something faster than ship movement
return posX;
}
[/source]

However I suggest looking into frame independent movement to give object a smoother feel to it.
Yes I have already tried that but ended up removing it as it did not work. I am now able to have the bullet sprite start where the ship is. The only problem is that the bullet sprite does not accelerate forward (most likely logic errors with bullet position being initialized to player ship position). It just appears in front of the ship. I have updated it to this.
[source lang="cpp"]if (Key_Down(DIK_SPACE)){
bullet1.posX= player.posX;
bullet1.posY= player.posY; //initialize bullet position to the current position of the ship

Sprite_Draw_Frame(playerShip,bullet1.bulletMovementFoward(),bullet1.posY,frame,100,100,2);
}[/source]

Thank you for your input Neo, only problem i have now is making the bullet sprite accelerate forward.
It's because bullet1 is declared locally, resulting in it being in the same spot every time. (Well, that's what it looks like anyway)

I would create a list of bullets and add them as they're fired, update them every frame, and destroy them when no longer needed(ie collision)
[font="Times New Roman"][size="3"][color="#000000"]I gave a short answer when you were looking for an explanation.

How do bullet objects work? Bullet, player, and enemy objects should all have the same framework of functionality. Have a current state(s), process input/AI based on state and create new states, move based on state, check collisions and change state if collided, render, and repeat.[/font]
[font="Times New Roman"][size="3"][color="#000000"]Some example of states an object should have is wait, move, shoot, hit, and explode. For instance, ‘D’ input from player creates a move_forward state. A bullet should have a reoccurring state of move_forward. An enemy gets its state from some kind of AI.

Then after all the initial states for all objects have been created, then move objects based on the state they have.[/font]
[font="Times New Roman"][size="3"][color="#000000"]Cycle through all the objects and check if they collided with each other or collided with the out of bounds for the screen. Objects could have bounding box or circle. The screen should have a bounding box larger than the screen itself. The type of collision should have a specific state for the object.[/font]
[font="Times New Roman"][size="3"][color="#000000"]Render all your objects.[/font]
[font="Times New Roman"][size="3"][color="#000000"]Repeat cycle.

“If spacebar is pressed, would it create a bullet object and be destroyed if it came in contact with an alien ship or is off the screen?” Pressing the spacebar should create a shoot state for the player. During the move phase is where I would suggest creating a new object for bullets.

How would each bullet object differ from the previous bullets? Nickem explained it pretty well however I would add that you should give sometime between each fire state say half second or several frames of animation. [/font]

This topic is closed to new replies.

Advertisement