predigame package

Submodules

predigame.Sprite module

class predigame.Sprite.Sprite(surface, rect, tag=None, abortable=False, name=None)

Bases: object

The Predigame Sprite - a generic two-dimensional object that is integrated with other sprites in a larger scene. A sprite can consist of a bitmap (still) image or a basic geometrical shape (circle, rectangle, ellipse). Sprites in Predigame have some fun properties - they can be clicked, collide with other sprites, even fade, spin or pulse.

angle

get the current rotated angle of the sprite

Returns:display angle in degrees
bouncy()

make this sprite a little bouncy. the speed attribute will control the speed of movement.

clicked(callback, button=1)

register a callback function for when this sprite is clicked with a mouse button.

Parameters:
  • callback – the callback function to invoke when a mouse click event is detected on this sprite.
  • button – the mouse button to register for click events. options are left button 1 (default), right button 3 or middle button/track wheel 2.
Todo:

confirm that collides are bi-directional events.

collides(sprites, callback)

register a callback function for when this sprite collides with another sprite. collision checks will occur as part of each update invocation.

Parameters:
  • sprites – one (single object) or more (a list) of sprites to check for collisions.
  • callback – the callback function to invoke when a collision is detected.
Todo:

confirm that collides are bi-directional events.

destroy(*args)

destroy this sprite and remove from the game canvas.

destruct(time=1, **kwargs)

self destruct this sprite after time seconds

Parameters:
  • time – the amount of time (in seconds) to stay alive before self destructing.
  • callback – optional callback to execute after time is up (you’ll need to do your own destructing)
fade(time=1, **kwargs)

make this sprite fade away and automatically self-destruct. this can be slow to run on some computers.

Parameters:time – the amount of time (in seconds) to run the fade effect.
Todo:the final effect, like destroy should be configurable
flip(flip_x=True, flip_y=False)

flip the sprite image along x axis - left/right (default) or y axis - up/down.

Parameters:
  • flip_x – flip the sprite along the x axis. default is True.
  • flip_y – flip the sprite along the y axis. default is False.
float(distance=0.25)

make the sprite float around on the screen. this provides a simple movement animation.

Parameters:distance – the amount of grid cell distance a sprite should float. default is 0.25.
follow()

tell the sprite to follow the mouse cursor as it moves around the screen

height

get the width of a sprite in terms of the grid block size.

keys(right='right', left='left', up='up', down='down', **kwargs)

make this sprite move to keyboard events. by default movements will be directional, by grid location - meaning that the sprite will move up, down, left, or right by one (default) grid cell.

Parameters:
  • right – the keyboard button for right direction movement. default is the right arrow.
  • left – the keyboard button for left direction movement. default is the left arrow.
  • up – the keyboard button for up direction movement. default is the up arrow.
  • down – the keyboard button for down direction movement. default is the down arrow.
  • spaces – the number of grid cell. to move for each keyboard event. default is 1 grid cell.
  • precondition – an optional callback function to invoke prior to a movement. preconditions all the sprite to avoid making a certain move (e.g. to avoid a wall)
  • direction – the direction of the keyboard button and when to fire the event event. options are down for keyboard push down events or up for when the keyboard button is released. default is down.
move(vector, **kwargs)

move this sprite along a particular vector to the next grid cell (x and/or y dimensions)

Parameters:
  • vector – the movement vector (x, y). x or y can take the values 0 (no movement), 1 (right or down movement), -1 (up or left movement). For example a vector of (1, 0) will move the sprite right and a vector of (1, -1) will move the sprite up and right.
  • callback – an optional callback function to invoke when movement is complete.
  • precondition – an optional callback function to invoke prior to a movement. preconditions all the sprite to avoid making a certain move (e.g. to avoid a wall)
move_to(*points, **kwargs)

move the sprite to a given point or set of points. movement will be set according to the speed of the sprite.

Parameters:
  • points – one or more positions (x, y) pairs.
  • callback – an optional callback function to invoke when movement is complete.

Example:

s.move_to((0,0), (5,5), (10, 10), callback=s.destroy)

will move the sprite to position (0, 0) then (5,5) then (10,10) and then destroy itself.

