Posts from September 2010.

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

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

I Want To Teach My Kid How to Program

Computer programming is a practical skill that can be applied to many professions and hobbies besides software development. However, it can be intimidating to break into. This guide will help parents point their kids in the right direction to get started in programming. (And it is also useful for anyone to get into programming.)

The Gist

  • Kids as young as 9 (or even younger) can learn programming, it doesn't require math skills beyond basic arithmetic.
  • Making video games is the best way to learn programming and stay interested.
  • Python is one of the best programming languages to learn for a first language.
  • If your kid finds typing frustrating, then Scratch might be a better language to learn.
  • "Invent Your Own Computer Games with Python" is a free book that teaches programming to complete beginners in Python.
  • py2exe can help your kid share their programs with their friends.
  • When making your own games, computer version of board games or simple games of chance are a good starting point.

There is no "best" age to start programming; any age is fine. I'd recommend around 10 or 12 would be a good starting point, though I began teaching myself programming around 9. Perhaps before 8 would be "too young". Despite what you may think, programming does not require math skills beyond basic arithmetic. If your child is comfortable with addition, subtraction, and multiplication (maybe even division), then they will be fine. Programming is more about general problem solving and "recipe following" skills than mathematics.

Learning to program, like learning anything, is not about having a high IQ so much as being enthusiastic enough to practice and wanting to learn more. I think the best route to learning programming is by making video games.

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

The Top 10 Pygame Tutorials

UPDATE: This is an old post, written before I finished my book entirely dedicated to Pygame. You can read a free online copy of "Making Games with Python & Pygame" at http://inventwithpython.com/pygame.

I've scoured the web for some decent tutorials for Pygame, one of the best game engines for Python out there. Here's what I've found, ordered by (in my opinion) quality. Perhaps the most comprehensive guide to Pygame would be the Pygame documentation itself, or Beginning Game Development with Python and Pygame: From Novice to Professional by Will McGuan.

1. Eli Bendersky's website: Writing a game in Python with Pygame

Possibly the best Pygame tutorial on the web. The game example he covers is original and touches on many different concepts. The writing is concise and to the point.

Part I
Part II
Part III
Part IV

2. Invent Your Own Computer Games with Python by Al Sweigart

This is my own tutorial for Pygame. The first three chapters cover the basics of Pygame's features, and the fourth chapter details the entire source code to a simple "Dodger" game.

Chapter 17 - Graphics and Animation
Chapter 18 - Collision Detection and Input
Chapter 19 - Sound and Images
Chapter 20 - The "Dodger" Game

UPDATE: I also have the rough draft completed copy for my Pygame-specific book available: Making Games with Python & Pygame

More... »