Tetris for the command line with Python Curses

Last updated: Dec. 14, 2007, 12:24 a.m.

Reading through this month's Linux Format, I came across a tutorial on how to write curses applications in Python. For those of you un-initiated in programming speak, curses is a library designed to allow programmers to manipulate the command line interface. Unlike a simple line buffered text interface, curses allows you to print at a given set of screen co-ordinates in rows and columns in the terminal window, allowing you to do much more sophisticated layout. It also handles input and can even provide multiple "windows" within the text only display. Take a look at the Wikipedia article for more sensible info.

Curses Tetris:
A game of Tetris implemented in text only "curses" terminal interface.

Later that same day, I found myself playing Gnome Tetris, or whatever it's called, but it kept getting bogged down when there were too many pieces visible, some sort of bug I guess. Anyway, I came up with the idea of writing my own version of tetris in Python using the curses interface I had read about to allow you to play the game in the terminal window.

So, a couple of hours of fiddling later and here it is, a complete, playable game of tetris using curses, to run it you'll need Python installed and the curses module, I suspect that's pre installed on a lot of modern systems, and then just run the python script using the command

$ python tetris.py

The controls may take you a little by surprise, they are:
W: rotate piece
A: shift piece left
S: move piece down faster
D: shift piece right

And you can hit q at any time to quit the game.

Please have a look at the code first as good practice, never run code from the internet unless you trust the source. As usual, the code is GPL, so do what you want with it, and no warranty.