Skip to content

Instantly share code, notes, and snippets.

@Artanis
Forked from naengwen/startpoint.py
Created August 11, 2009 00:21
Show Gist options
  • Save Artanis/165523 to your computer and use it in GitHub Desktop.
Save Artanis/165523 to your computer and use it in GitHub Desktop.
# This doesn't really do anything outside of post a text label at the
# lower left hand corner of #the screen right now.
from pyglet import window, font
class as_pyglet_text(object):
""" Decorator class
Modifies a function that returns a value (or tuple of values,)
to output to a pyglet window via a font.Text object.
"""
def __init__(self, text, location, font="Arial", size=12):
""" How to modify the decorated function
Arguments:
text -- A text string the value(s) will be inserted into
location -- A tuple of coordinates. Origin in bottom-left of screen
font -- A string denoting the font to use (default "Arial")
size -- The point size of the string (default 12)
"""
self.text = text
self.x, self.y = location
self.font = font
self.size = size
def __call__(self, fn):
""" The actual decorating happens here
"""
# Create the new function
def fx(*args):
# Get the original function's results, passing in
# the original arguments
values = fn(*args)
# Build the font.Text object from the arguments provided in
# the decorator call and the results of the original function
font_style = font.load(self.font, self.size)
text = font.Text(font_style, x=self.x, y=self.y)
text.text = self.text % values
# Draw to screen
text.draw()
# Return the original function results just in case
return values
return fx
class Damageable(object):
""" Provides methods and properties suited to an object that can be damaged
"""
def __init__(self, health, armor):
""" Maximum hitpoints and armor values
"""
self.health = health
self.armor = armor
def hurt(self, damage, pierce):
""" Alters hitpoints by damage, armor values, and armor
piercing capability
Arguments:
damage -- The amount of damage being applied
pierce -- The amount of armor the damage ignores
"""
self.health -= damage + armor - pierce
class Player(Damageable):
""" A basic player representation
"""
def __init__(self, hp, armor):
""" Creates a basic player from the provided values
Arguments:
hp -- The hitpoints of the player
armor -- The armor value of the player
"""
Damageable.__init__(self, hp, armor)
@as_pyglet_text("Health: %d", (50,10), "Arial", 28)
def display_hp(self):
""" Returns the player's health.
Use a decorator to choose the display method.
"""
return self.health
class GameWindow (window.Window):
""" Contains the game window and main logic loop
"""
def __init__(self, *args, **kwargs):
""" Sets up the pyglet window
"""
window.Window.__init__(self, *args, **kwargs)
def game_loop(self):
""" The main logic loop
"""
# Create Player
player = Player(100,1)
# here's the loop
while not self.has_exit:
self.dispatch_events()
self.clear()
# Useful stat is checked
player.display_hp()
self.flip()
if __name__ == "__main__":
# when someone launches this directly; Don't run this when simply imported
game = GameWindow()
game.game_loop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment