A Tetris clone in Python / wxPython

May 31st, 2008 at 12:18 pm

As 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):

wxPyTris screenshot

And this is the About box, with some more information:

wxPyTris About dialog

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:

  1. Tetris implemented in PyQt
  2. Book review: “Beginning game development with Python and Pygame” by Will McGugan
  3. matplotlib with wxPython GUIs
  4. A year with Python
  5. Python impressions

11 Responses to “A Tetris clone in Python / wxPython”

  1. ripper234No Gravatar Says:

    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.

  2. elibenNo Gravatar Says:

    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)

  3. Vasudev RamNo Gravatar Says:

    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

  4. Ned BatchelderNo Gravatar Says:

    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.

  5. MichaelNo Gravatar Says:

    Looks nice!
    Looked into the code, and somehow felt a strong smell of C++ :)

  6. Sam DuttonNo Gravatar Says:

    Works really well — cheers!

  7. PyManNo Gravatar Says:

    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
    import wx

  8. capicNo Gravatar Says:

    How did you packed the output of py2exe into one executable?

  9. elibenNo Gravatar Says:

    This is the setup call:
    options = {"py2exe": {"compressed": 1,
    "optimize": 2,
    #~ "ascii": 1,
    "includes": ["encodings"],
    "bundle_files": 1}},
    zipfile = None,
    windows = [test_wx],

  10. capicNo Gravatar Says:

    Thank you. I didn’t know the compressed option.
    Do you have any idea if the compression affects execution speed?

  11. elibenNo Gravatar Says:


    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)