Tuesday, September 11, 2007

Python: PyWeek


I just finished PyWeek! (Here's the code.) It's a contest where you have one week to write a video game using PyGame. We also used PGU, which is a pretty helpful library for writing video games. This time, my buddy Adam Ulvi participated with me. We took two days off from work and wrote about 800 lines of code. I'm proud to say we created what I think is a pretty impressive and fun game.

I leaned pretty heavily on object-oriented programming this time. I know a lot of people like to talk smack about OOP, but I often find it helpful. The player, the enemies, and even the artillery all subclass a class called SuperSprite that takes care of things like integrating per-frame movement, per-frame animation, collisions, etc. OOP lets me say "these guys should all act exactly the same except in these slightly different ways." Often times, the subclass is no more than a few class-level constants, like a different image. Sometimes they behave slightly differently, which is achieved by overriding a single method.

Since I'm also a fan of functional programming, I also relied heavily on closures for doing animation. Each subclass of SuperSprite has an animator_func function. It's a pointer to whatever function I happen to be using for animation at the time. Hence, when you destroy an enemy, I set that enemy's animator_func to a closure that animates the enemy's explosion:
    def create_destroyed_animator(self):

"""This is the animator for when the enemy is damaged.

And by "damaged" I mean exploding. This function returns a closure.

"""

def f():
f.count += 1
n = f.count / 3
if n <= 2:
self.image = self.g.images['explosion-%s' % n][0]
else:
self.after_destroyed_func()

def task():
self.invincible = True

play_wav('explosion.wav')
f.count = 0
self.g.post_frame_tasks.append(task)
return f
The closure plays number games with the frame count to switch images every few frames and then finally go away. This made it really easy to animate explosions using three successive images. We used this same trick to animate a bunch of different things.

Another neat thing is that the turrets always turn to face you when they shoot. My trigonometry is pretty rusty and I was pretty tired when I came up with that code, so I'm just glad that I was able to remember sohcahtoa.

All in all, I'm pretty thrilled about what we came up with. There are many programmers in the world who are far more talented than I am, but it's nice to actually create something in so short a time span and say, "Hey, look what I can do!"

5 comments:

Rob De Almeida said...

Cool! I haven't tried your game yet since I'm moving down the list on alphabetical order, but it looks promising!

I used a similar approach on my game, Galvinball. The idea is that you can create arbitrary rules like "ALL DOGS CLOSE TO PLAYER TELEPORT" at any time during the game. The rules follow a simple DSL, and when processed they return closures that act on the corresponding objects. The closures can be inserted into two different queues, one that is called at the beginning of the frame, and one after the frame is finished.

adam u. said...

Rob, I'll have to check your game out, the idea of totally arbitrary rules sounds like a neat hack!

I'm still pretty green when it comes to OO and Python so for me the best part was working with JJ. You know you're dealing with a perfectionist when the guy is still refactoring out two-liners on the 14th straight hour of hacking ;-)

3am watching "survivorman" eating frozen pizza.. that's how programming contests are won!

Shannon -jj Behrens said...

> I used a similar approach on my game, Galvinball.

cool :)

Shannon -jj Behrens said...

> 3am watching "survivorman" eating frozen pizza.. that's how programming contests are won!

w00t!

Shannon -jj Behrens said...

The source code is now on GitHub: https://github.com/jjinux/hellacopy