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

Drawing Sierpinski's Triangle in Minecraft Using Python

In his keynote at PyCon, Eben Upton, the Executive Director of the Rasberry Pi Foundation, mentioned that not only has Minecraft been ported to the Rasberry Pi, but you can even control it with Python . Since four of my kids are avid Minecraft fans, I figured this might be a good time to teach them to program using Python. So I started yesterday with the goal of programming something cool for Minecraft and then showing it off at the San Francisco Python Meetup in the evening. The first problem that I faced was that I didn't have a Rasberry Pi. You can't hack Minecraft by just installing the Minecraft client. Speaking of which, I didn't have the Minecraft client installed either ;) My kids always play it on their Nexus 7s. I found an open source Minecraft server called Bukkit that "provides the means to extend the popular Minecraft multiplayer server." Then I found a plugin called RaspberryJuice that implements a subset of the Minecraft Pi modding API for B

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

Creating Windows 10 Boot Media for a Lenovo Thinkpad T410 Using Only a Mac and a Linux Machine

TL;DR: Giovanni and I struggled trying to get Windows 10 installed on the Lenovo Thinkpad T410. We struggled a lot trying to create the installation media because we only had a Mac and a Linux machine to work with. Everytime we tried to boot the USB thumb drive, it just showed us a blinking cursor. At the end, we finally realized that Windows 10 wasn't supported on this laptop :-/ I've heard that it took Thomas Edison 100 tries to figure out the right material to use as a lightbulb filament. Well, I'm no Thomas Edison, but I thought it might be noteworthy to document our attempts at getting it to boot off a USB thumb drive: Download the ISO. Attempt 1: Use Etcher. Etcher says it doesn't work for Windows. Attempt 2: Use Boot Camp Assistant. It doesn't have that feature anymore. Attempt 3: Use Disk Utility on a Mac. Erase a USB thumb drive: Format: ExFAT Scheme: GUID Partition Map Mount the ISO. Copy everything from