Code Comments Tutorial: Tetromino

Welcome to the Code Comments Tutorial for Tetromino, a Tetris clone. Code Comments is a series of simple games with detailed comments in the source code, so you can see how the game works.

The text in between the triple-double-quotes are comments (technically they are multi-line strings, but Python uses them for multi-line comments). The Python interpreter ignores any text in between them, so we can add any comments about the source code without affecting the program. In general for Code Comments, the comments will describe the lines of code above the comment. It helps to view this file either on the Code Comments site or with a text editor that does “syntax highlighting”, so that the comments appear in a separate color and are easier to distinguish from the code.

This Code Comments assumes you know some basic Python programming. If you are a beginner and would like to learn computer programming, there is a free book online called “Invent Your Own Computer Games with Python” at http://inventwithpython.com

The Code Comments programs make references to sections of this book throughout the program. This Code Comments can also teach you how to use the Pygame library to make your own games with graphics, animation, and sound. You can download Pygame from http://pygame.org and view its documentation.

You can make some easy modifications to the game by changing the all-caps constant variables (such as WINDOWWIDTH, FPS, BLOCKSIZE, etc.) This code uses global variables instead of object oriented programming in order to make it simple to understand.

Tetromino source code with comments.

tetrisb.mid background music. (required)

tetrisc.mid background music. (required)

Tetromino source code without comments.

Tetromino .exe executable Windows binary (no need to download Python & Pygame)

How To Play Tetromino:

Move and rotate the falling blocks for as long as possible. The falling rate will increase the longer you play. If the blocks fill up to the top, you lose.

4 thoughts on “Code Comments Tutorial: Tetromino

  1. Is there a reason why you don’t write your code (especially your comments) in lines of 80 chars? I did open your code in my browser and it is really hard to read because the lines are incredable long.
    But overall nice work ;) I’m interested in more!

  2. These games you make are great! One (not necessarily small) plea. I have tried to learn object-oriented programming. Would it be impossibe to include also OO example? It would me very educational way to see the difference.

  3. This program didn’t work for me out-of-the box. I got this error:
    TypeError: ‘dict_keys’ object does not support indexing

    I resolved this error by changing line 491 from
    shape = random.choice(PIECES.keys())
    to
    shape = random.choice(list(PIECES.keys()))

    Is this a recent change to python?

    NOTE FROM AL: Hey, thanks for pointing this out. It was some bug that caused the game to work with Python 2 but not 3. The version up now should work with both. Thanks again!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>