Posts by Al.

New Game Source Code: Simon Gesture (Simon clone with mouse gestures)

This game works with Python 2 or 3, though you’ll need the Pygame framework first. This game makes use of the MooseGesture mouse gestures module I developed.

Draw out horizontal, vertical, or diagonal lines in the same directions that the dot moves. The pattern of the dot’s movement becomes longer and longer.

Source code & music file.

If you’d like to learn about how the mouse gesture algorithm in the moosegesture.py module works, look at the MooseGesture post

Pygcurse – A “curses” Emulator Built on Pygame

Pygcurse (pronounced “pig curse”) is a curses library emulator that runs on top of the Pygame framework. It provides an easy way to create text adventures, roguelikes, and console-style applications. The mascot of Pygcurse is a blue pig with a skull tattoo on its butt.

Download Pygcurse and Demo Programs.

Read the Pygcurse tutorial.

View the Pygcurse homepage.

Pygcurse provides several benefits over normal text-based stdio programs:

  1. Color text and background.
  2. The ability to move the cursor and print text anywhere in the console window.
  3. The ability to make console apps that make use of the mouse.
  4. The ability to have programs respond to individual key presses, instead of waiting for the user to type an entire string and press enter (as is the case with input()/raw_input()).
  5. The ability to use any font and any character in those fonts.
  6. Since the console window that Pygcurse uses is just a Pygame surface object, additional drawing and transformations can be applied to it. Multiple surfaces can also be used in the same program.

Pygcurse also provides some additional features that curses normally doesn’t, such as tinting, shadows, textboxes, and line drawing functions.

Pygcurse requires Pygame to be installed. Pygame can be downloaded from http://pygame.org. Pygcurse can be used with either Python 2 or Python 3.

Rough Drafts of Next Two Books Available

I’ve decided to make the incomplete rough drafts of my next two Python books available.

Become a Codebreaker with Python

Making Graphical Games with Pygame

The emphasis is on “rough” and “incomplete”, but I thought it would be better to give a preview of the direction I was going. These books are also available under a Creative Commons BY-NC-SA license like the first “Invent with Python” book.

The Code Breaker book is aimed at complete beginners who have never programmed before, and as such has a lot of the same content as Invent with Python. It covers various encryption programs, and also how to write programs that can break encryption. (It’s an intro to programming and cryptography at the same time.)

The Pygame book is aimed at people who have read the first book or have a moderate amount of Python experience, and want to learn how to use the Pygame library to make graphical games. So far, the book really only has the source code for the games that will be in the book (these are the same programs that have been featured on this blog before.)

Hope you enjoy them. Feel free to send any ideas on content or presentation to me (don’t bother with typos and such, these are incomplete drafts and those are probably known issues.) al@inventwithpython.com

 

 

 

 

 

Nobody Cares About a Few Million Nanoseconds

A Clever Programming Trick…

If you need to swap the values of two variables, this usually requires a third temporary variable (that is, if you’re not using a language like Python that supports the a, b = b, a syntax.) It looks something like this:

temp = a;
a = b;
b = temp;

But if these are integer variables, there’s a nifty trick to save yourself a little bit of memory. You can use arithmetic instead of a temporary variable:

a = a + b;
b = a - b;
a = a - b;

If the integers on your platform are 32-bits, your new swap will save four bytes of memory.

NOBODY CARES ABOUT FOUR BYTES OF MEMORY. More… »

Recursion Explained with the Flood Fill Algorithm (and Zombies and Cats)

This is a programming tutorial for beginner and intermediate programmers who want to learn what recursion is. The programming language used for the examples is Python, but you can probably follow along if you know programming in some other language such as PHP or JavaScript. There’s a lot more information about recursion on the Wikipedia article: http://en.wikipedia.org/wiki/Recursion_(computer_science) But this guide is meant to be a more practical guide to show how handy recursion is.

The source code of everything in this article can be downloaded here: floodfill_src.zip

Consider the Lazy Zombie

This is a cat:

This is a normal human:

This is a normal human who has been turned into an ungodly, flesh-eating zombie of the undead:

Zombies are lazy and will only bite things that are next to them. Humans that are bitten will then turn into zombies:

There is an interesting recursive principle here, because the humans that have turned into zombies will start to bite other humans that are next to them, which will make more zombies, who bite more adjacent humans, which will make more zombies, and so on and so on in a chain reaction:

More… »

Call for help to review games & code for a new “Invent with Python” book.

UPDATE: Thanks for everyone who emailed their help. I’ll leave this post up here for now, but the code seems fairly solid now.

I’m currently looking for help editing the source code for the games that will go into my next book. This book will also be released under a Creative Commons license and be freely available.

Download the game source. (Requires Python & Pygame)

UPDATE: I’ve fixed a problem where the midi files for the Tetris game were left out of this zip. Redownload the zip file to get them.

More… »

New Game Source Code: Gemgem (A Bejeweled clone)

Here’s the source code for a Bejeweled clone called Gemgem, written in Python with the Pygame library. You’ll need Python (2 or 3) and Pygame installed to run it. I’ve tried to keep the source code simple so it’s easy to follow and learn programming and Pygame from it. It comes in under 540 lines of code, including whitespace & comments.

Download the Gemgem source code and graphics.

To play, click on (or drag over) two adjacent gems to swap their position. You need to swap them so that there are three or more gems of the same type in a row. This causes the gems to disappear and new gems to fall in their place. You get more points for chain reactions or more-than-three matches. The score constantly trickles down, so solve as fast as possible. The game ends when no more moves can be made.

The backspace key will reset the board, the escape key quits.

The graphics were from Osmic on opengameart.org

New Game Source Code: Star Pusher (Sokoban clone)

Here’s a Sokoban (“box pusher”) clone called Star Pusher. I’ve used the graphics from the Planet Cute collection. You’ll need Python (2 or 3) and Pygame installed to run it. Just download and unzip the files to the same directory. It comes with 201 levels from David W. Skinner.

Download the source code and graphics.

(UPDATE: I’ve updated the code so that it works on Linux. It was due to a \r\n newline issue. I’ve tested the game in Ubuntu and it works.)

The source code is designed to be readable so that new programmers can understand and modify it without much effort.

New Game Source Code: Four in a Row

I’ve made a Connect Four AI. There is a text-only version and a graphical version made with Pygame. You will need Python 3 (not Python 2) to play the games (and Pygame for the graphical version.)

In the graphical version, drag the red tokens over the top of the board to make a move.

Source Code and image files zipped.

fourinarow.py (text version, doesn’t need Pygame)

gFourinarow.py (graphical version, needs image files)

Black token image, Red token image, Board image

Great Pygame Example Site

I wanted to share this link to a great site with some simple Pygame examples:

Python and Pygame Examples

There is also a textbook draft called “Introduction to Computer Science Using Python and Pygame” by Paul Vincent Craven.

Switch to our mobile site