gif auto hide

This commit is contained in:
Frank DeMarco 2020-10-28 20:02:23 -04:00
parent 2f172566b0
commit acf212a5ca
1 changed files with 38 additions and 19 deletions

57
NS.py
View File

@ -15,7 +15,7 @@ from pygame import Surface, Color, mixer
from pygame.event import clear
from pygame.mixer import Sound
from pygame.image import load, fromstring
from pygame.transform import rotate, flip, scale
from pygame.transform import rotate, flip, scale, smoothscale
from pygame.time import get_ticks
from pygame.font import Font
from pygame.draw import aalines, lines
@ -213,12 +213,13 @@ class NS(Game, Animation):
print("auto arduino reset triggered")
self.reset_arduino()
self.no_reset_elapsed = 0
self.title.update()
self.introduction.update()
self.ending.update()
self.boss.update()
self.platform.update()
self.chemtrails.update()
self.title.update()
if not self.title.active:
self.platform.update()
self.chemtrails.update()
self.boss.update_dialogue()
self.wipe.update()
self.idle_elapsed += self.time_filter.get_last_frame_duration()
@ -339,29 +340,32 @@ class Tony(Sprite):
class Video(Sprite):
def __init__(self, parent):
def __init__(self, parent, diameter, next_video_chance=.01):
Sprite.__init__(self, parent, 100)
self.next_video_chance = next_video_chance
pattern = join(self.get_resource("gif"), "Boarding_*.gif")
self.gifs = []
for path in iglob(pattern):
self.gifs.append(Image.open(path))
print(self.gifs[-1].info)
self.gif = self.gifs[1]
self.mask = Surface((320, 320), SRCALPHA)
self.mask = Surface([diameter] * 2, SRCALPHA)
rect = self.mask.get_rect()
filled_circle(self.mask, rect.centerx, rect.centery, rect.centerx, (255, 255, 255))
filled_circle(self.mask, rect.centerx, rect.centery, rect.centerx, (0, 0, 0))
filled_circle(self.mask, rect.centerx, rect.centery, rect.centerx - 2, (255, 255, 255))
self.add_frame(self.mask)
self.play()
def update(self):
if random() < .01:
def shift_frame(self):
Sprite.shift_frame(self)
if random() < self.next_video_chance:
while True:
selection = choice(self.gifs)
if selection != self.gif:
self.gif = selection
break
if random() < .005:
self.toggle_hidden()
self.gif.seek((self.gif.tell() + 1) % self.gif.n_frames)
frame = scale(
frame = smoothscale(
fromstring(self.gif.convert("RGBA").tobytes(), self.gif.size, "RGBA"),
(self.mask.get_width(), int(self.gif.width * self.gif.height / self.mask.get_width())))
copy = self.mask.copy()
@ -370,22 +374,21 @@ class Video(Sprite):
copy.blit(frame, rect, None, BLEND_RGBA_MIN)
self.clear_frames()
self.add_frame(copy)
Sprite.update(self)
class Title(GameChild):
class Title(Animation):
UNLOCK_MOVES = NS.N, NS.NW, NS.E, NS.S
def __init__(self, parent):
GameChild.__init__(self, parent)
Animation.__init__(self, parent)
self.plank = Sprite(self)
self.plank.load_from_path(self.get_resource("Title_plank.png"), True)
ds = self.get_display_surface()
dsr = ds.get_rect()
self.plank.location.center = dsr.center
# self.angle = choice((pi / 4, 3 * pi / 4, 5 * pi / 4, 7 * pi / 4))
self.angle = 7 * pi / 4
self.angle = pi / 8
self.background = Sprite(self)
self.background.load_from_path(self.get_resource("Title_tile.png"), True)
for y in range(0, dsr.h + self.background.location.h, self.background.location.h):
@ -400,7 +403,10 @@ class Title(GameChild):
for x in range(0, dsr.w, thickness):
frame.fill(palette[(offset + x) % len(palette)], (x, 0, thickness, dsr.h))
self.effect.add_frame(frame)
self.video = Video(self)
self.video = Video(self, 320)
self.video.location.center = 329, 182
self.register(self.show_video, self.hide_video)
self.show_video()
def reset(self):
self.first_pressed = False
@ -456,10 +462,19 @@ class Title(GameChild):
seconds, fraction = divmod(milliseconds, 1000)
return "%i:%02i.%i" % (minutes, seconds, fraction / 100)
def show_video(self):
self.video.unhide()
self.play(self.hide_video, delay=16000, play_once=True)
def hide_video(self):
self.video.hide()
self.play(self.show_video, delay=7000, play_once=True)
def update(self):
'''
Move title, check button presses, and draw screen
'''
Animation.update(self)
if self.active:
ds = self.get_display_surface()
dsr = ds.get_rect()
@ -507,8 +522,12 @@ class Title(GameChild):
self.video.move(dy=dsr.top - self.video.location.top)
dx, dy = get_delta(self.angle, 5, False)
self.video.move(dx, dy)
if not platform.get_pressed():
self.video.update()
if platform.get_pressed():
self.video.hide()
self.halt(self.show_video)
self.halt(self.hide_video)
self.play(self.show_video, delay=10000, play_once=True)
self.video.update()
self.draw_scores()