Jump to content
  • Advertisement
Sign in to follow this  
kingpinzs

How do you make my sprite shoot a bullet?

This topic is 4960 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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.Update() == false)
{
delete Bullets;
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!