Sign in to follow this  
Yasaswi Srikanth

How to move the bullet along with the ship

Recommended Posts

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?


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;
} Edited by Yasaswi Srikanth

Share this post


Link to post
Share on other sites
kauna    2922
You'll need to rethink your bullet logic. The bullet you have now is just a graphic presentation of the object and you need one of them. You'll need to think that graphics and the game are 2 different things.


so you'll need a structure:

class BulletInstance
{
public:
float x,y; //bullet position

BulletInstance(float start_x,float start_y) : x(start_x), y(start_y){};
};

And you'll need a container for the bullets

#include<vector>

std::vector<BulletInstance> BulletContainer;

//Update logic for bullets

void UpdateBullets()
{

for(std::vector<BulletInstance>::Iterator it = BulletContainer.begin(); it != BulletContainer.end(); it++)
{
(*it).y += 1.0f; //move bullets to some direction by some amount
}

}

void DrawBullets()
{

for(std::vector<BulletInstance>::Iterator it = BulletContainer.begin(); it != BulletContainer.end(); it++)
{
DrawSprite((*it).x,(*it).y, ...) //call your DrawSprite using the bullet location data
}
}


case SDLK_SPACE:
{
BulletContainer.pushback(Bullet(start_position_x,start_position_y)); //add a new bullet to the container, at desired position
}
break;


call UpdateBullets() and DrawBullets() in your game loop.

The code doesn't work directly. Also, one thing to take care is deleting bullets which get off screen or hit enemies etc.
Best regards! Edited by kauna

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this