This commit is contained in:
Frank DeMarco 2013-03-19 19:11:32 +09:00
parent a9fdb230c9
commit 0bce7c6973
4 changed files with 81 additions and 24 deletions

View File

@ -4,33 +4,77 @@ from GameChild import GameChild
class Animation(GameChild):
def __init__(self, parent, procedure=None, interval=None):
def __init__(self, parent, method=None, interval=None):
GameChild.__init__(self, parent)
self.current_procedure = procedure or self.procedure
self.interval = interval
self.default_method = method or self.build_frame
self.accounts = {self.default_method: Account(interval)}
self.current_elapsed = 0
self.last_update = 0
self.playing = False
def procedure(self):
def build_frame(self):
pass
def play(self, procedure=None, interval=None):
def register(self, method, interval=None, delay=0):
if method not in self.accounts:
self.accounts[method] = Account(interval, delay)
def play(self, method=None, interval=None, delay=0):
method = self.get_default(method)
self.register(method, interval, delay)
self.accounts[method].play()
def get_default(self, method):
if not method:
method = self.default_method
return method
def halt(self, method=None):
if not method:
for account in self.accounts.values():
account.halt()
else:
if self.accounts.has_key(method):
self.accounts[method].halt()
def update(self):
for method, account in self.accounts.iteritems():
if account.update():
method()
class Account:
def __init__(self, interval, delay=0):
self.interval = interval
self.delay = delay
self.last_frame = 0
self.last_update = None
self.halt()
def play(self):
self.playing = True
if procedure:
self.current_procedure = procedure
if interval:
self.interval = interval
def halt(self):
self.playing = False
def update(self):
if self.playing:
if self.interval:
ticks = get_ticks()
elapsed = self.current_elapsed - self.last_update + ticks
if elapsed < self.interval:
return
self.last_update = ticks
self.current_procedure()
ticks = get_ticks()
self.update_delay(ticks)
if not self.delay:
interval = self.interval
if interval:
if ticks - self.last_frame < interval:
return False
self.last_frame = ticks
return True
def update_delay(self, ticks):
delay = self.delay
if delay > 0:
last_update = self.last_update or ticks
delay -= ticks - last_update
if delay < 0:
delay = 0
self.last_update = ticks
self.delay = delay

View File

@ -64,12 +64,13 @@ class Configuration(RawConfigParser):
section = "display"
add_section(section)
set_option(section, "dimensions", "480, 360")
set_option(section, "frame-duration", "33")
set_option(section, "frame-duration", "40")
set_option(section, "wait-duration", "2")
set_option(section, "caption", "")
set_option(section, "centered", "yes")
set_option(section, "icon-path", "")
set_option(section, "skip-frames", "no")
set_option(section, "fullscreen", "no")
section = "sprite"
add_section(section)
set_option(section, "transparent-color", "magenta")
@ -243,7 +244,7 @@ class TypeDeclarations(dict):
list_member_sep = ','
defaults = {"display": {"int": ["frame-duration", "wait-duration"],
"bool": ["centered", "skip-frames"],
"bool": ["centered", "skip-frames", "fullscreen"],
"int-list": "dimensions"},
"screen-captures": {"path": "path"},
"setup": {"list": ["classifiers", "resources-search-path",

View File

@ -11,12 +11,22 @@ class Display(GameChild):
GameChild.__init__(self, game)
self.config = self.get_configuration("display")
self.align_window()
self.set_screen()
self.init_screen()
self.set_caption()
self.set_icon()
self.set_mouse_visibility()
self.subscribe(self.toggle_fullscreen)
def align_window(self):
if self.config["centered"]:
environ["SDL_VIDEO_CENTERED"] = "1"
def init_screen(self):
flags = 0
if self.config["fullscreen"]:
flags = FULLSCREEN
self.set_screen(flags)
def set_screen(self, flags=0):
self.screen = display.set_mode(self.config["dimensions"], flags)
@ -33,10 +43,6 @@ class Display(GameChild):
visibility = self.get_configuration("mouse", "visible")
mouse.set_visible(visibility)
def align_window(self):
if self.config["centered"]:
environ["SDL_VIDEO_CENTERED"] = "1"
def get_screen(self):
return self.screen

View File

@ -78,6 +78,12 @@ class Sprite(Animation):
overflow[1] -= int(overflow[1])
return excess
def collide(self, other):
rect = other
if not isinstance(other, Rect):
rect = other.rect
return self.rect.colliderect(rect)
def update(self):
if self.active:
Animation.update(self)