Code Comments Tutorial: Memory Game

Welcome to the Code Comments Tutorial for Memory. 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

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 and view its documentation.

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

Memory source code with comments.

Memory source code without comments.

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

How To Play Memory:

Click on one of the tiles, and then click on a second one. The colored shape behind the white box will be revealed. Try to remember the shapes you see while playing, and select the same colored shape in a pair to match them. The game is won once you have matched all the shapes.

4 thoughts on “Code Comments Tutorial: Memory Game

  1. wow this is awesome, I'm trying to make this game for my final project in computer programming at school, a simplified version obviously... having some trouble with it... wish I could do it as good as you guys!

  2. Hi.
    I know your post is 4 years old but that Memory Game using Pygame is just what I need to serve as a base for an experiment I'm currently building.
    Unfortunately, the code is no longer accessible via the links on this page. If you still have it would you mind sharing?
    It would be a great help.

Leave a Reply

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