There may be a much better way to manage artillery.

T

Tobiah

I'm writing a video game with armed space ships.
I decided to make a class to manage all of the bullets
that may be on the screen at a given time:

class Bullets():

def __init__(self):
self.bullets = []

def update(self):
temp = []
for bullet in self.bullets:
bullet.update()
bullet.time_to_live -= 1
if bullet.time_to_live:
temp.append(bullet)
self.bullets = temp

def add(self, new_bullet):
self.bullets.append(new_bullet)

When the main loop calls .update() on the bullets
object, I want it to decrement a counter on each
bullet, and destroy the bullets who's time has come.

I wanted the bullets to be responsible for destroying
themselves, but a little Googling brought me to points
about dangling references and how an object is not allowed
(nor does it seem to have the means) to destroy itself.
That's why I made this wrapper class for all of the bullets.

The idea is to copy bullets with time left into a temp list
and then overwrite the man bullets array with the good bullets.
I believe that the other bullets will be garbage collected.
I could not delete the items in place within the loop, of course.
Was there a better container than a list for my purposes?

Above is what I settled on, but I wonder
whether my approach is a good one.

Thanks very much,

Toby
 
R

Rhodri James

[Snippety snip]
I wanted the bullets to be responsible for destroying
themselves, but a little Googling brought me to points
about dangling references and how an object is not allowed
(nor does it seem to have the means) to destroy itself.
That's why I made this wrapper class for all of the bullets.

An object can, however, erase its representation from the
screen and tell whatever control system is keeping track
of objects to forget about it. Once the last reference to
the object is deleted, the object will be garbage collected.

What framework are you writing your game in? Pygame has
facilities for handling this sort of thing fairly
straightforwardly.
 
D

Dennis Lee Bieber

This also gives you the means to destroy the oldest bullet when creating a
new one, once some arbitrary limit was reached. Think back to how old games
like Raptor worked.
As long as one does not recreate the logic of shots from an old
Coleco tank game.

Projectile position was based upon "distance/time_unit *
elapsed_time_units" -- projected from the <current> angle of the tank
that fired it.

This meant one could shoot around corners! Fire along a wall, then
pivot the tank in place, and the projectile would swing to follow the
muzzle angle...
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
T

Tobiah

As long as one does not recreate the logic of shots from an old
Coleco tank game.

Projectile position was based upon "distance/time_unit *
elapsed_time_units" -- projected from the <current> angle of the tank that
fired it.

This meant one could shoot around corners! Fire along a wall, then
pivot the tank in place, and the projectile would swing to follow the
muzzle angle...

Are you sure you are not referring to the tank game that came with
the Atari 2600? Some of the games behaved as you described, but others
let the bullet go it's own direction.

I remember that there were only 16 possible tank rotations, making it
difficult to hit the opponent from a long distance. I have the luxury
now, of calculating infinite rotational positions for my space ships.
I convert the points in the ship shape to polar, adding some amount
to the angle, then converting back to cartesian coordinates for plotting.
 
T

Tobiah

[Snippety snip]
I wanted the bullets to be responsible for destroying themselves, but a
little Googling brought me to points about dangling references and how
an object is not allowed (nor does it seem to have the means) to destroy
itself. That's why I made this wrapper class for all of the bullets.

An object can, however, erase its representation from the screen and tell
whatever control system is keeping track of objects to forget about it.
Once the last reference to the object is deleted, the object will be
garbage collected.

What framework are you writing your game in? Pygame has facilities for
handling this sort of thing fairly straightforwardly.

Actually I am using Pygame, although I'm quite new to it. Which
module has the facilities that you describe?

Thanks,

Toby
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top