Space Invaders: the First Day

The next round at the Recurse Center was pairing programming. One of the tasks they are suggesting to implement is Space Invaders game. I haven’t done much GUI development in recent years, so I felt like it’s a nice opportunity to try something new. Once you submit the initial implementation, you pick up the next simple task to implement during pairing programming.

After a quick Googling, I landed on a Tcl page which explains how to build native GUI applications in Python. This sounded reasonable, and I was especially convinced since it’s an integral part of Python.

To my surprise, a standard Python which is shipped in macOS doesn’t play nicely with GUI. I had to install a new version with Tcl support. I’m sticking with 3.7.5 for no particular reason, just something I’ve been using for some time at my work.

$ pyenv uninstall 3.7.5
$ brew install tcl-tk
$ env \
  PATH="$(brew --prefix tcl-tk)/bin:$PATH" \
  LDFLAGS="-L$(brew --prefix tcl-tk)/lib" \
  CPPFLAGS="-I$(brew --prefix tcl-tk)/include" \
  PKG_CONFIG_PATH="$(brew --prefix tcl-tk)/lib/pkgconfig" \
  CFLAGS="-I$(brew --prefix tcl-tk)/include" \
  PYTHON_CONFIGURE_OPTS="--with-tcltk-includes='-I$(brew --prefix tcl-tk)/include' --with-tcltk-libs='-L$(brew --prefix tcl-tk)/lib -ltcl8.6 -ltk8.6'" \
  pyenv install 3.7.5

Anyway, it turned out to be too much of a boilerplate to create something simplistic like a game in Tcl, so I decided to explore other options.

I ended up at Pygame website, a mature framework to create Python games. Kudos to the maintainers.

I think about a game engine and objects as pure functions that receive signals and produce outputs. A close analogy is Erlang signals. Simply put, there’re actors and messages they are passing between each other. For example, World is going to listen to “tick” events, and a player could launch a rocket, etc.

With this idea in mind, coding was fairly straighforward. I just needed to arrange objects on the screen and implement their reactions.

Here’s a Day 1 result:

Space Invaders -- Day 1 result

Space Invaders using Pygame

Source code:

← Setting up Minikube on Thinkpad X220

Space Invaders: Part 2 →