Python Saves the Day (again)

This happens often enough that I feel I should hang the words above my doorway: “When all else fails, use Python instead.” Optional addition: “Like you should have in the first place.”

Around New Year’s, I wrote a little implementation of Conway’s Game of Life in jQuery. Why jQuery? For funsies. Unfortunately, after a few hours on a plane, the funsies went to a picnic, leaving me with manifest failure for poor decision-making skills. It’s not that javascript is incapable of building games; it’s just that it isn’t particularly well-suited to the task. Kind of like the way a spatula might not be the best tool for herding wild boar.

After learning that educational lesson, shaking my fists impotently, and returning to my lair to nurse my wounds, I decided it was time to approach the problem again. I won’t deny I was somewhat inspired by the recently-released online textbook Making Games with Python & Pygame. I had been eyeing the pygame library for a while (Python being one my favorite scripting languages), and this new resource offered a great opportunity to dive right in. Obviously, the imminent arrival of this term’s final exams played no part in the timing of this project.

With that said, I’m not sure I would recommend the textbook as a great resource. It’s a handy thing for the novice game programmer, but it falls short as an instructional text. The book’s structure is partially to blame for this: each chapter lists the source code of a particular game, and then proceeds to give a line-by-line (or chunk-by-chunk) analysis of what the program does. This is fine at first; it lets you see the pygame functions & syntax in action. But at some point, you — the reader — are left to read between the lines: why is this function structured this way? Why does this need to be a global variable? Is this use of fonts really the best approach? At some point, a good teacher needs to step back from the gritty details to talk about big picture concepts; s/he needs to the address the “Why” in addition to the “How.” Unfortunately, that doesn’t happen here.

I could also gripe that the author has no voice, and that some of his coding habits/choices raise my eyebrows up to my hairline. But then I’d be nitpicking. Heaven knows I wouldn’t want to do that.

But anyway, after working through a couple chapters, I opened up IDLE (the development environment for Python) to cobble together a few pygame functions. After a couple days’ worth of lunchtimes, I had a fully-functional version of Conway’s Game of Life, with some rudimentary keyboard controls thrown in for good measure.

life_py

It’s no work of art, but holy cow and hot damn! Not only was that was way faster than I imagined, but development was super-smooth: I was able to fix any error that popped up in a matter of moments. That’s distinctly unlike, say, C++ or javascript errors, which have been a little more time-consuming in my experience.

I’m not done with it: I’d like to change the keyboard controls into visual, clickable buttons (and add a few I missed). Also, letting the user modify the game clock is a mistake. I wanted to let you control the frequency at which each generation is run, but I shouldn’t have conflated that idea with modifying the frame rate (the number of frames displayed per second ). Because consequently, that determines how fast the game (and therefore, you) can perform some action. To draw it out, if you reduce the FPS to 1, not only do you see one generation per second, but there will be a significant lag between when you press a button and when the program reacts.

But these are good lessons. Frankly, that was the point of starting with Conway’s Game of Life: simple internal mechanics to allow me to focus on… well, everything else, really. Speaking of which, there’s also the question of distribution. I’d like to put this little program of mine online. Unfortunately, in order to run it, you would have to go through the same steps I did: install Python, install Pygame, and then use IDLE to run the source code. That seems a bit silly and excessive, so I’m going to explore to see if there are other options. Maybe it can be compiled into an app or an .exe, the kind of thing that will just run once on your computer. TBD.

In any case, I feel revitalized and rejuvenated. Solving this little problem has given me hope for all those other things I keep meaning to do. Deus Ex Python.

Advertisements
1 comment
  1. Karen said:

    revitalized and rejuvenated: sounds like spring to me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: