Tuesday, April 23, 2013

Pygame - Bad Guy Stuff

So I got to thinking about different bad guy types for the little zombie game I'm making. I've got a normal zombie that follows the player around, but that's boring as hell. I was trying to come up with something new. Something easy to do would be to adjust the speed of the zombies. Maybe randomize the speed upon intitialization of the sprite. Still kind of boring though...

What else can we do? Maybe zombies that leave a trail of acid or something? I thought this was an interesting idea, so I went ahead and started on it. Basically all you need is a function to create rectangles for you. Something like this:

def drawTrail(self,screen):
    old_x=self.rect.topleft[0]
    old_y=self.rect.bottom-(self.rect.height*.25)
    if self.trailcount == 0:
      if len(self.trailarray) > 10:
        self.trailarray = self.trailarray[1:]
      self.trailarray.append((old_x,old_y))
    self.trailcount+=1
    if self.trailcount == 15:
      self.trailcount=0
    for i in self.trailarray:
    pygame.draw.rect(screen, (0,255,0), (i[0],i[1],self.rect.width,self.rect.height/4))

It's not perfect, but it works. This function exists inside the badguy sprite class. It's called when the update function is called in the game loop.

What it does is takes the sprite position and puts the values into old_x and old_y. Then it checks a variable called trail count. I used this to tell the function when to append new coordinates to our array list.

The array length is how I tell where to draw the acid. If the array gets over 10 elements, I have it cut off the oldest ones.

Anyway, it works. You might want to change it up a bit and have it use images instead of just a green rectangle.

That's all I've got for now. I'm sure you can think of other ways to use this function. If you have a particle effect library, you might want to use something like this to draw little puffs of smoke as your player sprite moves. I dunno, I thought it was pretty useful.

-Newt

No comments:

Post a Comment