Created
March 2, 2013 17:45
-
-
Save rshk/5072173 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Fade IN/OUT some text.. | |
""" | |
import sys | |
import time | |
import pygame | |
## Show some text fade in/out | |
FADE_IN_TIME = 5 | |
FADE_OUT_TIME = 5 | |
FADE_IN_EASING = lambda x: x # Linear | |
FADE_OUT_EASING = lambda x: x # Linear | |
def apply_alpha(surface, alpha): | |
## Applies a given alpha amount to a surface. | |
for x in xrange(surface.get_width()): | |
for y in xrange(surface.get_height()): | |
color = surface.get_at((x, y)) | |
color.a = int(color.a * alpha) | |
surface.set_at((x, y), color) | |
return surface | |
pygame.init() | |
clock = pygame.time.Clock() | |
size = width, height = 1024, 400 | |
screen = pygame.display.set_mode(size) | |
font = pygame.font.SysFont('sans-serif', 160, True) | |
fps_font = pygame.font.SysFont('monospace', 20, True) | |
rendered_text1 = font.render("Hello, world!", True, (255, 0, 0)) | |
rendered_text2 = font.render("Hello, world!", True, (0, 0, 255)) | |
text_rect = rendered_text1.get_rect(center=(width / 2, height / 2)) | |
ST_FADEIN = 0 | |
ST_FADEOUT = 1 | |
state = ST_FADEIN | |
last_state_change = time.time() | |
while 1: | |
## Check for events | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
pygame.quit() # Exit the main loop | |
sys.exit() | |
## Update the state | |
state_time = time.time() - last_state_change | |
if state == ST_FADEIN: | |
if state_time >= FADE_IN_TIME: | |
state = ST_FADEOUT | |
#state_time = max(0, min(state_time - FADE_IN_TIME, 1)) | |
state_time -= FADE_IN_TIME | |
last_state_change = time.time() - state_time | |
elif state == ST_FADEOUT: | |
if state_time >= FADE_OUT_TIME: | |
state = ST_FADEIN | |
#state_time = max(0, min(state_time - FADE_OUT_TIME, 1)) | |
state_time -= FADE_OUT_TIME | |
last_state_change = time.time() - state_time | |
else: | |
raise ValueError() | |
if state == ST_FADEIN: | |
alpha = FADE_IN_EASING(1.0 * state_time / FADE_IN_TIME) | |
rt = rendered_text1 | |
elif state == ST_FADEOUT: | |
alpha = 1. - FADE_OUT_EASING(1.0 * state_time / FADE_OUT_TIME) | |
rt = rendered_text2 | |
else: | |
raise ValueError() | |
surf = apply_alpha(rt.copy(), alpha) | |
screen.fill((0, 0, 0)) | |
screen.blit(surf, text_rect) | |
fps = clock.get_fps() | |
fpslabel = fps_font.render(str(int(fps)), True, (255, 255, 255)) | |
rec = fpslabel.get_rect(top=5, right=width - 5) | |
screen.blit(fpslabel, rec) | |
pygame.display.flip() | |
clock.tick(50) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment