Code Comments Tutorial: Simulate – A “Simon” Clone

Welcome to the Code Comments Tutorial for Simulate (A “Simon” 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 BUTTONSIZE, FLASHSPEED, TIMEOUT, etc.) This code uses global variables instead of object oriented programming in order to make it simple to understand.

Simulate source code with comments.

(more…)

Read More

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 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 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.

(more…)

Read More

Code Comments Tutorial: Ink Spill – A “Flood It” Clone

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

UPDATE: I’ve since created a “fancy version” of the game that adds some nice images and variable board sizes and difficulty settings. The zip file can be downloaded below.

Ink Spill source code with comments.

(more…)

Read More

Gorilla.py: A Remake of Gorilla.bas

Source code: gorilla.py

An entire generation of people remember the Gorilla.BAS game that came with Qbasic, where gorillas on top of buildings threw exploding bananas at each other. This is a Python remake of that game using the Pygame game engine, and is fairly heavily commented so you can explore the source. To play the game in your browser, there is also a Flash version of Gorilla.BAS.

My prime motivation behind creating this was to compare how much easier programming is today compared to a decade ago with Qbasic. Python makes a great first language to learn, and I have argued before how Python is the New BASIC. (The second motivation was to promote my (free) book on Python games and get people back into programming. :) )

To demonstrate: not counting blank lines and comments (and the multiline strings for the graphics), Qbasic’s gorillas.bas is 784 lines of code while Python’s gorillas.py is 544 lines of code (about 30% less code!) Every programmer’s mileage may vary, but I think that makes a good case for Python’s syntax being expressive and simple.

This game was written by the author of “Invent Your Own Computer Games with Python”, a free book available under a Creative Commons license at http://inventwithpython.com. The book teaches Python to kid and adult beginners by giving the source code to several different games (along the lines of gorilla.py) and explaining them line by line. Learning to program this way is fun!

A print copy of the book is available (with free shipping) on Amazon.com for $25.

Read More