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 objectpShip player;float bulletX = player.posX;float bulletY = player.posY;//Create bullet logicstruct 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.

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.

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.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.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.Render all your objects.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.