TypeDeclarations reads declarations from dict

This commit is contained in:
Frank DeMarco 2012-07-31 14:40:31 -04:00
parent 2578195944
commit 3bc51f264d
3 changed files with 32 additions and 25 deletions

6
README
View File

@ -8,7 +8,9 @@ Classes that facilitate the creation of Pygame projects
Example
=======
Create a project which draws a square every second:
Save and run the following script to create a project that redraws a square at a
random location every second. This script may also be found in the `sample.py`
file.
from time import sleep
from random import randint
@ -20,7 +22,7 @@ class SampleGame(Game):
square_width = 30
# update runs every frame, you can think of it as the mainloop
# update runs every frame (a.k.a. the mainloop)
def update(self):
sleep(1)
screen = self.get_screen()

View File

@ -215,29 +215,31 @@ class Configuration(RawConfigParser):
class TypeDeclarations(dict):
list_member_sep = ','
defaults = {"display": {"int": ["frame-duration", "wait-duration"],
"bool": "centered",
"int-list": "dimensions"},
"screen-captures": {"path": "path"},
"setup": {"list": ["classifiers", "resources-search-path",
"requirements", "data-exclude"],
"path": ["installation-dir", "package-root",
"changelog", "description-file",
"main-object"]},
"keys": {"list": ["up", "right", "down", "left"]}}
additional_defaults = {}
def __init__(self):
dict.__init__(self, {"bool": [], "int": [], "float": [], "path": [],
"list": [], "int-list": []})
add = self.add
add("int", "display", "frame-duration")
add("int", "display", "wait-duration")
add("bool", "display", "centered")
add("int-list", "display", "dimensions")
add("path", "screen-captures", "path")
add("list", "setup", "classifiers")
add("list", "setup", "resources-search-path")
add("path", "setup", "installation-dir")
add("path", "setup", "package-root")
add("list", "setup", "data-exclude")
add("path", "setup", "changelog")
add("list", "setup", "requirements")
add("path", "setup", "description-file")
add("path", "setup", "main-object")
add("list", "keys", "up")
add("list", "keys", "right")
add("list", "keys", "down")
add("list", "keys", "left")
self.add_chart(self.defaults)
self.add_chart(self.additional_defaults)
def add(self, type, section, option):
self[type].append((section, option))
def add(self, cast, section, option):
self[cast].append((section, option))
def add_chart(self, chart):
for section, declarations in chart.iteritems():
for cast, options in declarations.iteritems():
if type(options) != list:
options = [options]
for option in options:
self.add(cast, section, option)

View File

@ -17,9 +17,10 @@ class Game(GameChild, Animation):
resources_path = None
def __init__(self, config_rel_path=None):
def __init__(self, config_rel_path=None, type_declarations=None):
self.init_gamechild()
self.config_rel_path = config_rel_path
self.type_declarations = type_declarations
self.set_configuration()
self.init_animation()
self.align_window()
@ -29,13 +30,15 @@ class Game(GameChild, Animation):
self.subscribe_to(self.get_user_event_id(), self.end)
self.clear_queue()
self.delegate.enable()
def init_gamechild(self):
GameChild.__init__(self)
def set_configuration(self):
self.configuration = Configuration(self.config_rel_path,
self.resources_path)
self.resources_path,
self.type_declarations)
def init_animation(self):
Animation.__init__(self,