Square Shooter on the desktop

2012-07-16

Square Shooter was my first HTML5 game, and while that has allowed me to show it off easily (and brag about it when HTML5 was still fairly exotic), I always wanted a desktop version as well. And I made one! The most intense coding marathon I've ever done (500 lines of code in two days!) resulted in a game that's easier to control and understand, and looks better to boot.

square shooter desktop

Let me tell you how I did it.

This wasn't my first go at a desktop version. A previous attempt using C++ and FreeGLUT had been instructive and fun but I was never happy with the result. SDL would have been a better fit, but its C API is positively scary. So what to do?

Luckily, there are wrappers around SDL that not only make it much more palatable but also hook it up to a dynamic language, which is more to my taste. The two best known options are Pygame and LÖVE, and the choice between them is far from automatic.

Love2D has a number of advantages at first sight:

Pygame, too, has a few things going for it:

The last point is what made the difference for me; it's just so much easier to not have to learn a new language. There's also something to be said about using a non-minimalist language: while Lua's feature set and standard library is comparable to Javascript's, Python's motto is "batteries included", and it lives up to it. My code is consistently 33% shorter in Python versus JS (and I have a lot more experience with the latter). Simply being able to say random.choice(["eeny", "meeny", "miny", "moe"]) makes programming a lot more fun.

I also took the opportunity to overhaul the game logic, which featured bad physics, as well as some dependencies on the fixed 20 FPS speed of the original. This of course required significant rebalancing. If you never had to do that, beware of details: it's amazing what difference can result from a 20% faster bullet.

Speaking of speed, Python has a reputation for slowness, but in my experience it was more than fast enough. Just with the window background on, the game spins at an amazing 195 FPS; by the time I finished, it was down to 170-180, depending on system load. (The final version is capped at 60, to avoid wasting CPU.) Even text rendering has negligible effect on performance, unlike in HTML5 where it's more than noticeable.

On the minus side, putting text exactly where you want it is awkward. Then again, it was this limitation that inspired the new layout of the game window. But the real problem was figuring it out: the Pygame documentation really, really needs to make text handling more obvious. At first, I couldn't seem to find any text support at all! Turns out, the pygame.Font class has a render() method. But how to right-align text, or whatever? Turns out, Surface objects have a get_rect() method for that... just go to pygame.Rect to figure out the accepted parameters.

If you have Python and PyGame or don't mind installing them, you can download the new version of Square Shooter on the game's homepage.