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

Posted by Al Sweigart at 10:37 am on July 30th, 2012.

Categories: Uncategorized.

### 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)
0.8414709848078965
>>> math.sin(2)
0.90929742682568171
>>> math.sin(3)
0.14112000805986721
>>> math.sin(4)
-0.7568024953079282
>>> math.sin(5)
-0.95892427466313845

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

Posted by Al Sweigart at 9:20 am on July 18th, 2012.

Categories: Tutorials.

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
print('Hello!')

**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:
print('Hello!')

**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:

print('Hello!')
print('Howdy!')

…and this:

if spam == 42:
print('Hello!')
print('Howdy!')

…and this:

if spam == 42:
print('Hello!')

More… »

Posted by Al Sweigart at 10:00 am on July 9th, 2012.

Categories: Uncategorized.