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

Re: [pygame] Firing bullets



Well, I start by making a container, which will be just a simple list

bullets = []

This is assuming the simple case, where all of the bullets are the same type.

Then, create a bunch of bullets

for i in range(0, totalBullets):
   bullets.append(myBulletClass())

When you need a bullet,

firedBullet = bullets.pop()

When that bullet "dies",

bullets.append(firedBullet)

You might want to make the bullets list into a whole class if you
start adding features, but for most cases, this is all the firepower
you'll need.


As for patterns, I would do macros, personally.

Declare different functions that do small patterns

def starBurst(qty):
     angleSlice = 360/qty
     for i in range(0, qty-1):
          current = bullets.pop()
          current.movingX = math.cos(angleSlice*i)*current.maxSpeed
          current.movingY = math.sin(angleSlice*i)*current.maxSpeed

Then, assign and object's fire function to the value of the function

myBadGuy.attack = starBurst

Now, when you call myBadGuy.attack(), your starBurst function will be called.