The Invent with Python Blog

Writings from the author of Automate the Boring Stuff.

Code Comments Tutorial: Flippy

Mon 03 January 2011    Al Sweigart

Welcome to the Code Comments Tutorial for Flippy, an Othello 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.

This particular program is a graphical version of reversi.py in the Invent with Python book. The original reversi.py is covered specifically in chapter 15, so it may be helpful to read that chapter while looking at the source code for Flippy.

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

Screenshot of Flippy game

Flippy source code with comments.

Flippy source code without comments.

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

How To Play Flippy:

Place down tiles so that you have your opponent's tiles in between your newly placed tile and one of your existing tiles. All of your opponent's surrounded tiles will be flipped to become your colored tiles. You and the computer take turns placing down tiles until no more moves can be made. The player with the most tiles of their color on the board at the end wins.

Note that tiles on the side of the board are less likely to be surrounded, and any tiles on the corners can never be flipped.

There is a hints mode which can be toggled on to show you every valid move you can make on the board.


Learn to program for free with my books for beginners:

Sign up for my "Automate the Boring Stuff with Python" online course with this discount link.

Email | Mastodon | Twitter | Twitch | YouTube | GitHub | Blog | Patreon | LinkedIn | Personal Site