This commit is contained in:
Frank DeMarco 2013-03-16 20:06:21 +09:00
parent 92ea55e016
commit df5acb0e91
5 changed files with 133 additions and 0 deletions

36
pgfw/Animation.py Normal file
View File

@ -0,0 +1,36 @@
from pygame.time import get_ticks
from GameChild import GameChild
class Animation(GameChild):
def __init__(self, parent, procedure=None, interval=None):
GameChild.__init__(self, parent)
self.current_procedure = procedure or self.procedure
self.interval = interval
self.current_elapsed = 0
self.last_update = 0
self.playing = False
def procedure(self):
pass
def play(self, procedure=None, interval=None):
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()

View File

@ -70,6 +70,9 @@ class Configuration(RawConfigParser):
set_option(section, "centered", "yes")
set_option(section, "icon-path", "")
set_option(section, "skip-frames", "no")
section = "sprite"
add_section(section)
set_option(section, "transparent-color", "magenta")
section = "screen-captures"
add_section(section)
set_option(section, "rel-path", "caps")

View File

@ -52,6 +52,12 @@ class Game(GameChild):
def update(self):
pass
def blit(self, source, destination, area=None, special_flags=0):
self.get_screen().blit(source, destination, area, special_flags)
def get_rect(self):
return self.get_screen().get_rect()
def end(self, evt):
if evt.type == QUIT or self.delegate.compare(evt, "quit"):
self.mainloop.stop()

16
pgfw/Point.py Normal file
View File

@ -0,0 +1,16 @@
class Point(list):
def __init__(self, x=0, y=0):
list.__init__(self, (x, y))
def place(self, x=None, y=None):
if x is not None:
self[0] = x
if y is not None:
self[1] = y
def move(self, dx=0, dy=0):
if dx:
self[0] += dx
if dy:
self[1] += dy

72
pgfw/Sprite.py Normal file
View File

@ -0,0 +1,72 @@
from os import listdir
from os.path import isfile
from pygame import Color, Rect
from pygame.image import load
from pygame.locals import *
from Animation import Animation
class Sprite(Animation):
def __init__(self, parent, framerate=None):
Animation.__init__(self, parent, self.shift_frame, framerate)
self.frames = []
self.frame_index = 0
self.rect = Rect((0, 0, 0, 0))
self.deactivate()
def deactivate(self):
self.active = False
def activate(self):
self.active = True
def load_from_path(self, path, transparency=False):
if isfile(path):
paths = [path]
else:
paths = sorted(listdir(path))
for path in paths:
self.add_frame(load(path), transparency)
def add_frame(self, frame, transparency=False):
if transparency:
self.frames.append(frame.convert_alpha())
else:
self.frames.append(frame.convert())
self.measure_rect()
if len(self.frames) > 1:
self.play()
def measure_rect(self):
max_width, max_height = 0, 0
for frame in self.frames:
width, height = frame.get_size()
max_width = max(width, max_width)
max_height = max(height, max_height)
self.rect.size = max_width, max_height
def shift_frame(self):
if len(self.frames) > 1:
self.increment_frame_index()
def get_current_frame(self):
return self.frames[self.frame_index]
def increment_frame_index(self):
index = self.frame_index + 1
if index >= len(self.frames):
index = 0
self.frame_index = index
def move(self, dx=0, dy=0):
self.rect.move_ip(dx, dy)
def update(self):
if self.active:
Animation.update(self)
self.draw()
def draw(self):
self.parent.blit(self.get_current_frame(), self.rect)