public
Created

  • Download Gist
simple_fade.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
"""
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
 
 
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()
 
surf2 = pygame.surface.Surface((text_rect.width, text_rect.height))
surf2.set_alpha(255 * alpha)
 
screen.fill((0, 0, 0))
surf2.blit(rt, (0, 0))
screen.blit(surf2, 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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.