sprite mask collision; rainbow sprite can set frames separately; get blinds transition frames from surface

This commit is contained in:
Frank DeMarco 2019-08-19 17:24:15 -04:00
parent b7e6f80a33
commit 63f5cf5db3
2 changed files with 34 additions and 4 deletions

View File

@ -3,7 +3,7 @@ from os.path import isfile, join
from sys import exc_info, stdout
from glob import glob
from pygame import Color, Rect, Surface, PixelArray
from pygame import Color, Rect, Surface, PixelArray, mask
from pygame.image import load
from pygame.transform import flip
from pygame.locals import *
@ -129,6 +129,14 @@ class Sprite(Animation):
if location.colliderect(other):
return location
def collide_mask(self, other):
if self.collide(other):
current_mask = mask.from_surface(self.get_current_frame())
other_mask = mask.from_surface(other.get_current_frame())
offset = other.location.left - self.location.left, \
other.location.top - self.location.top
return current_mask.overlap(other_mask, offset) is not None
def mirror(self):
frames = self.frames
for ii, frame in enumerate(frames):
@ -473,7 +481,12 @@ class BlinkingSprite(Sprite):
class RainbowSprite(Sprite):
def __init__(self, parent, image, hue_shift=8, framerate=None):
def __init__(self, parent, image=None, hue_shift=8, framerate=None):
Sprite.__init__(self, parent, framerate)
for hue in range(0, 360, hue_shift):
self.hue_shift = hue_shift
if image is not None:
self.set_frames(image)
def set_frames(self, image):
for hue in range(0, 360, self.hue_shift):
self.add_frame(get_hue_shifted_surface(image, hue))

View File

@ -1,7 +1,7 @@
from random import randint, random
from math import sin, cos, atan2, radians, sqrt, pi
from pygame import Surface, PixelArray, Color
from pygame import Surface, PixelArray, Color, Rect
from pygame.mixer import get_num_channels, Channel
from pygame.locals import *
@ -231,6 +231,23 @@ def get_shadowed_text(text, font, offset, color, antialias=True, shadow_color=(0
(abs(offset[1]) - offset[1]) / 2))
return surface
def get_blinds_frames(surface, step=.05, count=4, fill=(0, 0, 0, 0)):
frames = []
rects = []
h = int(round(surface.get_height() / float(count)))
for ii in range(count):
rects.append(Rect(0, h * ii, surface.get_width(), 0))
bar_h = int(round(h * step))
while rects[0].h < h:
frame = surface.copy()
for rect in rects:
rect.inflate_ip(0, bar_h)
frame.fill(fill, rect)
bottom = rect.bottom
rect.bottom = bottom
frames.append(frame)
return frames
def get_hsla_color(hue, saturation=100, lightness=50, alpha=100):
color = Color(0, 0, 0, 0)
color.hsla = hue % 360, saturation, lightness, alpha