Latest posts.

Source Code Makeover: Square Shooter, Part 2

UPDATE: Felix, the original author of Square Shooter, has made some comments in the comment section that are pretty insightful. I recommend you check them out.

This is a continuation from Part 1, where I go through the source code of Square Shooter, an Asteroids clone, and try to redesign the code to be more readable. (Part 3 is also up.)

All of the changes I make can be viewed on github. You can also download the original source code for Square Shooter.
More... »

Source Code Makeover: Square Shooter, Part 1

UPDATE: Felix, the original author of Square Shooter, has made some comments in the comment section that are pretty insightful. I recommend you check them out.

In this blog post, I'm taking a game off of and going through it to make it more readable and extend its functionality. You'll see an example of how to take code that works and changes to improve it's design (but more importantly, I explain why I make those changes). This is an intermediate level tutorial and assumes some familiarity with Python. This can be pretty helpful if you know programming basics but want to know, "How can I write better code?"

(Part 2 and Part 3 are now up as well.)

This blog post covers Square Shooter, made by Felix Pleșoianu. You can download the original source or view my changes on GitHub.

I only check in working code, so you can download the code at any point in history and see how it looks. I make a lot more check ins than I normally would, but I want you to see the gradual changes that I make.

(UPDATE: A note to everyone who mentions that I break PEP-8, see this link. Also, see this link.)

Let's play it. You'll need to have Python (a version 2, not version 3) and Pygame installed on your computer to run the file. It looks like a standard Asteroids clone. I notice that the circles dont wrap around the map until the center of the circle goes past the edge. Also, the bullets don't wrap around either, they just stop once they get to the edge.

Now take a look at the original source code. Try to get an idea of what each of the parts do, but you don't need to fully understand everything before continuing. (I didn't understand all the code when I started cleaning it up.)

First we'll do just a code clean up, and then we'll look at changing the the game to improve play and user experience.

The computer doesn't care what your code looks like. It will run the most unreadable code just fine. But the purpose of cleaning up code to be readable is so that it is easier to understand for humans so they can easily change code either for new features or bug fixes. This becomes especially important if multiple people are working on the same source code.

More... »

Need a Game Idea? A List of Game Mechanics and a Random Mechanic Mixer.

If you need an idea for a game, try this random game mechanic generator:

A full list is at the bottom of this page. I wanted to get fairly low level with these game mechanics, so I don't include things such as "first person shooter" or "puzzles" (since those are, in my opinion, more correctly called genres).

Just to prove to myself that this generator isn't worthless, I decided to make some small Python/Pygame games based off of it. These games are fairly rough since I wanted to rapidly produce them as proof-of-concepts. The mechanics that got selected were:

More... »

Using Trigonometry to Animate Bounces, Draw Clocks, and Point Cannons at a Target

About Trigonometry

(Links to the Python programs in this tutorial. Requires Python and Pygame to be installed.)

Trigonometry is the branch of mathematics that studies triangles and the relationships between their sides and the angles between these sides. The sine, cosine, and tangent trigonometry functions are implemented as programming functions in most languages and given the names sin(), cos(), and tan(). In Python, these functions exist in the math module. These functions are very handy in games and graphical programs because of the smooth wave-like pattern their return values produce.

This tutorial shows how you can get a variety of neat animation behavior using these functions in your programs. (The animated gifs you see above were taken from the programs in this tutorial.) The code examples in this tutorial should work with both Python 2 and Python 3.

Trig Function Basics

You don't need to know how these functions work. Let's just treat these functions as black boxes: they take one number parameter in, and return a float value out. If you already know the mathematics of sine and cosine, then you can just skim this section.

In the interactive shell, let's see what math.sin() returns for some values:

>>> import math
>>> math.sin(1)
>>> math.sin(2)
>>> math.sin(3)
>>> math.sin(4)
>>> math.sin(5)

It looks like math.sin() just spits out some random-looking float values. (Actually, it's the length of the opposite side divided by the hypotenuse of a right triangle with the given angle. But you don't need to know or understand this.) But if we graph the return values of the input arguments 1 through 10 on a graph, we can see the pattern:

More... »

16 Common Python Runtime Errors Beginners Find

Figuring out what Python's error messages mean can be kind of tricky when you are first learning the language. Here's a list of common errors that result in runtime error messages which will crash your program.


1) Forgetting to put a : at the end of an if, elif, else, for, while, class, or def statement. (Causes “SyntaxError: invalid syntax”)

This error happens with code like this:

if spam == 42


2) Using = instead of ==. (Causes “SyntaxError: invalid syntax”)

The = is the assignment operator while == is the "is equal to" comparison operator. This error happens with code like this:

if spam = 42:


3) Using the wrong amount of indentation. (Causes “IndentationError: unexpected indent” and “IndentationError: unindent does not match any outer indentation level” and “IndentationError: expected an indented block”)

Remember that the indentation only increases after a statement ending with a : colon, and afterwards must return to the previous indentation. This error happens with code like this:


...and this:

if spam == 42:

...and this:

if spam == 42:


More... »

A Common Programmer Mistake: Dog is not the Opposite of Cat

True is the opposite of false. Up is the opposite of down. What is the opposite of dog?

If you replied “cat”, what exactly makes cats the opposite of dogs the same way up is the opposite of down?

I use this example to point out a common problem that software developers make with variable and parameter names. When there are two possible values for some variable or parameter, it is very common that it should be defined as a boolean type.
More... »