Free Python Games API Reference

Free Python Games includes a few helpful utilities. The best way to expose beginners to these functions is with Python’s built-in help function. Learners should be able to understand and write the drawing functions themselves.

Drawing Functions

freegames.line(a, b, x, y)[source]

Draw line from (a, b) to (x, y).

freegames.square(x, y, size, name)[source]

Draw square at (x, y) with side length size and fill color name.

The square is oriented so the bottom left corner is at (x, y).

Helper Functions

freegames.floor(value, size, offset=200)[source]

Floor of value given size and offset.

The floor function is best understood with a diagram of the number line:

 -200  -100    0    100   200
<--|--x--|-----|--y--|--z--|-->

The number line shown has offset 200 denoted by the left-hand tick mark at -200 and size 100 denoted by the tick marks at -100, 0, 100, and 200. The floor of a value is the left-hand tick mark of the range where it lies. So for the points show above: floor(x) is -200, floor(y) is 0, and floor(z) is 100.

>>> floor(10, 100)
0.0
>>> floor(120, 100)
100.0
>>> floor(-10, 100)
-100.0
>>> floor(-150, 100)
-200.0
>>> floor(50, 167)
-33.0
freegames.path(filename)[source]

Return full path to filename in freegames module.

Vectors

class freegames.vector(x, y)[source]

Two-dimensional vector.

Vectors can be modified in-place.

>>> v = vector(0, 1)
>>> v.move(1)
>>> v
vector(1, 2)
>>> v.rotate(90)
>>> v
vector(-2.0, 1.0)
__abs__() -> abs(v)[source]
>>> v = vector(3, 4)
>>> abs(v)
5.0
__add__(w) → v + w[source]
>>> v = vector(1, 2)
>>> w = vector(3, 4)
>>> v + w
vector(4, 6)
>>> v + 1
vector(2, 3)
>>> 2.0 + v
vector(3.0, 4.0)
__eq__(w) → v == w[source]
>>> v = vector(1, 2)
>>> w = vector(1, 2)
>>> v == w
True
__getitem__(v, i) → v[i][source]
>>> v = vector(3, 4)
>>> v[0]
3
>>> v[1]
4
>>> v[2]
Traceback (most recent call last):
    ...
IndexError
__hash__() -> hash(v)[source]
>>> v = vector(1, 2)
>>> h = hash(v)
>>> v.x = 2
Traceback (most recent call last):
    ...
ValueError: cannot set x after hashing
__iadd__(w) → v += w[source]
>>> v = vector(1, 2)
>>> w = vector(3, 4)
>>> v += w
>>> v
vector(4, 6)
>>> v += 1
>>> v
vector(5, 7)
__imul__(w) → v *= w[source]
>>> v = vector(1, 2)
>>> w = vector(3, 4)
>>> v *= w
>>> v
vector(3, 8)
>>> v *= 2
>>> v
vector(6, 16)
__init__(x, y)[source]

Initialize vector with coordinates: x, y.

>>> v = vector(1, 2)
>>> v.x
1
>>> v.y
2
__isub__(w) → v -= w[source]
>>> v = vector(1, 2)
>>> w = vector(3, 4)
>>> v -= w
>>> v
vector(-2, -2)
>>> v -= 1
>>> v
vector(-3, -3)
__itruediv__(w) → v /= w[source]
>>> v = vector(2, 4)
>>> w = vector(4, 8)
>>> v /= w
>>> v
vector(0.5, 0.5)
>>> v /= 2
>>> v
vector(0.25, 0.25)
__len__() -> len(v)[source]
>>> v = vector(1, 2)
>>> len(v)
2
__mul__(w) → v * w[source]
>>> v = vector(1, 2)
>>> w = vector(3, 4)
>>> v * w
vector(3, 8)
>>> v * 2
vector(2, 4)
>>> 3.0 * v
vector(3.0, 6.0)
__ne__(w) → v != w[source]
>>> v = vector(1, 2)
>>> w = vector(3, 4)
>>> v != w
True
__neg__() → -v[source]
>>> v = vector(1, 2)
>>> -v
vector(-1, -2)
__radd__(other)

v.__add__(w) -> v + w

>>> v = vector(1, 2)
>>> w = vector(3, 4)
>>> v + w
vector(4, 6)
>>> v + 1
vector(2, 3)
>>> 2.0 + v
vector(3.0, 4.0)
__repr__() -> repr(v)[source]
>>> v = vector(1, 2)
>>> repr(v)
'vector(1, 2)'
__rmul__(other)

v.__mul__(w) -> v * w

>>> v = vector(1, 2)
>>> w = vector(3, 4)
>>> v * w
vector(3, 8)
>>> v * 2
vector(2, 4)
>>> 3.0 * v
vector(3.0, 6.0)
__sub__(w) → v - w[source]
>>> v = vector(1, 2)
>>> w = vector(3, 4)
>>> v - w
vector(-2, -2)
>>> v - 1
vector(0, 1)
__truediv__(w) → v / w[source]
>>> v = vector(1, 2)
>>> w = vector(3, 4)
>>> w / v
vector(3.0, 2.0)
>>> v / 2
vector(0.5, 1.0)
copy()[source]

Return copy of vector.

>>> v = vector(1, 2)
>>> w = v.copy()
>>> v is w
False
move(other)[source]

Move vector by other (in-place).

>>> v = vector(1, 2)
>>> w = vector(3, 4)
>>> v.move(w)
>>> v
vector(4, 6)
>>> v.move(3)
>>> v
vector(7, 9)
rotate(angle)[source]

Rotate vector counter-clockwise by angle (in-place).

>>> v = vector(1, 2)
>>> v.rotate(90)
>>> v == vector(-2, 1)
True
scale(other)[source]

Scale vector by other (in-place).

>>> v = vector(1, 2)
>>> w = vector(3, 4)
>>> v.scale(w)
>>> v
vector(3, 8)
>>> v.scale(0.5)
>>> v
vector(1.5, 4.0)
x

X-axis component of vector.

>>> v = vector(1, 2)
>>> v.x
1
>>> v.x = 3
>>> v.x
3
y

Y-axis component of vector.

>>> v = vector(1, 2)
>>> v.y
2
>>> v.y = 5
>>> v.y
5