gummworld2.hudlight
index
c:\cygwin64\home\bw\dev\python\dist\gummworld2\gamelib\gummworld2\hudlight.py

hudlight.py - a basic HUD with a lot of conveniences
 
hud = HUD()
 
hud.add('fps', '{} fps', 0)
hud.update_item('fps', int(clock.get_fps()))
 
hud.add('date', '{} {}', time, date)
hud.update_item('date', time, date)
 
hud.add('mouse', '{0[0]}, {0[1]}', (0, 0))
hud.update_item('mouse', pygame.mouse.get_pos())
 
hud.draw(pygame.display.get_surface())
 
See demo1-so-easy.py for an introductory usage; demo2-casting-a-shadow.py for slightly more complex.
 
Despite the seeming simplicity of the above examples, one can do quite a lot with this HUD. See demo3-all-features.py
for a very elaborate example. But keep in mind that demo3 is not typical of HUD implementations. It is very elaborate
because the HUD is the interactive GUI which displays the real-time values for all the HUD features. This demo3 is
primarily an interactive runtime tester, so it lets you play with all the features.

 
Modules
       
collections
pygame
gummworld2.pygametext

 
Classes
       
__builtin__.object
HUD
exceptions.BaseException(__builtin__.object)
HUDBadArgs
HUDNameExists
HUDNameNotFound

 
class HUD(__builtin__.object)
    HUD class for a basic Heads Up Display
 
Not supported:
    - Per-item details (font, size, style, antialias, color).
 
Tips and tricks:
    Color arguments may be string names, color sequences, or pygame.Color.
    
    Note the difference between set_color(None) and set_background(None). The former is ignored. The latter
    has the side effect of making the background transparent.
 
    set_bold() and set_italic() are software rendering effects. If bold and italic fonts exist they give
    more pleasing results.
 
    If you want per-item details (font, size, style, antialias, color) or want to use HUD in a more robust
    dashboard-like way, you can make multiple HUDs with custom x, y positions. If you want to calculate
    their size for positioning, use the hud_size() method, keeping in mind that changing SysFont styles,
    font file, etc. will change the rendered size. One can use the same trick with the HUD that is often
    done with words: render the longest strings with the widest character (usually "W") and then remember
    the size for layout calculations.
 
    The font items are rendered in the order they are added. Order can be rearranged at any time by
    modifying the hud.order list, which is simply a list of the item names. When modifying hud.order and/or
    hud.items take care to keep the contents one-to-one.
 
  Methods defined here:
__init__(self, fontname=None, fontsize=24, sysfontname='sans', bold=False, italic=False, underline=False, color='gold', background=None, gcolor=None, ocolor=None, scolor=None, owidth=1, shadow=(1, 1), antialias=True, alpha=1.0)
create a basic HUD
 
The defaults are simply sysfont_name='sans', fontsize=16, antialias=True.
 
Positioning is anchored at topleft. Modify via x and y attribute. The default is 10, 10.
 
Vertical spacing for items is 3 pixels by default. Modify via space attribute.
 
:param fontname: the name of a font file
:param fontsize: point size of the font
:param sysfontname: the name of a system font
:param bold: boolean
:param italic: boolean
:param underline: boolean
:param color: text foreground color
:param background: text background color
:param gcolor: text gradient color
:param ocolor: text outline color
:param scolor: text shadow color
:param owidth: int (float?), width in pixels of outline
:param shadow: tuple of int, (x, y) offset for drop shadow
:param antialias: boolean
:param alpha: float, valid values are 0.0 - 1.0
:return: HUD
add(self, name, text, *args, **kwargs)
add a HUD item
 
HUD items are rendered in the order they are added. Order can be rearranged at any time by modifying
hud.order, which is simply a list of the item names.
 
An item can use varargs or kwargs, not both. The choice is enforced when the item is updated.
 
:param name: unique ID
:param text: format text compatible with str.format()
:param args: arguments for str.format()
:param kwargs: optional, callback=func
:return: None
draw(self, surf)
draw all the items
 
:param surf: target surface
:return: None
fps(self)
get_alpha(self)
get_antialias(self)
get_background(self)
get_bold(self)
get_color(self)
get_font(self)
get the current font object
:return: pygame.font.Font
get_fontname(self)
get_fontsize(self)
get_gcolor(self)
get_italic(self)
get_ocolor(self)
get_owidth(self)
get_scolor(self)
get_shadow(self)
get_sysfontname(self)
get_underline(self)
hud_size(self)
get the total rendered size of the HUD (may change depending on style, etc.)
:return: w, h
set_alpha(self, alpha)
change the surface alpha value
 
Valid values are 0.0 to 1.0.
 
:param alpha: int, 0 to 255; if None it is disabled
:return: self
set_antialias(self, boolean)
render antialiased font
:param boolean
:return: self
set_background(self, color)
change background color
 
If color is None, the background will be transparent.
 
:param color: a pygame.Color, color sequence, or name string
:return: self
set_bold(self, boolean)
change the bold style
:param boolean
:return: self
set_color(self, color)
change foreground color
:param color: a pygame.Color, color sequence, or name string
:return: self
set_font(self, fontname=None, fontsize=None, sysfontname=None, bold=None, italic=None, underline=None)
change font
 
If any of the args are not specified the instance variables are used. The instance variables are
updated to reflect the supplied values.
 
