gummworld2.toolkit (version $Id: toolkit.py 418 2013-08-19 14:38:28Z stabbingfinger@gmail.com $)
index
c:\cygwin64\home\bw\dev\python\dist\gummworld2\gamelib\gummworld2\toolkit.py

toolkit.py - Some helper tools for Gummworld2.

 
Modules
       
gummworld2.data
gummworld2.hudlight
os
pygame
re
sys
urllib

 
Classes
       
__builtin__.object
Struct
Tilesheet

 
class Struct(__builtin__.object)
    A class with arbitrary members. Construct it like a dict, then access the
keys as instance attributes.
 
s = Struct(x=2, y=4)
print s.x,s.y
 
  Methods defined here:
__init__(self, **kwargs)

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class Tilesheet(__builtin__.object)
     Methods defined here:
__init__(self, file_path, image, margin, tile_size, spacing, rects)
get_image(self, tile_id)
tile_info(self, tile_id)
Return a Struct populated with tilesheet info for tile_id. This info
represents everything needed by the Tile class constructor in
world_editor.py.
 
Struct members:
    name : str; relative path to image file
    image : pygame.surface.Surface; the loaded image
    margin : Vec2d; size of image's edge border
    size : Vec2d; size of a single tile
    spacing : Vec2d; spacing between tiles
    rects : pygame.Rect; list of rects defining tile subsurfaces

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
Functions
       
draw_grid(grid_cache, layeri=0, color=(0, 0, 255, 255), alpha=33)
Draw a grid over the camera view.
 
The grid_lines argument is a list of length two containing a cached
[vline,hline] pair. This function will initialize the contents if the
starting values evaluate to False (e.g. [0,0]).
 
The layeri argument is the map layer from which to get the grid spacing.
 
The color argument is the desired grid color.
 
The alpha argument is the surface alpha. If alpha is not None, it must be a
valid value for pygame.Surface.set_alpha().
 
color and alpha are only used when when creating the surfaces for the
grid_lines list.
draw_labels(cache_dict, layeri=0, color=(0, 0, 0, 255))
draw_object_array(object_array)
Draw a layered array of objects.
 
For best performance:
    
def update(self, dt):
    self.visible_objects = toolkit.get_object_array()
def draw(self, dt):
    ...
    toolkit.draw_object_array(self.visible_objects)
draw_parallax_tile_range(layer, tile_range, pax_rect)
# EXPERIMENTAL: not working quite right
draw_parallax_tiles(maps, view)
maps is a list of maps
 
Assumptions:
    1. Maps are in world coords.
    2. Each map has the same number of layers.
    3. Each map's corresponding layer has the same parallax value.
    4. Each map layer has a parallax attribute: tuple of length 2.
    5. Each map layer has a tile_range attribute: range of visible tiles.
draw_parallax_tiles_of_layer(cam, map, layer, parallax=(1.0, 1.0))
# EXPERIMENTAL: not working quite right
draw_sprite(s, blit_flags=0)
Draw a sprite on the camera's surface using world-to-screen conversion.
draw_tiles()
Draw visible tiles.
 
Quick and dirty draw function, handles getting the objects and drawing
them. This is a bit more expensive than getting tiles during the update
cycle and reusing the object array for the draw cycles. However, it is
less hassle, and one might want to use this tactic if panning more than
one tile span per tick.
draw_tiles_of_layer(layeri, pallax_factor_x=1.0, pallax_factor_y=1.0)
Draw visible tiles.
 
This function assumes that the tiles stored in the map are sprites.
get_object_array(max_speed=10)
Return a list of the map's objects that would be visible to the camera.
Operates on State.map and State.camera.
 
The max_speed argument adds this many pixels to each edge of the query rect
to accommodate for the space moved during frame interpolation cycles. This
should at least match the scrolling speed to avoid black flickers at the
screen edges.
 
The return value is a list of object lists:
    [   [obj0, obj1, ...],          # layer0
        [obj0, obj1, ...],          # layer1
        ...,                        # layerN
    ]
get_objects_in_cell_ids(map_, cell_ids_per_layer)
Return a list of objects per layer for the specified cell IDs.
This function is mainly for SuperMap, which needs to specify the map.
 
The argument cell_ids_per_layer is a list of nested lists containing
cell IDs:
    [   [cell_id0, cell_id1, ...],  # layer0
        [cell_id0, cell_id1, ...],  # layer1
        ...,                        # layerN
    ]
 
The return value is a similary constructed list of object lists:
    [   [obj0, obj1, ...],          # layer0
        [obj0, obj1, ...],          # layer1
        ...,                        # layerN
    ]
get_parallax_tile_range(cam, map, layer, parallax, orig='center')
# EXPERIMENTAL: not working quite right
#def get_parallax_tile_range(cam, map, layer, parallax, orig='bottomleft'):
get_visible_cell_ids(camera, map_, max_speed=10)
Return a list of the map's cell IDs that would be visible to the camera.
This function is mainly for SuperMap, which needs to specify the map.
 
The camera argument is the camera that defines the view.
 
The map_ argument is the BasicMap or TiledMap to query.
 
The max_speed argument adds this many pixels to each edge of the query rect
to accommodate for the space moved during frame interpolation cycles. This
should at least match the scrolling speed to avoid black flickers at the
screen edge.
 