pixelate(pixel_size=10)

a simple effect for blurring or pixelating a sprite.

Parameters:pixel_size – the size of the pixels. default is 10.
pos

get the x and y positions as a tuple (pair of numbers). this is basically the same as calling Sprite.x and Spite.y separately.

Returns:a position tuple (x, y)
pulse(time=1, size=None)

add a pulsing effect. make the sprite simultaneously increase then decrease in size.

Parameters:
  • time – the amount of time (in seconds) to complete a pulse. default is 1 second.
  • size – the maximum size to pulse. default is to pulse until the size is doubled and then contract back to original size.
resize(value)

similar to Sprite.size() but callable as a method.

rotate(angle)

rotate the sprite by some angle (in degrees)

Parameters:angle – the directional angle (in degrees) to rotate this sprite.
scale(size)

scale (increase or decrease) the size of the sprite. applies to both with and height.

Parameters:size – multiplier for modifying sprite scale. if greater than 1 sprite will increase in scale. if less than one (a decimal) sprite will decrease in case. if value is 1 sprite will left intact.
Todo:scale needs to be applied separately to x and y axis.
size

get the size of the sprite. sprites have a default size of 1 grid block. they can be increased or decreased as desired. the Sprite.pulse() effect will call size many times to create the desired effect.

Todo:this method only returns the size along the x dimension. sprites should maintain separate sizes, one for each dimension.
speed(speed=5)

set the speed of the sprite for movement actions. by default the sprite will move five grid cells per second.

Parameters:speed – the speed for movement actions. default is 5.
spin(time=1, rotations=None, **kwargs)

make this sprite spin.

Parameters:
  • time – the amount of time (in seconds) to complete a revolution. default is 1 second.
  • rotations – the amount of time (in seconds) to run the spin effect. default will run forever.
  • callback – a function to call back when done spinning (rotations must be specified)
tag

sprites can be “tagged” with extra information. for example, many zombie sprites can all have the singular tag “zombie”. :return: the tag of the sprite (can be None if no tag provided)

wander(callback, time=1)

turn your sprite into a bot by allowing the sprite to wander around. how the sprite “wanders” is based on the callback function.

Parameters:
  • callback – the callback function to determine how the sprite should wander. this function should call a move operation.
  • time – the frequency (in seconds) in order to call the wander function. default is 1 second.
width

get the width of a sprite in terms of the grid block size.

x

get the x (left/right) position of the sprite

Returns:the x position of the sprite as an integer
y

get the y (up/down) position of the sprite

Returns:the y position of the sprite as an integer

predigame.Actor module

class predigame.Actor.Actor(actions, rect, tag=None, abortable=False, name=None)

Bases: predigame.Sprite.Sprite

MAX_WEALTH = 500000
act(action, loop=-1)
actit(action, loop=-1)
defend
dump_state()

extract wealth, health, energy, inventory

energy
facing(distance=100)

returns a position (off the screen) where this actor is facing

get_actions()

return all actions that this actor can perform

health
jump(height=None)
kill(delay=1.25)

used to kill this actor

load_state(state)

load saved state values

move(vector, **kwargs)

move this sprite along a particular vector to the next grid cell (x and/or y dimensions)

Parameters:
  • vector – the movement vector (x, y). x or y can take the values 0 (no movement), 1 (right or down movement), -1 (up or left movement). For example a vector of (1, 0) will move the sprite right and a vector of (1, -1) will move the sprite up and right.
  • callback – an optional callback function to invoke when movement is complete.
  • precondition – an optional callback function to invoke prior to a movement. preconditions all the sprite to avoid making a certain move (e.g. to avoid a wall)
move_to(*points, **kwargs)

move the sprite to a given point or set of points. movement will be set according to the speed of the sprite.

Parameters:
  • points – one or more positions (x, y) pairs.
  • callback – an optional callback function to invoke when movement is complete.

Example:

s.move_to((0,0), (5,5), (10, 10), callback=s.destroy)

will move the sprite to position (0, 0) then (5,5) then (10,10) and then destroy itself.

next(direction)

the next position (in the current direction or relative board position)

next_object(direction=None, distance=50)

