Code Comments Tutorial: Slide Puzzle Game

Welcome to the Code Comments Tutorial for Slide Puzzle. 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 BGCOLOR, COLS, ROWS, TILESIZE, etc.) This code uses global variables instead of object oriented programming in order to make it simple to understand.

Slide Puzzle source code with comments.

Slide Puzzle source code without comments.

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

How To Play Slide Puzzle:

There are 15 boxes in a 4x4 grid. At the start of the game, the boxes are randomly switched around the board to generate a new puzzle. When the puzzle is ready, start sliding around the boxes to reorder the boxes.

4 thoughts on “Code Comments Tutorial: Slide Puzzle Game

  1. First of all, excuse my english!!! ;p
    Just share with everyone the funny and interesting that Al does with python!!! I've begun with his book two months ago, and now it's a hobby for me, trying to write my own programs in Python.... but it isn't so easy as it looks... ;p

    Congratulations Al for your book.... And thanks for sharing your knowledge about python and for your updates from your Blog...

    If I have a child, he/she will learn to program in Python with your book.... I promise...

    Thank you so much!!!!! An Spaniard Noob!!!!

  2. hi I really really need your help
    Can u contact me..?
    I have a homework that making a slide puzzle game with python
    But im very poor on python...
    I have just 2hours..y0y
    Can you help me?
    I don't know what can i do

Leave a Reply

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