multiple sprite locations

This commit is contained in:
Frank DeMarco 2013-10-13 21:55:18 +09:00
parent 132133dbb2
commit 50de5d523b
1 changed files with 61 additions and 27 deletions

View File

@ -1,5 +1,7 @@
from os import listdir
from os.path import isfile, join
from sys import exc_info, stdout
from traceback import print_exc, print_stack
from pygame import Color, Rect, Surface
from pygame.image import load
@ -16,10 +18,17 @@ class Sprite(Animation):
self.clear_frames()
self.frame_index = 0
self.mirrored = False
self.rect = Rect((0, 0, 0, 0))
self.locations = [Location()]
self.motion_overflow = Vector()
self.display_surface = self.get_display_surface()
def __getattr__(self, name):
if name in ("location", "rect"):
return self.locations[0]
if hasattr(Animation, "__getattr__"):
return Animation.__getattr__(self, name)
raise AttributeError, name
def set_framerate(self, framerate):
self.register(self.shift_frame, interval=framerate)
@ -61,7 +70,8 @@ class Sprite(Animation):
width, height = frame.get_size()
max_width = max(width, max_width)
max_height = max(height, max_height)
self.rect.size = max_width, max_height
for location in self.locations:
location.size = max_width, max_height
def shift_frame(self):
if len(self.frames) > 1:
@ -77,12 +87,58 @@ class Sprite(Animation):
self.frame_index = index
def move(self, dx=0, dy=0):
rect = self.rect
for location in self.locations:
location.move_ip(dx, dy)
def reset_motion_overflow(self):
for location in self.locations:
location.reset_motion_overflow()
def collide(self, other):
if not isinstance(other, Rect):
other = other.rect
for location in self.locations:
if location.colliderect(other):
return location
def mirror(self):
frames = self.frames
for ii, frame in enumerate(frames):
frames[ii] = flip(frame, True, False)
self.mirrored = not self.mirrored
def clear_frames(self):
self.frames = []
def add_location(self, offset=(0, 0), count=1):
base = self.locations[0]
current_offset = list(offset)
for ii in xrange(count):
self.locations.append(base.move(*current_offset))
current_offset[0] += offset[0]
current_offset[1] += offset[1]
def update(self):
Animation.update(self)
self.draw()
def draw(self):
for location in self.locations:
self.display_surface.blit(self.get_current_frame(), location)
class Location(Rect):
def __init__(self, rect=(0, 0, 0, 0)):
Rect.__init__(self, rect)
self.motion_overflow = Vector()
def move_ip(self, dx, dy):
if isinstance(dx, float) or isinstance(dy, float):
excess = self.update_motion_overflow(dx, dy)
rect.move_ip(int(dx) + excess[0], int(dy) + excess[1])
Rect.move_ip(self, int(dx) + excess[0], int(dy) + excess[1])
else:
rect.move_ip(dx, dy)
Rect.move_ip(self, dx, dy)
def update_motion_overflow(self, dx, dy):
overflow = self.motion_overflow
@ -94,25 +150,3 @@ class Sprite(Animation):
def reset_motion_overflow(self):
self.motion_overflow.place_at_origin()
def collide(self, other):
rect = other
if not isinstance(other, Rect):
rect = other.rect
return self.rect.colliderect(rect)
def mirror(self):
frames = self.frames
for ii, frame in enumerate(frames):
frames[ii] = flip(frame, True, False)
self.mirrored = not self.mirrored
def clear_frames(self):
self.frames = []
def update(self):
Animation.update(self)
self.draw()
def draw(self):
self.display_surface.blit(self.get_current_frame(), self.rect)