Sign in to follow this  
kingpinzs

How do you make my sprite shoot a bullet?

Recommended Posts

(C++, Windows, DirectDraw) I have a main character that I want to be able to shoot a bullet.I know how to draw now I need to figure out the process of having it shoot a sprit from that image. And how do I have my character draw a gun?

Share this post


Link to post
Share on other sites
I would make a class for bullets, and then have a vector of "active" bullets. To fire a bullet just add it to the list, and every frame go through the list updating each bullets position, and checking if it hits anything. Maybe something like this:


class Bullet
{
Sprite * theBulletSprite;
Vector2d myPosition;
Vector2d myDirection;
float mySpeed;

Bullet(Sprite * bulletSprite, Vector2d source, Vector2d direction, float speed)
{
theBulletSprite = bulletSprite;
myPosition = source;
myDirection = direction;
mySpeed = speed;
}

bool Update(float timeElapsed)
{
myPosition += (myDirection * speed * timeElapsed);

if (colliding_with_something)
{
// deal damage to the target
return true;
}

return false;
}
}

std::vector < Bullet * > Bullets;

void YourMainUpdateFunction()
{
....
if (shoot_gun_key_pressed)
{
Bullet * bullet = new Bullet(bulletSprite, mainPlayerPosition, mainPlayerFacing, 10.0f);
Bullets.push_back(bullet);
}
....
for (int i = 0; i < Bullets.size(); ;)
{
if (Bullets[i].Update() == false)
{
delete Bullets[i];
Bullets.erase(Bullets.begin() + i);
}
else
{
i++;
}
}
....
}



Alan

Share this post


Link to post
Share on other sites
Is there a way to do that with out using a class?

I have been trying to lear classes but I can not get a handel on them.

So I need to stay with functions for now.

Share this post


Link to post
Share on other sites
Instead of a class, you could just use a struct to hold all of the bullet data, and then use some "HandleBullet" function to loop through them all and ensure they're moving properly.

In case you're not familiar with vectors, have a look at this site:

www.cppreference.com/cppvector.html


Ryan

Share this post


Link to post
Share on other sites
You can do it without classes, but OOP makes it simpler to manage. If you are having trouble with classes do a bit of JAVA, then you catch on pretty quick.

The easiest way to create a bullet without a class is to use a struct and array:

[SOURCE]
typdef struct tbullet
{
float xpos, xspeed;
float ypos, yspeed;
bool active;
SpriteType sprite;
}
tbullet;

tbullet bullets[ MAX_NUMBER_OF_BULLETS ];

Loop through them when you need to update:

for( i = 0; i < MAX_NUMBER_OF_BULLETS; ++i )
{
xpos += xspeed;
ypos += yspeed;

if( some condition )
{
active = false;
}
}

[/SOURCE]


When you want to fire a bullet just set it at the same coordinates as the player.

Thats the general idea. BTW, you can't be programming C++ if you not using classes, else it's just plain C.

Share this post


Link to post
Share on other sites
How do you clone an image and have it display the same image every time a key is pressed?

How do you call a mouse click in the middle of a function?

[Edited by - kingpinzs on November 14, 2004 7:26:10 PM]

Share this post


Link to post
Share on other sites
If you use a linear data structure (vector, whatever) of bullet objects (that you handcraft) each bullet object would presumably have its own DrawSelf method, all of which could point to the same bullet graphic(s).

That's the way I'd do it.

Share this post


Link to post
Share on other sites
first of all,store all of your bitmaps in a array in acsending sequence.also set a variable which control how long each bitmap will be displayed.

in your main game loop, you will check if the fire key is pressed. if it is then do this,and starting from the beginning of the array,display the bitmap and increment the varible.if the varible is bigger enough then reset it,and increment the array index so that it will display the next bitmap in the array.do this for each frame until fire key is released.

Share this post


Link to post
Share on other sites
How can I get a sprite to appear on the screen when I press a button and then keep moving and bouncing around then press the key again and do the same thing?

Share this post


Link to post
Share on other sites
How do you add sprite to back buffer and flip it to the main buffer

This is what I have

LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
…..
…..
……
else if (wParam==VK_SPACE)
{

Bsprite();

return(0);
}
…….
……
…..
}
/////////////////
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd)
{
…..
…..
…..
ShowWindow(hwnd, nShowCmd);
Game_Int();
Splash();
//message pump
for(;;)
{
…..
……
…..
}
Game_Main();
}

