This commit is contained in:
Frank DeMarco 2013-12-11 00:50:02 +09:00
parent 7ad159094a
commit 7df72f4fc4
1 changed files with 99 additions and 5 deletions

View File

@ -8,6 +8,7 @@ from pygame import Color, Rect, Surface
from pygame.image import load
from pygame.transform import flip
from pygame.locals import *
from pygame.time import get_ticks
from Animation import Animation
from Vector import Vector
@ -19,7 +20,8 @@ class Sprite(Animation):
self.clear_frames()
self.frame_index = 0
self.mirrored = False
self.locations = [Location()]
self.alpha = 255
self.locations = [Location(self)]
self.motion_overflow = Vector()
self.display_surface = self.get_display_surface()
@ -66,6 +68,7 @@ class Sprite(Animation):
def add_frame(self, frame):
self.frames.append(frame)
self.measure_rect()
frame.set_alpha(self.alpha)
if len(self.frames) > 1:
self.play()
@ -77,6 +80,7 @@ class Sprite(Animation):
max_height = max(height, max_height)
for location in self.locations:
location.size = max_width, max_height
location.fader.init_surface()
def shift_frame(self):
if len(self.frames) > 1:
@ -118,29 +122,47 @@ class Sprite(Animation):
def add_location(self, topleft=None, offset=(0, 0), count=1, base=0):
if topleft is not None:
for ii in xrange(count):
self.locations.append(Rect(topleft, self.locations[0].size))
self.locations.append(Location(
self, Rect(topleft, self.locations[0].size)))
else:
base = self.locations[base]
current_offset = list(offset)
for ii in xrange(count):
self.locations.append(base.move(*current_offset))
self.locations.append(Location(self,
base.move(*current_offset)))
current_offset[0] += offset[0]
current_offset[1] += offset[1]
def fade(self, length=0, out=None, index=None):
if index is None:
for location in self.locations:
location.fader.start(length, out)
else:
self.locations[index].fader.start(length, out)
def set_alpha(self, alpha):
self.alpha = alpha
for frame in self.frames:
frame.set_alpha(alpha)
for location in self.locations:
location.fader.set_alpha()
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)
location.fader.draw()
class Location(Rect):
def __init__(self, rect=(0, 0, 0, 0)):
def __init__(self, sprite, rect=(0, 0, 0, 0)):
self.sprite = sprite
Rect.__init__(self, rect)
self.motion_overflow = Vector()
self.fader = Fader(self)
def move_ip(self, dx, dy):
if isinstance(dx, float) or isinstance(dy, float):
@ -159,3 +181,75 @@ class Location(Rect):
def reset_motion_overflow(self):
self.motion_overflow.place_at_origin()
class Fader(Surface):
def __init__(self, location):
self.location = location
self.reset()
def reset(self):
self.init_surface()
self.fade_remaining = None
def init_surface(self):
Surface.__init__(self, self.location.size)
if self.location.sprite.frames:
background = Surface(self.get_size())
sprite = self.location.sprite
key = sprite.get_current_frame().get_colorkey() or (255, 0, 255)
self.set_colorkey(key)
background.fill(key)
self.background = background
self.set_alpha()
def set_alpha(self, alpha=None):
if alpha is None:
alpha = self.location.sprite.alpha
Surface.set_alpha(self, alpha)
def start(self, length, out=None):
if self.fade_remaining <= 0:
alpha = self.get_alpha()
maximum = self.location.sprite.alpha
if out is None:
out = alpha == maximum
if out and alpha > 0 or not out and alpha < maximum:
self.fade_length = self.fade_remaining = length
self.start_time = get_ticks()
self.fading_out = out
def draw(self):
sprite = self.location.sprite
if self.fade_remaining >= 0:
self.update_alpha()
self.clear()
frame = sprite.get_current_frame()
frame.set_alpha(255)
self.blit(frame, (0, 0))
frame.set_alpha(sprite.alpha)
sprite.display_surface.blit(self, self.location)
elif self.fade_remaining is None or self.get_alpha() >= sprite.alpha:
if self.fade_remaining >= 0:
self.update_alpha()
sprite.display_surface.blit(sprite.get_current_frame(),
self.location)
def update_alpha(self):
remaining = self.fade_remaining = self.fade_length - (get_ticks() - \
self.start_time)
ratio = self.fade_length and float(remaining) / self.fade_length
if not self.fading_out:
ratio = 1 - ratio
maximum = self.location.sprite.alpha
alpha = int(ratio * maximum)
if alpha > maximum:
alpha = maximum
elif alpha < 0:
alpha = 0
self.set_alpha(alpha)
def clear(self):
self.blit(self.background, (0, 0))