returns the next thing along the path where this actor is facing, up to a certain distance. if the next thing is an Actor, it must be alive.

rate(frame_rate)

the rate to swap animation frames, default is 1 per update call

stop()
take(obj)

take this object and add to inventory

wealth

predigame.constants module

predigame.constants.AQUA = (0, 255, 255)
predigame.constants.BLACK = (0, 0, 0)
predigame.constants.BLUE = (0, 0, 255)
predigame.constants.CIRCLE = 'circle'
predigame.constants.CYAN = (0, 200, 200)
predigame.constants.ELLIPSE = 'ellipse'
predigame.constants.GRAY = (128, 128, 128)
predigame.constants.GREEN = (0, 128, 0)
predigame.constants.LIME = (0, 255, 0)
predigame.constants.MAROON = (128, 0, 0)
predigame.constants.NAVY = (0, 0, 128)
predigame.constants.OLIVE = (128, 128, 0)
predigame.constants.ORANGE = (255, 165, 0)
predigame.constants.PINK = (255, 0, 255)
predigame.constants.PURPLE = (128, 0, 128)
predigame.constants.RECT = 'rect'
predigame.constants.RED = (255, 0, 0)
predigame.constants.SILVER = (192, 192, 192)
predigame.constants.TEAL = (0, 128, 128)
predigame.constants.WHITE = (255, 255, 255)
predigame.constants.YELLOW = (255, 255, 0)

predigame.predigame module

predigame.predigame.actor(name=None, pos=None, center=None, size=1, abortable=False, tag='')
predigame.predigame.background(bg=None)

set the background color or image

predigame.predigame.callback(function, wait, repeat=0)

register a time based callback function

Parameters:
  • function – pointer to a callback function
  • wait – the amount of time to wait for the callback to execute.
  • repeat – the number of times this callback should repeat (default 0)
predigame.predigame.destroyall()
predigame.predigame.display(eventkey, name, wrapper=None)

create a new pygame drawing surface that is triggered when eventkey is pressed. any active game is paused when the display is swapped

predigame.predigame.gameover(delay=0.5)

end the current game

Parameters:delay – the amount of time to wait before ending
predigame.predigame.garbagecollect()
predigame.predigame.grid()

show the grid cells on the game canvas

predigame.predigame.image(name=None, pos=None, center=None, size=1, tag='')
predigame.predigame.init(path, width=800, height=800, title='Predigame', bg=(220, 220, 220), fullscreen=False, **kwargs)
predigame.predigame.level(_level)

create a game with levels

predigame.predigame.main_loop()
predigame.predigame.maze(name=None, callback=None)
predigame.predigame.pause()
predigame.predigame.quit()
predigame.predigame.reset(**kwargs)
predigame.predigame.reset_score(**kwargs)

forces a reset for a given scoreboard element

Parameters:pos – the corner position of the scoreboard. Default is UPPER_LEFT. Options inclue UPPER_RIGHT, LOWER_LEFT, and LOWER_RIGHT.
predigame.predigame.resume()
predigame.predigame.score(value=0, **kwargs)

Predigame scoring functions. Any game may have four separate scoreboards on the game - one in each corner. NOTE: all parameters, besides value, are only applied at scoreboard construction time.

Parameters:
  • value – some scoring value (default is 0). the semantics of the value depends on the scoring method
  • pos – the corner position of the scoreboard. Default is UPPER_LEFT. Options inclue UPPER_RIGHT, LOWER_LEFT, and LOWER_RIGHT.
  • color – the color of the scoring block (default is (25, 25, 25)).
  • size – the size (in grid cells) of the scoreboard text (default is 0.75).
  • method – the type of scoreboard to create. options include ACCUMULATE (value added/subtracted to score), VALUE (simply display the current value), TIMER (count time as defined by step)
  • step – applies to method=TIMER and sets the operation of the timer. Default is -1 (count up by seconds).
  • goal – applies to method=TIMER. a goal metric of the scoreboard. If the goal is reached a callback will be invoked.
  • prefix – optional text that can be provided to the start of the scoreboard.
  • callback – optional callback to invoke when the method=TIMER reaches a goal.
