Handling Bullets

Started by
5 comments, last by destructivArts 12 years, 1 month ago
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
-------------------------------------
"Other than that, I have no opinion."
My Blog - Check it Out
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.
Game Programming.. Creativity and Logic in one massive bundle. Gotta love it.
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
-------------------------------------
"Other than that, I have no opinion."
My Blog - Check it Out

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)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
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?
-------------------------------------
"Other than that, I have no opinion."
My Blog - Check it Out

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))
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
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
-------------------------------------
"Other than that, I have no opinion."
My Blog - Check it Out

This topic is closed to new replies.

Advertisement