This commit is contained in:
Frank DeMarco 2013-03-05 22:23:17 +09:00
parent 1c70279d3d
commit 796999eb8f
8 changed files with 45 additions and 38 deletions

3
README
View File

@ -21,7 +21,7 @@ class SampleGame(Game):
square_width = 30
# update is a special method that runs once every frame
# update is a special method that runs once every frame
def update(self):
sleep(1)
screen = self.get_screen()
@ -47,6 +47,7 @@ Todo
====
- Remove unecessary python libraries from windows build
- Debug levels
Contact

View File

@ -15,7 +15,7 @@ class Audio(GameChild):
def __init__(self, game):
GameChild.__init__(self, game)
self.load_fx()
self.subscribe_to(self.get_custom_event_id(), self.mute)
self.subscribe(self.mute)
def load_fx(self):
fx = {}

View File

@ -72,7 +72,7 @@ class Configuration(RawConfigParser):
section = "screen-captures"
add_section(section)
set_option(section, "rel-path", "caps")
set_option(section, "file-name-format", "%Y-%m-%d_%H:%M:%S")
set_option(section, "file-name-format", "%Y%m%d%H%M%S")
set_option(section, "file-extension", "png")
section = "mouse"
add_section(section)
@ -94,7 +94,7 @@ class Configuration(RawConfigParser):
section = "event"
add_section(section)
set_option(section, "custom-event-id", "USEREVENT")
set_option(section, "command-event-name", "command")
set_option(section, "custom-event-name", "command")
section = "audio"
add_section(section)
set_option(section, "sfx-path", "aud/fx/")

View File

@ -12,7 +12,7 @@ class Display(GameChild):
self.set_caption()
self.set_icon()
self.set_mouse_visibility()
self.subscribe_to(self.get_custom_event_id(), self.toggle_fullscreen)
self.subscribe(self.get_custom_event_id(), self.toggle_fullscreen)
def set_screen(self, flags=0):
self.screen = display.set_mode(self.config["dimensions"], flags)

View File

@ -27,8 +27,8 @@ class Game(GameChild, Animation):
self.align_window()
pygame.init()
self.set_children()
self.subscribe_to(QUIT, self.end)
self.subscribe_to(self.get_custom_event_id(), self.end)
self.subscribe(self.end, QUIT)
self.subscribe(self.end)
self.clear_queue()
self.delegate.enable()

View File

@ -49,37 +49,50 @@ class GameChild:
self.print_debug("Couldn't find resource: {0}, {1}".\
format(section, option))
def is_shared_mode(self):
return "-s" in argv
def print_debug(self, statement):
if self.is_debug_mode():
print statement
def is_debug_mode(self):
return "-d" in argv
def is_absolute_path(self, path):
return normpath(path) == abspath(path)
def is_shared_mode(self):
return "-s" in argv
def subscribe_to(self, kind, callback):
def subscribe(self, callback, kind=None):
kind = self.resolve_event_type(kind)
self.get_game().delegate.add_subscriber(kind, callback)
def unsubscribe_from(self, kind, callback):
self.get_game().delegate.remove_subscriber(kind, callback)
def resolve_event_type(self, kind):
if kind is None:
kind = self.get_custom_event_id()
return kind
def is_debug_mode(self):
return "-d" in argv
def unsubscribe(self, callback, kind=None):
kind = self.resolve_event_type(kind)
self.get_game().delegate.remove_subscriber(kind, callback)
def get_custom_event_id(self):
return globals()[self.get_configuration().get("event",
"custom-event-id")]
def is_command(self, evt, cmd):
name = self.get_configuration().get("event", "command-event-name")
if not isinstance(cmd, list):
cmd = [cmd]
return evt.type == self.get_custom_event_id() and evt.name == name and \
evt.command in cmd
def is_command(self, evt, command, **attributes):
if not isinstance(command, list):
command = [command]
return evt.type == self.get_custom_event_id() and \
evt.dict[self.get_custom_event_name()] in command and \
self.event_contains(evt, attributes)
def post_command(self, command):
name = self.get_configuration().get("event", "command-event-name")
event.post(event.Event(self.get_custom_event_id(), name=name,
command=command))
def event_contains(self, evt, attributes):
return all(key in evt.dict and evt.dict[key] == value for \
key, value in attributes.iteritems())
def get_custom_event_name(self):
return self.get_configuration("event", "custom-event-name")
def post_command(self, command, **attributes):
attributes[self.get_custom_event_name()] = command
event.post(event.Event(self.get_custom_event_id(), attributes))

View File

@ -16,11 +16,11 @@ class Input(GameChild):
self.build_key_map()
def subscribe_to_events(self):
self.subscribe_to(KEYDOWN, self.translate_key_press)
self.subscribe_to(JOYBUTTONDOWN, self.translate_joy_button)
self.subscribe_to(JOYAXISMOTION, self.translate_axis_motion)
self.subscribe_to(MOUSEBUTTONDOWN, self.translate_mouse_input)
self.subscribe_to(MOUSEBUTTONUP, self.translate_mouse_input)
self.subscribe(self.translate_key_press, KEYDOWN)
self.subscribe(self.translate_joy_button, JOYBUTTONDOWN)
self.subscribe(self.translate_axis_motion, JOYAXISMOTION)
self.subscribe(self.translate_mouse_input, MOUSEBUTTONDOWN)
self.subscribe(self.translate_mouse_input, MOUSEBUTTONUP)
def suppress(self):
self.suppressed = True
@ -47,13 +47,6 @@ class Input(GameChild):
if key in keys:
self.post_command(cmd)
def post_command(self, cmd, **kwargs):
config = self.get_configuration().get_section("event")
eid = self.get_custom_event_id()
self.print_debug("Posting %s command with id %s" % (cmd, eid))
name = config["command-event-name"]
event.post(event.Event(eid, name=name, command=cmd, **kwargs))
def translate_joy_button(self, evt):
if not self.suppressed:
button = evt.button

View File

@ -12,7 +12,7 @@ class ScreenGrabber(GameChild):
def __init__(self, game):
GameChild.__init__(self, game)
self.subscribe_to(self.get_custom_event_id(), self.save_display)
self.subscribe(self.save_display)
def save_display(self, event):
if self.is_command(event, "capture-screen"):