A Tetris clone in Python / wxPython
May 31st, 2008 at 12:18 pmAs a part of my quest to learn Python, I decided to fulfill an old dream(*) and implement a Tetris clone, using the excellent wxPython toolkit. While rudimentary, it is a complete Tetris game you can play from beginning to end, with a sophisticated scoring system and a high-scores table.
Here’s a screenshot (you can see the full version by clicking on it):
And this is the About box, with some more information:
The game is downloadable from here. It’s a source-only distribution, so you’ll need Python 2.5 and wxPython 2.8 installed to run it.
Update 06.06.2008: I’ve uploaded a Windows executable (4.8 MB) of the game. It was created with py2exe and doesn’t require installing anything in order to run.
The code is about 1 KLOC, divided into several files (Python’s zip-import ability is used, so most of the code is in the lib.zip file). It is my first non-trivial Python program, so the code is probably far from being beautiful idiomatic Python, although I’m sure it’s not bad.
(*) Back when I started programming, I really wanted to write a Tetris. At that time (1998) I wrote graphical programs using Borland’s conio library, drawing on the DOS screen. After implementing a Snake clone, I felt ready for Tetris but got quickly stuck. I recall having a problem with the rotation code – I couldn’t figure out how to rotate the blocks.
Later as I gained programming experience I realized that it’s simple, but I didn’t have the opportunity to get back to Tetris. Indeed, as it turned out, the rotation code is one of the simplest parts of the game. Most of my time was spent reading the wxPython manuals and getting all the GUI widgets to line-up nicely
Related posts:

May 31st, 2008 at 12:56
Very nice (just the screenshots, I don’t think I’ll download python + wxPython just to play). I think we all played around with Snake at the time
My experience was taking the QBasic code and modifying it to the extreme.
May 31st, 2008 at 15:39
Actually, my snake was in Borland C++. I recall the QuickBasic example, but somehow I’ve never connected to Basic and its kin so I didn’t play with it too much.
I’m enough of a hoarder to have kept all those old programs, by the way. My “snake” clone is called Worm, written circa May 1999. It’s OO C++ (!!)
Now, if I could just make it run… (it uses Borland’s old graphics libs I can’t find)
May 31st, 2008 at 17:56
Sounds cool.
For the graphics libs, try googling for sites that archive old software – by using appropriate keywords. Then search on those sites for the libs. Might get it.
- Vasudev Ram
http://www.dancingbison.com
May 31st, 2008 at 23:08
Very nice. If the pieces rotated counterclockwise, it would be perfect! Luckily I could easily change a boolean to make it so.
I noticed the Mandelbrot in your header. You might want to take a look at Aptus.
June 2nd, 2008 at 08:45
Looks nice!
Looked into the code, and somehow felt a strong smell of C++
July 24th, 2008 at 08:55
Works really well — cheers!
May 15th, 2009 at 13:49
By the way, it won’t run if you have both wxpython 2.6 and 2.8 installed at the same time because it will default to using 2.6. One way to easily get around this is to modify the first 3 lines at the top of the wxpytris.py so that they look like this (make sure you have wxversion installed first):
import sys
import wxversion
wxversion.select(’2.8′)
import wx
January 11th, 2012 at 10:27
How did you packed the output of py2exe into one executable?
January 11th, 2012 at 10:35
This is the
setupcall:setup(options = {"py2exe": {"compressed": 1,
"optimize": 2,
#~ "ascii": 1,
"includes": ["encodings"],
"bundle_files": 1}},
zipfile = None,
windows = [test_wx],
)
January 11th, 2012 at 13:39
Thank you. I didn’t know the compressed option.
Do you have any idea if the compression affects execution speed?
January 11th, 2012 at 14:42
capic,
I don’t know. If I had to guess, I’d say it affects load-time (start-up) speed a bit, but probably not execution speed afterwards (since the DLLs have to be loaded into memory uncompressed anyway)