//////////////////////
int Game_Int()
{
…..
…..
…..
g_pddbullet = DDLoadBitmap(g_pdd,"Bullit.bmp",0,0);
DDSetColorKey( g_pddbullet, RGB(0, 0, 255));
……..
……..
}
/////////////////////
int Game_Main()
{

r.left = 0;
r.top = 0;
r.right = 800;
r.bottom = 600;
lpBackBuffer->BltFast(0, 0, g_pddground, &r,
DDBLTFAST_NOCOLORKEY | DDBLTFAST_WAIT);
//the bug
r.left = cl;
r.top = ct;
r.right = cr;
r.bottom = cb;
lpBackBuffer->BltFast(x, y, g_pddbug, &r,
DDBLTFAST_SRCCOLORKEY | DDBLTFAST_WAIT);

////////Spider movment
r.left = 0;
r.top = 0;
r.right = 64;
r.bottom = 64;
lpBackBuffer->BltFast(sx, sy, g_pddspider, &r,
DDBLTFAST_SRCCOLORKEY | DDBLTFAST_WAIT);

r.left = 65;
r.top = 0;
r.right = 128;
r.bottom = 64;
lpBackBuffer->BltFast(500,500,g_pddhouse,&r,
DDBLTFAST_SRCCOLORKEY | DDBLTFAST_WAIT);

g_pddsprimary->Flip(NULL, DDFLIP_WAIT);
return(1);
}
////////////////////////
int Bsprite()
{
bx =x;
by =y;

for( i = 0; i < 50; ++i )
{
float xspeed = 1;
float yspeed = 1;

bx =x + xspeed;
by =y+ yspeed;
}
r.left =0;
r.top =0;
r.right =64;
r.bottom=64;
lpBackBuffer->BltFast(bx,by,g_pddbullet, &r,
DDBLTFAST_SRCCOLORKEY | DDBLTFAST_WAIT);
return (1);
}


But it will not put the bullet to the primary surface when I hit the space bar. Why won’t it work?

Share this post


Link to post
Share on other sites
What you effectively have is a particle system with:


  • Position
  • Velocity
  • Colour
  • Life (time before particle dies)


A bullet is just a special kind of particle. If you want other kinds of particles, (eg. snow, smoke, fire), consider writing a generic class and deriving from it, for each type of particle you need to implement.

Have a look on gamedev for Particle System should be one, else try google.

Share this post


Link to post
Share on other sites
You really could do with getting your head around classes and objects before attempting something like this. It is far easier to understand than masses of variables, arrays and functions.

Share this post


Link to post
Share on other sites
How do you clone an image and have it display the same image every time a key is pressed?


I would suggest learning classes and the like before you worry about how to do this. Once you understand the concept of it, and have built a bullet class (or just a bullet manager), you'll want to define an array of active bullets. Each time a certain key is pressed, you'll just 'activate' another bullet and it will take off from there. Of course, it'd be a good idea to first set the bullet position to the chars (as well as the direction), but that's a bit minor

How do you call a mouse click in the middle of a function?

Okay, you've lost me on this one. What you do is setup a function to handle mouse-clicks...each click of the mouse will cause the program to run through this particular function. You could use it for the bullets just like the key stroke listed above.

Share this post


Link to post
Share on other sites
So this is what I have

struct TSprite
{
int top,left; //Top left of section to make sprite from
int right, bottom; //Bottom Right of sprite section
int Screenx; // were to put the sprite on the screen
int Screeny;
}Sprite;
//////////////////////////////////
TSprite Bullet[5];
/////////////////////////////////
else if (wParam==VK_SPACE)
{
screenx= x;
screeny = y;
bullettest =1;

for (int Loop = 1; Loop<(rate);Loop++)
{ Bullet[Loop].Screenx = Bullet[Loop].Screenx + 50;

Loop = rate;
}
//////////////////////////////////////////////
for ( int Loop = 1; Loop< rate;Loop++)
{
Bullet[Loop].top = 0;
Bullet[Loop].bottom =64;
Bullet[Loop].right =64;
Bullet[Loop].left =0;
Bullet[Loop].Screenx= screenx;
Bullet[Loop].Screeny= screeny;
}

/////////////////////////////////
int test()
{
if(screenx< 0 || screenx> mapX ) xspeed *= -1;
if(screeny < 0 || screeny > mapY ) yspeed *= -1;
for (int Loop = 1; Loop < rate; Loop++)
{
screenx += xspeed;
screeny += yspeed;
bltsprite(Bullet[Loop]);
}
}
////////////////////////////////////////
int Game_Main()
{
if (g_pddsprimary == NULL)
{

return(0);
}
//collistion();

if ( bullettest == 1)
{
backgournd();
test();
players();
foreground();
Flip();
}
else if (bullettest == 0)
{
backgournd();
players();
foreground();
Flip();
}
return(1);
}


This still does not work

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