- method to cancel progpagation of events after subscriber processes them

- method to suppress input for amount of time
- shadowed sprite class
- replace color in a surface utility function
This commit is contained in:
Frank DeMarco 2020-01-18 10:28:18 -05:00
parent 07d404e64b
commit 10ee6533f0
4 changed files with 69 additions and 10 deletions

View File

@ -11,6 +11,7 @@ class Delegate(GameChild):
self.subscribers = dict()
self.load_configuration()
self.disable()
self.cancelling_propagation = False
def load_configuration(self):
config = self.get_configuration("event")
@ -40,10 +41,17 @@ class Delegate(GameChild):
self.interpolator.gui.__class__):
self.print_debug("Passing %s to %s" % (evt,
subscriber))
subscriber(evt)
if not self.cancelling_propagation:
subscriber(evt)
else:
self.cancelling_propagation = False
break
else:
pump()
def cancel_propagation(self):
self.cancelling_propagation = True
def add_subscriber(self, callback, kind=None):
self.print_debug("Subscribing %s to %s" % (callback, kind))
if kind is None:

View File

@ -1,7 +1,7 @@
import pygame
from pygame.locals import *
from .GameChild import GameChild
from .Animation import Animation
from .Mainloop import Mainloop
from .Audio import Audio
from .Display import Display
@ -14,13 +14,14 @@ from .VideoRecorder import VideoRecorder
from .Interpolator import Interpolator
from .TimeFilter import TimeFilter
class Game(GameChild):
class Game(Animation):
resource_path = None
def __init__(self, config_rel_path=None, type_declarations=None):
self.profile = Profile(self)
GameChild.__init__(self)
self.time_filter = TimeFilter(self)
Animation.__init__(self, self)
self.print_debug(pygame.version.ver)
self.config_rel_path = config_rel_path
self.type_declarations = type_declarations
@ -29,6 +30,7 @@ class Game(GameChild):
self.set_children()
self.subscribe(self.end, QUIT)
self.subscribe(self.end)
self.register(self.get_input().unsuppress)
self.delegate.enable()
def set_configuration(self):
@ -37,7 +39,6 @@ class Game(GameChild):
self.type_declarations)
def set_children(self):
self.time_filter = TimeFilter(self)
self.delegate = Delegate(self)
self.display = Display(self)
self.mainloop = Mainloop(self)
@ -51,6 +52,7 @@ class Game(GameChild):
self.time_filter.update()
self.delegate.dispatch()
if not self.interpolator.is_gui_active():
Animation.update(self)
self.update()
else:
self.interpolator.gui.update()
@ -70,6 +72,10 @@ class Game(GameChild):
def get_rect(self):
return self.get_screen().get_rect()
def suppress_input_temporarily(self, length=700):
self.get_input().suppress()
self.play(self.get_input().unsuppress, delay=length, play_once=True)
def end(self, evt):
if evt.type == QUIT or self.delegate.compare(evt, "quit"):
self.mainloop.stop()

View File

@ -491,3 +491,45 @@ class RainbowSprite(Sprite):
def set_frames(self, image):
for hue in range(0, 360, self.hue_shift):
self.add_frame(get_hue_shifted_surface(image, hue))
class ShadowedSprite(Sprite):
def __init__(self, parent, offset=Vector(3, 3), opacity=1, color=(0, 0, 0), framerate=None):
Sprite.__init__(self, parent, framerate)
if not isinstance(offset, Vector):
offset = Vector(*offset)
if not isinstance(color, Color):
color = Color(*color)
self.shadow_color = color
self.shadow_offset = offset
self.shadow_opacity = opacity
self.shadow_color = color
self.shadow = Sprite(self)
def add_frame(self, frame, omit=False):
Sprite.add_frame(self, frame, omit)
fr = frame.get_rect()
shadow = Surface(fr.size, SRCALPHA)
pixels = PixelArray(frame)
shadow_pixels = PixelArray(shadow)
r, g, b = self.shadow_color.r, self.shadow_color.g, self.shadow_color.b
for y in range(len(pixels)):
for x in range(len(pixels[0])):
frame_pixel_color = frame.unmap_rgb(pixels[y][x])
if frame_pixel_color == frame.get_colorkey():
shadow_pixel_color = 0, 0, 0, 0
else:
shadow_pixel_color = r, g, b, frame_pixel_color.a * self.shadow_opacity
shadow_pixels[y][x] = shadow_pixel_color
self.shadow.add_frame(shadow)
def clear_frames(self):
self.shadow.clear_frames()
Sprite.clear_frames(self)
def update(self):
self.shadow.location.topleft = Vector(*self.location.topleft).get_moved(
*self.shadow_offset)
self.shadow.update()
Sprite.update(self)

View File

@ -186,7 +186,7 @@ def get_boxed_surface(surface, background=None, border=None, border_width=1,
surface = bordered_surface
return surface
def render_box(font, text, antialias, color, background=None, border=None,
def render_box(font, text, antialias=True, color=(0, 0, 0), background=None, border=None,
border_width=1, padding=0):
return get_boxed_surface(font.render(text, antialias, color, background),
background, border, border_width, padding)
@ -230,11 +230,14 @@ def get_wrapped_text_surface(font, text, width, antialias, color,
surface = Surface((0, 0), SRCALPHA)
return get_boxed_surface(surface, background, border, border_width, padding)
def get_color_swapped_surface(surface, current, replacement):
swapped = surface.copy()
pixels = PixelArray(swapped)
pixels.replace(current, replacement)
def replace_color(surface, color, replacement):
pixels = PixelArray(surface)
pixels.replace(color, replacement)
del pixels
def get_color_swapped_surface(surface, color, replacement):
swapped = surface.copy()
replace_color(swapped, color, replacement)
return swapped
def get_busy_channel_count():