a-robots-conundrum/arobotsconundrum/fadeout.py

72 lines
2.4 KiB
Python
Raw Normal View History

# This file is part of A Robot's Conundrum.
2012-08-09 15:51:59 +02:00
#
# A Robot's Conundrum is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
2012-08-09 15:51:59 +02:00
#
# A Robot's Conundrum is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
2012-08-09 15:51:59 +02:00
#
# You should have received a copy of the GNU General Public License along with
# A Robot's Conundrum. If not, see <http://www.gnu.org/licenses/>.
2012-08-09 15:51:59 +02:00
#
# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
#
# fadeout.py
# --------------------
# date created : Thu Aug 9 2012
# copyright : (C) 2012 Niels G. W. Serup
# maintained by : Niels G. W. Serup <ngws@metanohi.name>
2012-08-09 15:51:59 +02:00
"""
Fade out and in.
"""
import os
import pygame
class Fadeout(object):
def __init__(self, game, function, duration=500):
2012-08-09 15:51:59 +02:00
self.__dict__.update(locals())
self.game.objs.append(self)
self.img = pygame.Surface(self.game.window.get_size())
self._start_time = pygame.time.get_ticks()
self._middle_time = self._start_time + duration
self._end_time = self._middle_time + duration
2012-08-09 15:51:59 +02:00
self.img.set_alpha(0)
self.img.fill((0, 0, 0))
self._has_run = False
def update(self, e, t, dt):
if t < self._middle_time:
self.img.set_alpha(255 * (t - self._start_time) / self.duration)
2012-08-09 15:51:59 +02:00
else:
if not self._has_run:
self.function()
self._has_run = True
if t < self._end_time:
self.img.set_alpha(255 * (self.duration - (t - self._middle_time)) / self.duration)
2012-08-09 15:51:59 +02:00
else:
self.game.objs.remove(self)
self.update = lambda *xs: None
def draw(self, window):
window.blit(self.img, (0, 0))
2012-08-09 17:18:44 +02:00
class Darkness(Fadeout):
def __init__(self, game, darkness):
self.__dict__.update(locals())
self.set_darkness(darkness)
def set_darkness(self, darkness):
self.darkness = darkness
self.img = pygame.Surface(self.game.window.get_size())
self.img.set_alpha(int(darkness * 255))
self.img.fill((0, 0, 0))
def update(self, *xs):
pass