Skip to main content

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!"

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.
Anonymous 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!
jjinux said…
> I used a similar approach on my game, Galvinball.

cool :)
jjinux said…
> 3am watching "survivorman" eating frozen pizza.. that's how programming contests are won!

w00t!
jjinux said…
The source code is now on GitHub: https://github.com/jjinux/hellacopy

Popular posts from this blog

Ubuntu 20.04 on a 2015 15" MacBook Pro

I decided to give Ubuntu 20.04 a try on my 2015 15" MacBook Pro. I didn't actually install it; I just live booted from a USB thumb drive which was enough to try out everything I wanted. In summary, it's not perfect, and issues with my camera would prevent me from switching, but given the right hardware, I think it's a really viable option. The first thing I wanted to try was what would happen if I plugged in a non-HiDPI screen given that my laptop has a HiDPI screen. Without sub-pixel scaling, whatever scale rate I picked for one screen would apply to the other. However, once I turned on sub-pixel scaling, I was able to pick different scale rates for the internal and external displays. That looked ok. I tried plugging in and unplugging multiple times, and it didn't crash. I doubt it'd work with my Thunderbolt display at work, but it worked fine for my HDMI displays at home. I even plugged it into my TV, and it stuck to the 100% scaling I picked for the othe

ERNOS: Erlang Networked Operating System

I've been reading Dreaming in Code lately, and I really like it. If you're not a dreamer, you may safely skip the rest of this post ;) In Chapter 10, "Engineers and Artists", Alan Kay, John Backus, and Jaron Lanier really got me thinking. I've also been thinking a lot about Minix 3 , Erlang , and the original Lisp machine . The ideas are beginning to synthesize into something cohesive--more than just the sum of their parts. Now, I'm sure that many of these ideas have already been envisioned within Tunes.org , LLVM , Microsoft's Singularity project, or in some other place that I haven't managed to discover or fully read, but I'm going to blog them anyway. Rather than wax philosophical, let me just dump out some ideas: Start with Minix 3. It's a new microkernel, and it's meant for real use, unlike the original Minix. "This new OS is extremely small, with the part that runs in kernel mode under 4000 lines of executable code.&quo

Haskell or Erlang?

I've coded in both Erlang and Haskell. Erlang is practical, efficient, and useful. It's got a wonderful niche in the distributed world, and it has some real success stories such as CouchDB and jabber.org. Haskell is elegant and beautiful. It's been successful in various programming language competitions. I have some experience in both, but I'm thinking it's time to really commit to learning one of them on a professional level. They both have good books out now, and it's probably time I read one of those books cover to cover. My question is which? Back in 2000, Perl had established a real niche for systems administration, CGI, and text processing. The syntax wasn't exactly beautiful (unless you're into that sort of thing), but it was popular and mature. Python hadn't really become popular, nor did it really have a strong niche (at least as far as I could see). I went with Python because of its elegance, but since then, I've coded both p