predigame.predigame.screenshot(directory='screenshots', filename=None)
predigame.predigame.shape(shape=None, color=None, pos=None, size=(1, 1), tag='', **kwargs)
predigame.predigame.sound(name=None, plays=1, duration=0)

play a sound (wav or ogg file). Sounds must be stored in the sounds/ directory.

Parameters:
  • name – the name of the sound to play
  • plays – the number of times to play the sound (default is 1)
  • duration – the amount of time (in seconds) to play the sound clip (default plays the entire clip)
predigame.predigame.stopwatch(value=0, goal=999, pos=2, color=(0, 0, 0), prefix='Duration: ', callback=None)
predigame.predigame.text(string, color=None, pos=None, size=1, tag='')

draw a text sprite

Parameters:
  • string – the text to display
  • color – the color to use to display (default is to select a random color)
  • pos – the position of the sprite in grid coordinates (default is on center of game canvas)
  • size – the size of the text font (default is 1)
predigame.predigame.time()

returns the time (in seconds) since the start of the game

predigame.predigame.timer(value=30, pos=3, color=(0, 0, 0), prefix='Time Remaining: ', callback=None)

predigame.utils module

predigame.utils.animate(obj, duration=1, callback=None, abortable=False, action=None, **kwargs)
predigame.utils.at(pos)
predigame.utils.distance(p1, p2)
predigame.utils.fill(obj, prob=1, collide_obj=None, collide_callback=None)

fills some amount white space with an object. object can be set to collide with something (collide_obj) in which case a callback would be invoked (collide_callback).

Parameters:
  • prob – the probability of filling a given square with an object.
  • obj – a callback (or partial) to a sprite to create in whitespace.
  • collide_obj – an object or (list of objects) to check for collisions
  • collide_callback – a callback function to invoke when collide_obj collides with obj.
predigame.utils.get(name)
predigame.utils.get_animation(obj)
predigame.utils.graze(sprite)

a sprite.wander() operation. randomly move around

predigame.utils.has_animation(obj, action=0)
predigame.utils.has_tag(lst, tag)

checks if a list contains a sprite with a given tag

predigame.utils.import_plugin(plugin_file)

allows other ‘plugin’ code to be imported into a game. this works a little like load module

predigame.utils.is_wall(pos)

returns true if a wall is at the desired location, false otherwise

predigame.utils.load_module(path, api)
predigame.utils.load_state(actor, sfile)

save the state of an actor in an encoded file

Parameters:
  • actor – actor class reference to load state into
  • sfile – encoded file containing state information
predigame.utils.player_physics(action, sprite, vector)

simple physical model that keep a player from walking into obstacles

predigame.utils.rand_arc()
predigame.utils.rand_color()
predigame.utils.rand_maze(callback)
predigame.utils.rand_pos(x_padding=0, y_padding=0, empty=False)
predigame.utils.randrange_float(start, stop, step)
predigame.utils.register_cell(pos, s)

helper function that builds the index of all sprites in a given cell

predigame.utils.register_keydown(key, callback)
predigame.utils.register_keyup(key, callback)
predigame.utils.roundup(num, step)
predigame.utils.save_state(actor, sfile)

loads previously saved actor state from encoded file

Parameters:
  • actor – actor class to dump state from
  • sfile – encoded file used to write state information
predigame.utils.score_pos(pos=0)

return the grid position of the score sprite

predigame.utils.sign(num)
predigame.utils.sprites()

return a list of all loaded sprites

predigame.utils.to_area(x, y, w, h)

takes a upper_left point, width and height and returns full covering area (as list)

predigame.utils.to_grid(screen_pos)

converts screen coordinates to grid coordinates

predigame.utils.track(sprite, find_tags, pbad=0.1)

a sprite.wander() operation. attempt to a path that moves sprite closer to player_sprite.

Parameters:
  • sprite – the sprite to automate movements.
  • find_tags – the tags to track
  • pbad – the probability to make a bad move. some number of bad moves are needed to
predigame.utils.track_astar(sprite, find_tags, pabort=-1)
predigame.utils.visible(p1)

Module contents

predigame.bootstrap()
predigame.err()
predigame.get_games()
predigame.main()
predigame.new_game()
predigame.pull_game()