Posts categorized “Games”.

New Game Source Code: Squirrel Eat Squirrel

Made a new game with Pygame. It’s called “Squirrel Eat Squirrel”, where you move your squirrel around the screen eating the smaller squirrels and avoiding the larger ones. The more squirrels you eat, the larger you grow. This is a Python 3 game, but I think it’s compatible with Python 2. You need Pygame installed as well.

Use the arrow keys to move around. You can be hit three times before you die.

Download the source & images.

Try modifying the constant variables at the top of the file to change around the game. (Squirrel speeds, number of squirrels, amount of health, etc.) This isn’t part of my Code Comments tutorials, since I haven’t had time to go through and add detailed comments to the code (but it’s still commented.)

New Extra Game: Connect Four clone

I have a text version of a Connect Four clone done. The AI for it looks ahead two moves, which makes it fairly impossible to beat unless you concentrate. I was planning to use this game for a chapter on recursion in my next book, but decided to publish the code for the text-version now.

Download fourinarow.py (This is for Python 3, not Python 2)

The code has few comments, but looking at its source code might be a good exercise for someone learning to program. It’s available on the book’s website in the Extra section.

Code Comments Tutorial: Flippy

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.

Flippy source code with comments.

More… »

Code Comments Tutorial: Tetromino

Welcome to the Code Comments Tutorial for Tetromino, a Tetris 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 WINDOWWIDTH, FPS, BLOCKSIZE, etc.) This code uses global variables instead of object oriented programming in order to make it simple to understand.

Tetromino source code with comments.

tetrisb.mid background music. (required)

tetrisc.mid background music. (required)

More… »

Code Comments Tutorial: Snakey

Welcome to the Code Comments Tutorial for Snakey, a Nibbles 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 WINDOWWIDTH, FPS, CELLSIZE, etc.) This code uses global variables instead of object oriented programming in order to make it simple to understand.

Snakey source code with comments.

More… »

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

More… »

Switch to our mobile site