The Games Module

Rewriting of the graphics lib in ‘Python for the Absolute Beginner’ book.

class superwires.games.Animation(images, angle=0, x=0, y=0, top=None, bottom=None, left=None, right=None, dx=0, dy=0, repeat_interval=1, n_repeats=0, is_collideable=True)

A subclass of Sprite that rotates through a list of images.

This is basically an emulation of a gif. As with Message, you don’t get to override tick() as it is already being used.

class superwires.games.Keyboard

A class to check key states with the keyboard.

Similar to Mouse object, this is not used directly, but via module variable games.keyboard and calls to games.init().

is_pressed(key)

Returns if the given key is pressed. Key constants are listed in this module global scope (Check end of pydoc of this module for a complete list of key constants).

class superwires.games.Message(value, size, color, angle=0, x=0, y=0, top=None, bottom=None, left=None, right=None, dx=0, dy=0, lifetime=0, is_collideable=True, after_death=None)

An object that is displayed for a temporary period.

A text object that disappears in a few frames and calls a function as it diappears. Used for things like game over message which disappears in few seconds and closes the program as it disappears. Can’t override tick() as it is already used.

class superwires.games.Mouse

A class for controlling the mouse.

Usually used via calls to games.init() and module variable games.mouse.

Attributes:
x: The x position of the mouse. y: The y position of the mouse. position: A two-element tuple containing the x and y position. is_visible: Whether or not mouse cursor is visible.
is_pressed(button_num)

Tells if given key is pressed.

Args:
button_num: The button being checked to see if it is pressed.
It’s values are 0 to 2.
Returns:
Boolean representing whether button is pressed
class superwires.games.Music

A class to play music in the background.

Similar to Mouse object, this is not used directly, but via module variable games.music and calls to games.init().

fadeout(millisec)

Fadesout music in the given amount of milliseconds

load(filename)

Loads the music from filename.

play(loop=0)

Plays the loaded music.

Args:
loop: Amount of times to repeat(-1 is forever)
stop()

Stops music

class superwires.games.Screen(width, height, fps)

A screen class.

The screen to use everything with. Shouldn’t make own copy. Use init() instead and screen instead.

Attributes:
background: The background image for the screen. Make
sure it is the same size as the screen or bigger and it is not transparent or else Bad Things Happen.

all_objects: List of all the objects on the screen. event_grab: A boolean for if all input events are grabbed by the

program. Use the escape key to exit when true.

width: The screen’s width. Can’t change it. height: The screen’s height. Can’t change it.

add(sprite)

Adds sprite to list of objects on screen.

Args:
sprite: The sprite to add.
Raises:
ValueError: When given object isn’t a Sprite
clear()

Clears entire screen.

Removes all sprites from screen, only the background will be left.

mainloop()

Starts the event loop.

Sprites are moved, drawn, etc. The program will keep looping until screen.quit() is called.

quit()

Stops mainloop()

Raises:
ValueError: When called without calling mainloop first.
remove(sprite)

Removes sprite from list of objects on screen.

Args:
sprite: The sprite to remove.
class superwires.games.Sprite(image, angle=0, x=0, y=0, top=None, bottom=None, left=None, right=None, dx=0, dy=0, interval=1, is_collideable=True)

A Sprite object to put on the screen.

This class is used to place objects on the screen. You can use this directly, make a your own subclass or use one of the subclasses made for you:Text, Animation, etc. Position of the object is measured by its center (and not its upper-left corner).

Attributes:
image: the image that is displayed on the screen. This is the
image before rotating.

angle: the amount in degrees to rotate the image. x: The x position of the center of the sprite.

The x value increases moving left to right on the screen.
y: The y position of the center of the sprite.
The y value increases moving TOP TO BOTTOM (and not bottom to top as in the cartesian co-ordinate system).

top: The y position of the top edge of the sprite. bottom: The y position of the bottom edge of the sprite. left: The x position of the left edge of the sprite. right: The x position of the right edge of the sprite. position: A two-element tuple with the x and y position of the (center

of the) sprite.

dx: delta x is the x velocity of the sprite. dy: delta y is the y velocity of the sprite. velocity: A two-element tuple containing the dx and dy of the sprite. is_collideable: Whether or not the sprite participates in collisions. interval: The number of frames between tick() calls.

destroy()

Destroys sprite.

get_overlapping_sprites()

Returns list of other sprites overlapping this sprite.

overlapping_sprites

Returns list of other sprites overlapping this sprite.

overlaps(other)

Tells if other sprite overlaps this sprite

tick()

Similar to update() but different.

Serves the exact same purpose as update() except it is called every interval frames.

update()

A function that is usually overridden.

This is called every mainloop() cycle and does nothing by default. It is overridden in subclasses to do what the user wants.

class superwires.games.Text(value, size, color, angle=0, x=0, y=0, top=None, bottom=None, left=None, right=None, dx=0, dy=0, interval=1, is_collideable=True)

A Sprite which displays text.

A GUI label with all the properties of a sprite.

Attributes:

value: The text to display. size: The height of the text in pixels. color: The color of the text (can be specified by using the color module

or an RGB tuple).
superwires.games.init(screen_width, screen_height, fps)

Initializes screen, keyboard, mouse and music.

Its necessary to initialize these global variables before anything is done.

Args:
screen_width: The desired width of the screen. screen_height: The desired height of the screen. fps: The desired Frames Per Second for the screen.
superwires.games.load_image(filename, transparent=True)

Returns Image object from the file filename.

Args:

filename: File containing image. transparent: If true, all pixels with upper-right-most

pixel color will become 100% transparent.
Returns:
An image object to use with Sprites, etc.
superwires.games.load_sound(filename)

Returns Sound object from the file filename.

Args:
filename: File containing sound.
Returns:
A sound object to play, etc.
superwires.games.scale_image(image, x_scale, y_scale=None)

Scales image by the given factors. Args:

x_scale: Width scaling factor. y_scale: Height scaling factor. If y_scale not given then uses x_scale

as height scaling factor.
Returns:
A scaled copy of the image. *DOES NOT AFFECT ORGINAL IMAGE*

Previous topic

Welcome to Superwires’s documentation!

Next topic

The Colors Module

This Page