Jump to content
  • Advertisement
Sign in to follow this  
destructivArts

Handling Bullets

This topic is 2415 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

I am creating a space invaders style game in Java, and I've got the framework for most of the game set up, but I've never done a game where I have to handle bullets, and I'm not really sure how.

I have the code set up so that I have a bullet class that would contain the position of the bullet, and would handle the image i would use. I also have a method that takes the x and y position and moves them at a constant speed.

I'm just not sure how to handle all the bullets,
Any help would be great,
Thanks,
Peter

Share this post


Link to post
Share on other sites
Advertisement
If by handle "all" the bullets you mean how can I contain all the bullets and move them together. (Forgive me if I misunderstood) the way I would do it is place all the bullets inside an array, and using a for loop (does java have a for each loop ?) to loop through said array and manipulate the bullets through there. You can then use the constant x - y speed and perhaps a time interval so the bullet gets destroyed after it leaves the screen.

Share this post


Link to post
Share on other sites
You got the idea right. The problem I'm seeing is that I would have to know the maximum number of bullets that could be on the screen at any point in time. And actually, by knowing the fire rate and the speed at which they move... which I do... I can figure that out. Thanks for the suggestion and for making me talk out the answer. I never would have gotten it otherwise....

Thanks again,
Peter

Share this post


Link to post
Share on other sites

You got the idea right. The problem I'm seeing is that I would have to know the maximum number of bullets that could be on the screen at any point in time. And actually, by knowing the fire rate and the speed at which they move... which I do... I can figure that out. Thanks for the suggestion and for making me talk out the answer. I never would have gotten it otherwise....

Thanks again,
Peter


You don't "have to" know the max number of bullets, you could use a dynamic container (such as a vector) for them and just add new bullets to it whenever the player fires one, its faster to have a fixed, pre allocated number of bullets though and simply "deactivate" the ones that aren't currently on screen since allocation/reallocation of objects is fairly expensive and can trigger the gc at inappropriate times (For a small game like spaceinvaders however its irrelevant as it will be fast enough either way. you're not allocating thousands of bullets per frame anyway).

Just remember to not allocate one image for each bullet (Having the bullets share one image will make creation of new bullets significantly faster and also cut down memory usage)

Share this post


Link to post
Share on other sites
What do you mean by share an image. I wasn't planning on making 50 different .jpg files of one bullet image.
In my paintComponent method I would just say
"for(i<bullet number){
g.drawImage("bullet_image.jpg",posX,posY,sizeX,sizeY);
}"
Is that what your taking about?

Share this post


Link to post
Share on other sites

What do you mean by share an image. I wasn't planning on making 50 different .jpg files of one bullet image.
In my paintComponent method I would just say
"for(i<bullet number){
g.drawImage("bullet_image.jpg",posX,posY,sizeX,sizeY);
}"
Is that what your taking about?


Graphics.drawImage doesn't take a filename, it takes an Image object (if you got your own drawImage method that takes a filename then you probably have a problem)

The image object you use to draw bullets should be shared between the bullets, Thus you either pass the image object to the bullets constructor or store it outside the bullet. (If you create the image object when you create a bullet or when you draw them then you will end up creating multiple copies of the image and things will slow down significantly) (Alot of beginners get this wrong and either load the image in the bullets constructor or worse, load it in the bullets draw method resulting in the same image being loaded multiple times (either once per bullet(bad) or once each time a bullet is drawn(insanely bad))

Share this post


Link to post
Share on other sites
Ok, I understand.
Thank you.
I've only written two games so far, and neither have used images. I know Java relatively well, but I've never programmed games before so a lot of the game specific mechanics are new to me.

Thanks again,
Peter

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!