The return value is a list of cell IDs in the map's spatialhash for each
layer. The per-layer values are necessary because maps can have layers with
different tile sizes, and therefore different grids.
 
The return value is suitable for passing to get_objects_in_cell_ids().
 
Returns:
    [   [cell_id0, cell_id1, ...],  # layer0
        [cell_id0, cell_id1, ...],  # layer1
        ...,                        # layerN
    ]
interpolated_step(pos, step, interp)
Returns (float,float).
 
This is a utility for drawing routines, after pos has been updated by step.
For example:
    def update():
        move_camera()
        State.camera.update()
        sprite_group.update()  # e.g. steps a sprite position
    def draw():
        interp = State.camera.interpolate()
        camera_pos = State.camera.rect.topleft
        for s in sprite_group:
            sprite_pos = Vec2d(s.rect.topleft)
            pos = sprite_pos - camera_pos
            step = s.step
            interp_pos = toolkit.interpolated_step(pos, step, interp)
            surf.blit(s.image, interp_pos)
make_default_hud(caption=None, theme=None, **custom_args)
factory to make a hudlight.HUD with HUD_THEMES and custom themes
 
This factory supports canned HUD_THEMES, custom themes, and permits merging the two.
 
Logic
    - The starting template is chosen by the theme arg. If theme is invalid, the starting template will be blank.
    - The starting template will be updated with the custom args, if any.
    - If no valid theme or custom args were supplied, the 'default' theme will be used.
 
For all the valid args, see class hudlight.HUD. Also see module pygametext.
 
Tip: You can also modify a HUD via its many style methods once it is created.
 
Examples
 
    Default theme:
        make_default_hud()
        make_default_hud(theme='default')
    Custom theme:
        make_default_hud(font_name='fonts/Roboto_Condensed.ttf')
        make_default_hud(sysfont_name='sans')
 
:param caption: optional static caption
:param theme: dict containing keyword args for the HUD constructor; if None, HUD_THEMES['default'] is used
:return: hudlight.HUD
make_tiles(label=False)
Create tiles to fill the current map. This is a utility for easily making
visible content to aid early game design or debugging.
 
Tiles transition from top-left to bottom-right, red to blue.
make_tiles2()
Create tiles to fill the current map. This is a utility for easily making
visible content to aid early game design or debugging.
 
Tiles transition from top to bottom, light blue to brown.

 
Data
        HUD_THEMES = {'boogaloo': {'color': 'red2', 'fontname': r'C:\cygwin64\home\bw\dev\python\dist\gummworld2\data\font\Boogaloo.ttf', 'fontsize': 26, 'gcolor': 'deepskyblue', 'ocolor': 'yellow'}, 'bubblegum': {'color': 'red2', 'fontname': r'C:\cygwin64\home\bw\dev\python\dist\gummworld2\data\font\Bubblegum_Sans.ttf', 'fontsize': 25, 'gcolor': 'orange1', 'ocolor': 'yellow'}, 'cherrycreamsoda': {'color': 'orange2', 'fontname': r'C:\cygwin64\home\bw\dev\python\dist\gummworld2\data\font\CherryCreamSoda.ttf', 'fontsize': 25, 'gcolor': 'mediumpurple4', 'ocolor': 'mediumpurple'}, 'default': {'color': 'red2', 'fontname': r'C:\cygwin64\home\bw\dev\python\dist\gummworld2\data\font\Boogaloo.ttf', 'fontsize': 26, 'gcolor': 'deepskyblue', 'ocolor': 'yellow'}, 'roboto': {'color': 'cornflowerblue', 'fontname': r'C:\cygwin64\home\bw\dev\python\dist\gummworld2\data\font\Roboto_Condensed.ttf', 'fontsize': 24, 'gcolor': 'lightyellow', 'ocolor': 'black'}, 'vera': {'color': 'lightblue', 'fontname': r'C:\cygwin64\home\bw\dev\python\dist\gummworld2\data\font\Vera.ttf', 'fontsize': 18, 'gcolor': 'green4', 'scolor': 'grey20', 'shadow': (-1, 1)}, 'verabd': {'color': 'red2', 'fontname': r'C:\cygwin64\home\bw\dev\python\dist\gummworld2\data\font\VeraBD.ttf', 'fontsize': 25, 'gcolor': 'gold', 'scolor': 'grey20'}}
IMAGE_FILE_EXTENSIONS = ('gif', 'png', 'jpg', 'jpeg', 'bmp', 'pcx', 'tga', 'tif', 'lbm', 'pbm', 'pgm', 'xpm')
__all__ = ['IMAGE_FILE_EXTENSIONS', 'Struct', 'Tilesheet', 'make_default_hud', 'HUD_THEMES', 'make_tiles', 'make_tiles2', 'get_visible_cell_ids', 'get_objects_in_cell_ids', 'get_object_array', 'draw_object_array', 'draw_sprite', 'draw_tiles', 'interpolated_step', 'get_parallax_tile_range', 'draw_parallax_tile_range', 'draw_parallax_tiles', 'draw_parallax_tiles_of_layer', 'draw_tiles_of_layer', 'draw_labels', ...]
__author__ = 'Gummbum, (c) 2011-2014'
__version__ = '$Id: toolkit.py 418 2013-08-19 14:38:28Z stabbingfinger@gmail.com $'
label_font = None

 
Author
        Gummbum, (c) 2011-2014