Font creation and re-rendering of items is triggered.
 
:param fontname: the name of a font file
:param fontsize: point size of the font
:param sysfontname: the name of a sysfont
:param bold: boolean
:param italic: boolean
:param underline: boolean
:return: self
set_fontname(self, name)
change the font source to a file font
 
:param name: the name of a font file or system font
:return: self
set_fontsize(self, fontsize)
change the font size
:param fontsize: int
:return: self
set_gcolor(self, color=None)
change gradient color
 
If color is None, the gradient effect will be removed.
 
:param color: a pygame.Color, color sequence, or name string
:return: self
set_italic(self, boolean)
change the italic style
:param boolean
:return: self
set_ocolor(self, color=None)
render an outline color
 
If color is None, the outline effect will be removed.
 
:param color: a pygame.Color, color sequence, name string, or None
:return: self
set_owidth(self, width)
set outline width
:param width: int >= 0
:return: self
set_scolor(self, color=None)
render a drop shadow color
 
If color is None, the shadow effect will be removed.
 
:param color: a pygame.Color, color sequence, name string, or None
:return: self
set_shadow(self, shadow)
set the x, y distance in pixels to project the shadow
:param shadow: tuple of two ints; the offset of the shadow; e.g. (1, 1), (0, 1), (-2, -2)
:return: self
set_sysfontname(self, name)
change the font source to a sysfont
 
:param name: the name of a font file or system font
:return: self
set_underline(self, boolean)
change the underline style
:param boolean
:return: None
update(self, *args)
update all items, using the callbacks provided with add()
 
Logic uses the type of the value passed in add() to determine how to update the value.
The order of evaluation is dict, str, sequence, then discrete value.
 
:param args: not used; for compatibility with timers, etc. that supply args by default
:return: None
update_item(self, name, *args, **kwargs)
update the value of an item (does not invoke the callback)
 
If the item was created with varargs, then the item must be updated with varargs. If item was created with
 kwargs, if must be updated with kwargs.
 
:param name: item's name
:param args: item's new value
:return: None

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

 
class HUDBadArgs(exceptions.BaseException)
    HUD update_item() arg count does not match args given to add()
 
 
Method resolution order:
HUDBadArgs
exceptions.BaseException
__builtin__.object

Data descriptors defined here:
__weakref__
list of weak references to the object (if defined)

Methods inherited from exceptions.BaseException:
__delattr__(...)
x.__delattr__('name') <==> del x.name
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__init__(...)
x.__init__(...) initializes x; see help(type(x)) for signature
__reduce__(...)
__repr__(...)
x.__repr__() <==> repr(x)
__setattr__(...)
x.__setattr__('name', value) <==> x.name = value
__setstate__(...)
__str__(...)
x.__str__() <==> str(x)
__unicode__(...)

Data descriptors inherited from exceptions.BaseException:
__dict__
args
message

Data and other attributes inherited from exceptions.BaseException:
__new__ = <built-in method __new__ of type object>
T.__new__(S, ...) -> a new object with type S, a subtype of T

 
class HUDNameExists(exceptions.BaseException)
    HUD non-unique name in items list
 
 
Method resolution order:
HUDNameExists
exceptions.BaseException
__builtin__.object

Data descriptors defined here:
__weakref__
list of weak references to the object (if defined)

Methods inherited from exceptions.BaseException:
__delattr__(...)
x.__delattr__('name') <==> del x.name
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__init__(...)
x.__init__(...) initializes x; see help(type(x)) for signature
__reduce__(...)
__repr__(...)
x.__repr__() <==> repr(x)
__setattr__(...)
x.__setattr__('name', value) <==> x.name = value
__setstate__(...)
__str__(...)
x.__str__() <==> str(x)
__unicode__(...)

Data descriptors inherited from exceptions.BaseException:
__dict__
args
message

Data and other attributes inherited from exceptions.BaseException:
__new__ = <built-in method __new__ of type object>
T.__new__(S, ...) -> a new object with type S, a subtype of T

 
class HUDNameNotFound(exceptions.BaseException)
    HUD name not found in items list
 
 
Method resolution order:
HUDNameNotFound
exceptions.BaseException
__builtin__.object

Data descriptors defined here:
__weakref__
list of weak references to the object (if defined)

Methods inherited from exceptions.BaseException:
__delattr__(...)
x.__delattr__('name') <==> del x.name
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__init__(...)
x.__init__(...) initializes x; see help(type(x)) for signature
__reduce__(...)
__repr__(...)
x.__repr__() <==> repr(x)
__setattr__(...)
x.__setattr__('name', value) <==> x.name = value
__setstate__(...)
__str__(...)
x.__str__() <==> str(x)
__unicode__(...)

Data descriptors inherited from exceptions.BaseException:
__dict__
args
message

Data and other attributes inherited from exceptions.BaseException:
__new__ = <built-in method __new__ of type object>
T.__new__(S, ...) -> a new object with type S, a subtype of T

 
Functions
       
set_font_template(s='%s')
set template using old style string formatting
 
For example, set_font_template("fonts/%s.ttf"). The module default is "%s". 
 
:param s: str
:return:

 
Data
        __all__ = ['HUDNameExists', 'HUDNameNotFound', 'HUDBadArgs', 'HUD', 'set_font_template']