Package pygsear :: Module Util
[show private | hide private]
[frames | no frames]

Module pygsear.Util

Utilities for finding and loading different resources
Exceptions
TextRectException  

Function Summary
  beep()
  color(name, modifier)
  color_darken(color)
  color_lighten(color)
  does_surface_have_pixel_alpha(surface)
  get_dirs(type)
return a list of potential data directories.
  get_full_path(filename, dirs)
return the first existing file found in dirs.
  have_same_signs(a, b)
return True if both integers have the same signs.
  line_seg_intersect(line1point1, line1point2, line2point1, line2point2)
return True if 2 line segments intersect.
  load_image(filename, convert)
Return pygame surface from filename string.
  load_images(filenames, dirname, convert)
Return list of pygame surfaces.
  load_images_dict(filenames, dirname, convert)
Return dict of filename: imagepygame surfaces.
  load_points(filename)
Return list of points (x, y).
  load_sound(filename)
Return pygame sound object.
  render_textrect(string, rect, text_color, bgcolor, fontSize, justification, trim)
Returns a surface containing the passed text.
  scale_image(img, width, height, keepAspectRatio)
return a scaled copy of a pygame.Surface

Variable Summary
tuple BLACK
tuple BLUE
int COLINEAR
tuple COLORS
tuple DBLUE
tuple DGREEN
dict dirs_cache
int DONT_INTERSECT
tuple DRED
tuple GRAY
tuple GREEN
dict image_cache
tuple LBLUE
tuple LGRAY
tuple LGREEN
tuple LRED
tuple ORANGE
float PI
float PIx2
dict point_cache
tuple PURPLE
tuple RED
dict sound_cache
tuple TRANSPARENT
tuple TRANSPARENT2
tuple WHITE
tuple YELLOW

Function Details

get_dirs(type=None)

return a list of potential data directories.

Looks around at possible locations, and returns possible data directories in this order:
  1. The current working directory.
  2. The "home" directory, where the main (invoked) script lives. NOTE: Not the user's home directory!
  3. The data/type directory underneath the "home" directory
  4. The data directory underneath the "home" directory
  5. The libdata/type directory underneath the pygsear lib directory.
  6. The libdata directory underneath the pygsear lib directory.

get_full_path(filename, dirs)

return the first existing file found in dirs.
Parameters:
filename - Name of file to find in dirs.
dirs - List of directories to search.

have_same_signs(a, b)

return True if both integers have the same signs.
Parameters:
a - One number.
           (type=int)
b - Another number.
           (type=int)

line_seg_intersect(line1point1, line1point2, line2point1, line2point2)

return True if 2 line segments intersect.
Parameters:
line1point1 - (x, y) coord of one end of first line
line1point2 - (x, y) coord of other end of first line
line2point1 - (x, y) coord of one end of second line
line2point2 - (x, y) coord of other end of second line

load_image(filename, convert=1)

Return pygame surface from filename string.

Uses get_dirs to know where to look for the file.
Parameters:
filename - Name of image file to load data from.
convert - Optimize if True. Can sometimes cause colorspace problems... Will not convert if the image has an alpha mask.

load_images(filenames=None, dirname=None, convert=1)

Return list of pygame surfaces.

Must pass either list of filenames, or name of directory from which to load all images.

Uses get_dirs to know where to look for the file or directory.
Parameters:
filenames - List of image file names to load data from.
dirname - Name of directory from which to load all images.
convert - Optimize if True. Can sometimes cause colorspace problems...

load_images_dict(filenames=None, dirname=None, convert=1)

Return dict of filename: image pygame surfaces.

Must pass either a list of filenames or the name of a directory from which to load all images.

Uses get_dirs to know where to look for the file or directory.
Parameters:
filenames - List of image file names to load data from.
dirname - Name of directory from which to load all images.
convert - Optimize if True. Can sometimes cause colorspace problems...

load_points(filename)

