How do you make my sprite shoot a bullet?
(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?
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:
Alan
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
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.
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.
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
In case you're not familiar with vectors, have a look at this site:
www.cppreference.com/cppvector.html
Ryan
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:
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.
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.
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]
How do you call a mouse click in the middle of a function?
[Edited by - kingpinzs on November 14, 2004 7:26:10 PM]
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.
That's the way I'd do it.
Heres a the code to a Space invaders game in a JAVA Applet that does exactly what you want.
http://www.iol.ie/~kellyswd/Patrick/game.zip
http://www.iol.ie/~kellyswd/Patrick/game.zip
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement