[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Firing bullets



andrew baker wrote:
In this case, we're keeping a list of bullets to avoid
creating/destroying lots of bullets.  Less overhead.  Also, having the
garbage collection kick in during some action would be obnoxious.

The bullet could have a class variable timeToLive that is decremented
during its update().  When timeToLive == 0, then remove the bullet
from the live queue and put it into the waiting queue.

My program creates and destroys Bullet objects constantly rather than using the more efficient method of recycling them, yet it's usually able to run at 30 FPS even with over a hundred bullets onscreen. So, I'm not sure that the recycling method is really necessary, if you find it hard to program. (It's probably not hard; just laziness on my part that I didn't use it.) A couple of other thoughts:


-You might set the bullet's TTL to zero if it hits something, so that the bullet is then deleted during the next regularly scheduled cleanup session. Just be sure that happens before it collides with the same object again!
-Is it worth using ODE physics for bullets? It would probably have been impractical when coupled with the constant creation/destruction method, but maybe if the bullets are recycled you could efficiently do interesting things with ODE like having "bola" bullets that whirl around each other or hit walls. But you'd also have the overhead of constantly being notified about bullet-on-bullet collisions, instead of being able to selectively look only at the kinds of collisions you want to handle.
-To implement BulletML and have fancy kinds of bullets that do things like heat-seeking or direction-changing, you'd basically be adding mini-AI to each bullet. How much overhead is involved in that? Seems like it'd at least require:


class Bullet:
	def __init__(self,**options):
		## Regular options like position and speed, then:
		self.pattern = options.get("pattern","FancyRocketThing")
		self.pattern_timer = 0
	def Go(self):
		self.patern_timer += 1
		if self.pattern == "FancyRocketThing":
			if self.pattern_timer >= 10:
				self.DoSomethingCool()

Kris