In An Illustrated Guide to Shell Magic: Typing Less & Doing More, I talked about writing shell filters in a handful of languages. Let's revisit a version of one of those,

#!/usr/bin/env python
# encoding: utf-8
import sys
import re

line = sys.stdin.readline()
while line:
    stars = re.sub('\S', '★', line)
    line = sys.stdin.readline()

This is some fairly simple Python. It takes some text from standard input one line at a time, uses a regular expression to convert any non-space character to a ★, and writes each line back to standard output.

In practice, that looks like this:

Usually, we write code like this to search, transform, store, or otherwise munge data.  But what if, to start, we just want to do something more visually interesting with the output than view some characters?

Pygame is a set of Python libraries for making it easy to write games. In turn, it wraps up SDL, a popular cross-platform graphics and sound library.

You probably won't be writing the next blockbuster first-person shooter in Pygame, but it's a great way to experiment with simple games that will run on all sorts of computers, including the Pi. For some good examples, you can have a look in the python_games folder in your home directory on the Pi. Here's, which you can run with python

If you cd ~/python_games and run nano, you can see it's about 400 lines of Python. This could be a little intimidating if you don't know Python - let's start with something a little simpler.

This guide was first published on Mar 20, 2015. It was last updated on Mar 20, 2015.

This page (Shell Filters) was last updated on Mar 09, 2015.

Text editor powered by tinymce.