Moving the bullet along with the ship in Galaxians Game


Hello,

I am making a galaxian game in sdl .

(For quick watch of galaxians who doesn't know it
http://www.1980-games.com/us/old-games/java-games/galaxian.php )

I have drawn the aliens ,ship and bullet . The ship is moving is moving left and right . The bullet is moving up for shooting the aliens.

I want to know how to make a new bullet appear after one is gone.
How to move the bullet along with the ship.
Also i didn't provide any kind of animation for the aliens .How to do it.

I didn't use any opengl purely sdl and c++.
If i use any opengl is there any benefit for me or I can go with sdl itself?
Last edited on
Just spawn the bullet at the ship's coordinates (depending on where from the ship you're shooting, you'll need to work that out).

Set the bullets y velocity to a value to that it travels upwards at each draw of the sprite and when its y position exceeds the screen height then redraw the bullet at the ship's location.
I don't know how to redraw the bullet.

Please see here this is my main.cpp that handles the key Events and drawing :


#include "SDLGameEngine/cGraphics.h"
#include "SDLGameEngine/Defines.h"
#include "SDLGameEngine/cInput.h"
#include "cGreenAlien.h"
#include "cShip.h"
#include "SDLGameEngine/Math.h"
#include "cBullet.h"


//collision detection with screen ends

bool shipCanMoveLeft();
bool shipCanMoveRight();

cShip ship;
cBullet bullet;

int main(int argc,char **argv)
{

cGraphics* graphics = new cGraphics(WINDOW_WIDTH,WINDOW_HEIGHT,"Galaxians Clone");

SDL_Surface* bitmap = graphics->LoadImage("Data/alien2.bmp");

cInput input = cInput();

//Green Alien
cGreenAlien greenalien1 = cGreenAlien(graphics,bitmap,100.0f,210.0f,5,9,28,
20,BIGBUG_SPEED);

cGreenAlien greenalien2 = cGreenAlien(graphics,bitmap,140.0f,210.0f,5,9,28,
20,BIGBUG_SPEED);

cGreenAlien greenalien3 = cGreenAlien(graphics,bitmap,180.0f,210.0f,5,9,28,
20,BIGBUG_SPEED);

cGreenAlien greenalien4 = cGreenAlien(graphics,bitmap,220.0f,210.0f,5,9,28,
20,BIGBUG_SPEED);

cGreenAlien greenalien5 = cGreenAlien(graphics,bitmap,260.0f,210.0f,5,9,28,
20,BIGBUG_SPEED);

cGreenAlien greenalien6 = cGreenAlien(graphics,bitmap,300.0f,210.0f,5,9,28,
20,BIGBUG_SPEED);


cGreenAlien greenalien7 = cGreenAlien(graphics,bitmap,340.0f,210.0f,5,9,28,
20,BIGBUG_SPEED);

cGreenAlien greenalien8 = cGreenAlien(graphics,bitmap,380.0f,210.0f,5,9,28,
20,BIGBUG_SPEED);

cGreenAlien greenalien9 = cGreenAlien(graphics,bitmap,420.0f,210.0f,5,9,28,
20,BIGBUG_SPEED);

cGreenAlien greenalien10 = cGreenAlien(graphics,bitmap,460.0f,210.0f,5,9,28,
20,BIGBUG_SPEED);


ship = cShip(graphics,bitmap,SHIP_START_X,SHIP_START_Y,SHIP_IMG_X,SHIP_IMG_Y,SHIP_WIDTH,SHIP_HEIGHT,SHIP_SPEED);

bullet = cBullet(graphics,bitmap,BULLET_START_X,BULLET_START_Y,BULLET_IMG_X,BULLET_IMG_y,
BULLET_WIDTH,BULLET_HEIGHT,1.0f);



bool quit = false;

while(quit == false)
{

if(input.GetEvent())
{

if(input.IsQuit())
{
quit = true;
}

switch(input.GetKeyPressed())
{
case SDLK_ESCAPE:
{
quit = true;
}
break;

case SDLK_SPACE:
{

bullet.pushUp();


}
break;



}
}
if(input.KeyHeld(SDLK_LEFT))
{
if(shipCanMoveLeft())
{
ship.moveLeft();
}
}

if(input.KeyHeld(SDLK_RIGHT))
{
if(shipCanMoveRight())
{
ship.moveRight();
}
}






bullet.MoveVertical();



graphics->ClearScreen(0,0,0);

greenalien1.Draw();

greenalien2.Draw();

greenalien3.Draw();

greenalien4.Draw();

greenalien5.Draw();

greenalien6.Draw();

greenalien7.Draw();

greenalien8.Draw();

greenalien9.Draw();

greenalien10.Draw();

ship.Draw();

bullet.Draw();

graphics->Show();

}

graphics->CloseImage(bitmap);

delete graphics;

return 0;

}



//collision detction of ship with left screen
bool shipCanMoveLeft()
{
SDL_Rect rect = ship.AfterMoveLeft();

// check left extremity

if(rect.x < 0)
{
return false;
}


return true;

}


bool shipCanMoveRight()
{

SDL_Rect rect = ship.AfterMoveRight();


if((rect.x + SHIP_WIDTH) > WINDOW_WIDTH)
{
return false;
}

return true;
}

I think you understand the code if you have any query please ask.
The file cBullet.h would probably be helpful. If I can see what methods the bullet has, I can probably give more of an indication as to how you'd set its position.


cEntity is a class , I have used to store the game entities such as position , width height etc.

//cEntity

#pragma once

#include "cGraphics.h"

class cEntity
{
public:
cEntity(cGraphics* graphics, SDL_Surface* bitmap, float x, float y,
int img_x, int img_y, int width, int height);

cEntity() {}

// This can be overloaded for animated entities
virtual void Draw();

// Return an SDL_Rect storing the cEntity's location and dimensions
SDL_Rect GetRect();

// Accessors/Mutators
float GetX() { return m_X; }
float GetY() { return m_Y; }
int GetWidth() { return m_Width; }
int GetHeight() { return m_Height; }
void SetX(float x) { m_X = x; }
void SetY(float y) { m_Y = y; }

protected:
// For drawing
cGraphics* m_Graphics;
SDL_Surface* m_Bitmap;
int m_ImageX;
int m_ImageY;

// Screen location
float m_X;
float m_Y;

// Dimensions
int m_Width;
int m_Height;
};

// Aaron Cox, 2005


//cBullet.h

//Handle events of the bullet like movements
// and collision detection

#pragma once

#include "SDLGameEngine/cEntity.h"

class cBullet : public cEntity
{

public :

cBullet(cGraphics* graphics,SDL_Surface* bitmap,float x,float y,
int imageX,int imageY,int width,int height,float speed);


cBullet(){}


//Move the bullet

void shoot();
void MoveLeft();
void MoveRight();

void MoveVertical();

//change velocity

void pushUp();


void reDraw();


//collision detection

SDL_Rect AfterMoveUp();


private :

float m_speed;


float m_velocityX;
float m_velocityY;

};


//cBullet.cpp


#include "cBullet.h"

cBullet::cBullet(cGraphics* graphics,SDL_Surface* bitmap,float x,
float y,int imageX,int imageY,int width,int height,
float speed):cEntity(graphics,bitmap,x,y,
imageX,imageY,width,height),m_speed(speed),
m_velocityX(0),
m_velocityY(0)
{}




void cBullet::shoot()
{
m_Y += m_speed;
}

void cBullet::MoveLeft()
{

}

void cBullet::MoveRight()
{

}



void cBullet::MoveVertical()
{

m_Y += m_velocityY;

}

void cBullet::pushUp()
{
if(m_velocityY > - m_speed)
m_velocityY -= m_speed;

}

//collision detection

SDL_Rect cBullet::AfterMoveUp()
{

SDL_Rect r;
r.x = m_X;
r.y = (m_Y - m_speed);
r.w = m_Width;
r.h = m_Height;

return r;
}





Last edited on
Set the bullet position using SetX and SetY to the ship position (plus an offset that you'll have to figure out so the bullet doesn't spawn at the centre or top left of the ship, depending on how the coordinates are set up), which would be retrieved using GetX and GetY of the ship.
Topic archived. No new replies allowed.