Module gamelib

Gamelib is a pure-Python single-file library/framework for writing simple games. It is intended for educational purposes (e.g. to be used in basic programming courses).

https://github.com/dessaya/python-gamelib

Functions

def check_audio_format(path)

Produce a warning message if the audio format is not supported

def check_image_format(path)

Produce a warning message if the image format is not supported

def draw_arc(x1, y1, x2, y2, **options)

Draw an arc, pieslice, or chord in the bounding box between points x1, y1 and x2, y2.

To see all supported options, see the documentation for tkinter.Canvas.create_arc.

Example

gamelib.draw_arc(10, 10, 20, 20, outline='white', fill='red')
def draw_begin()

Clear the window.

Any call to draw_* should be between draw_begin() and draw_end().

Example

gamelib.draw_begin()
gamelib.draw_rectangle(0, 0, 10, 10, fill='red')
gamelib.draw_end()
def draw_end()

Refresh the window.

Any call to draw_* should be between draw_begin() and draw_end().

Example

gamelib.draw_begin()
gamelib.draw_rectangle(0, 0, 10, 10, fill='red')
gamelib.draw_end()
def draw_image(path, x, y)

Draw an image located at path in the coordinates x, y.

Example

gamelib.draw_image('images/player.gif', 10, 10)

Note

The only image formats that are supported accross all platforms (Windows/Mac/Linux) are GIF and PPM/PGM/PBM.

def draw_line(x1, y1, x2, y2, **options)

Draw a straight line between points x1, y1 and x2, y2.

To see all supported options, see the documentation for tkinter.Canvas.create_line.

Example

gamelib.draw_line(10, 10, 30, 20, fill='blue', width=2)
def draw_oval(x1, y1, x2, y2, **options)

Draw an ellipse in the bounding box between points x1, y1 and x2, y2.

To see all supported options, see the documentation for tkinter.Canvas.create_oval.

Example

gamelib.draw_oval(10, 10, 30, 20, outline='white', fill='red')
def draw_polygon(points, **options)

Draw a polygon with vertices in the given points coordinates list. The list must have an even amount of numbers; each pair determines a vertex. The last vertex is automatically joined with the first one with a segment.

To see all supported options, see the documentation for tkinter.Canvas.create_polygon.

Example

gamelib.draw_polygon([10, 10, 30, 20, 0, 40], outline='white', fill='red')
def draw_rectangle(x1, y1, x2, y2, **options)

Draw an rectangle in the bounding box between points x1, y1 and x2, y2.

To see all supported options, see the documentation for tkinter.Canvas.create_rectangle.

Example

gamelib.draw_rectangle(10, 10, 30, 20, outline='white', fill='red')
def draw_text(text, x, y, font=None, size=12, bold=False, italic=False, **options)

Draw some text at coordinates x, y with the given properties.

Args

text
The text to draw.
x
The screen coordinates for the text.
y
The screen coordinates for the text.
font
Font family name (eg: 'Helvetica'). Note: the only font guaranteed to be available in all systems is the default font. If the selected font is not found, the default font will be used instead.
size
Size of the text.
bold
Whether or not to use bold weight.
italic
Whether or not to use italic slant.

Some of the supported extra options are:

  • fill: Fill color. It can be named colors like 'red', 'white', etc, or a specific color in '#rrggbb' hexadecimal format.
  • anchor: Where to place the text relative to the given position. It may be any combination of n (North), s (South), e (East), w (West) and c (center). Default is c.

To see all supported options, see the documentation for tkinter.Canvas.create_text.

Example

gamelib.draw_text('Hello world!', 10, 10, fill='red', anchor='nw')
def get_events()

Get the list of Events that happened since the last call to get_events().

This function is normally used in combination with loop(), in action games.

Example

while gamelib.loop(fps=30):
    # this is executed 30 times per second
    for event in gamelib.get_events():
        if event.type == gamelib.EventType.KeyPress and event.key == 'q':
            return
def icon(path)

Set the window icon to the image located at path.

Example

gamelib.icon('images/icon.gif')

Note

The only image formats that are supported accross all platforms (Windows/Mac/Linux) are GIF and PPM/PGM/PBM.

def init(game_main, args=None)

Initialize gamelib.

Args

game_main
Your main function.
args
List of arguments to be passed to the main function, or None.
def input(prompt)

Ask the user to enter a text value.

Args

prompt
A message to display.

Returns

A string containing the value that the user typed. None if the user clicked on Cancel instead of OK.

def is_alive()

Returns True if the game window is open.

Example

while gamelib.is_alive():
    event = gamelib.wait(gamelib.EventType.KeyPress):
    gamelib.say(f'You pressed {event.key}')
def loop(fps=30)

When used in a while loop, the body will be executed fps times per second.

Returns

True if the game window is still open, False otherwise.

Example

while gamelib.loop(fps=30):
    # this is executed 30 times per second
    for event in gamelib.get_events():
        if event.type == gamelib.EventType.KeyPress and event.key == 'q':
            return
def play_sound(sound)

Play a sound located at the given path.

Example

gamelib.play_sound('sound/jump.wav')

Note

The only sound format that is supported accross all platforms (Windows/Mac/Linux) is WAV.

def resize(w, h)

Resize the window to be w pixels wide and h pixels tall.

def say(message)

Present the user with the given message in a dialog box with an OK button.

def title(s)

Set the window title to s.

def wait(event_type=None)

Wait until the next Event: a key is pressed/released, the mouse is moved, etc, and return it.

This function is normally used in combination with is_alive(), in turn-based games.

Args

event_type
If an EventType is passed, the function will ignore any events that are not of this type. (It will still return None when the game is closed).

Returns

An Event, or None if the user closed the game window.

Example

while gamelib.is_alive():
    event = gamelib.wait(gamelib.EventType.KeyPress):
    gamelib.say(f'You pressed {event.key}')

Classes

class Event (tkevent)

Represents an event generated by the user.

Attributes

type
An EventType.
key
A key that has been pressed/released.
mouse_button
0, 1 or 2 for left, right and middle mouse buttons respectively.
x
The current mouse horizontal position, in pixels.
y
The current mouse vertical position, in pixels.

This is actually a wrapper for the Tkinter Event class. Any of the tk.Event attributes can be accessed through this object.

See also

wait(), get_events()

class EventType (value, names=None, *, module=None, qualname=None, type=None, start=1)

An enumeration of the different types of Events supported by gamelib.

Ancestors

  • enum.Enum

Class variables

var ButtonPress

The user pressed a mouse button.

var ButtonRelease

The user released a mouse button.

var KeyPress

The user pressed a key.

var KeyRelease

The user released a key.

var Motion

The user moved the mouse over the window.