Return list of points (x, y).
Parameters:
filename - Name of file to load data from. Data should be formatted as (x, y) with one point per line.

load_sound(filename)

Return pygame sound object.

Sound file shoud be a .wav file.

Checks the status of the sound system first, and if there is a problem, hands out pygsear.Sound.DummySound objects instead of actual pygame.mixer.Sound objects.
Parameters:
filename - Name of file to load data from.

render_textrect(string, rect, text_color=(255, 255, 255), bgcolor=(0, 0, 0), fontSize=20, justification=0, trim=0)

Returns a surface containing the passed text.

The text string will be reformatted to fit within the given rect, word-wrapping as necessary. The text will be anti-aliased.

Raises a TextRectException if the text won't fit onto the surface.
Parameters:
string - The text you wish to render. Newline character begins a new line.
rect - A rectstyle giving the size of the surface requested.
text_color - RGB color tuple (ex (0, 0, 0) = BLACK)
bgcolor - A three-byte tuple of the rgb value of the surface.
fontSize - A Font object
justification - Alignment of the text in the rectangle
  • 0 (default) left-justified
  • 1 horizontally centered
  • 2 right-justified
trim - If True, return a Surface just large enough to contain the text.
Returns:
A surface object with the text rendered onto it.

scale_image(img, width, height, keepAspectRatio=1)

return a scaled copy of a pygame.Surface
Parameters:
img - Original pygame.Surface.
width - width of returned surface.
height - height of returned surface.
keepAspectRatio - If True, the returned surface will be padded with transparent borders.

Variable Details

BLACK

Type:
tuple
Value:
(0, 0, 0)                                                              

BLUE

Type:
tuple
Value:
(100, 100, 255)                                                        

COLINEAR

Type:
int
Value:
-1                                                                     

COLORS

Type:
tuple
Value:
((255, 0, 0),
 (100, 100, 255),
 (0, 255, 0),
 (255, 255, 0),
 (255, 127, 0),
 (255, 0, 255),
 (255, 125, 125),
 (175, 175, 255),
...                                                                    

DBLUE

Type:
tuple
Value:
(0, 0, 127)                                                            

DGREEN

Type:
tuple
Value:
(0, 127, 0)                                                            

dirs_cache

Type:
dict
Value:
{}                                                                     

DONT_INTERSECT

Type:
int
Value:
0                                                                      

DRED

Type:
tuple
Value:
(127, 0, 0)                                                            

GRAY

Type:
tuple
Value:
(95, 95, 95)                                                           

GREEN

Type:
tuple
Value:
(0, 255, 0)                                                            

image_cache

Type:
dict
Value:
{}                                                                     

LBLUE

Type:
tuple
Value:
(175, 175, 255)                                                        

LGRAY

Type:
tuple
Value:
(195, 195, 195)                                                        

LGREEN

Type:
tuple
Value:
(125, 255, 125)                                                        

LRED

Type:
tuple
Value:
(255, 125, 125)                                                        

ORANGE

Type:
tuple
Value:
(255, 127, 0)                                                          

PI

Type:
float
Value:
3.1415926535897931                                                     

PIx2

Type:
float
Value:
6.2831853071795862                                                     

point_cache

Type:
dict
Value:
{}                                                                     

PURPLE

Type:
tuple
Value:
(255, 0, 255)                                                          

RED

Type:
tuple
Value:
(255, 0, 0)                                                            

sound_cache

Type:
dict
Value:
{}                                                                     

TRANSPARENT

Type:
tuple
Value:
(0, 51, 0, 0)                                                          

TRANSPARENT2

Type:
tuple
Value:
(0, 243, 0)                                                            

WHITE

Type:
tuple
Value:
(255, 255, 255)                                                        

YELLOW

Type:
tuple
Value:
(255, 255, 0)                                                          

Generated by Epydoc 1.1 on Sun Jan 25 19:10:10 2004 http://epydoc.sf.net