Python pygame框架实现屏幕数字雨特效
import pygame
import random
pygame.init()
FONT_PX = 20
SRCEEN = pygame.display.set_mode()
font = pygame.font.SysFont('arial', 20)
suface = pygame.Surface((1500, 800), flags=pygame.SRCALPHA)
pygame.Surface.convert(suface)
suface.fill(pygame.Color(0, 0, 0, 28))
SRCEEN.fill((0, 0, 0))
texts = [font.render(str(i), True, (0, 255, 0)) for i in range(10)]
column = int(1500 / FONT_PX)
drops = [0 for i in range(column)]
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
pygame.time.delay(60)
SRCEEN.blit(suface, (0, 0))
for i in range(len(drops)):
text = random.choice(texts)
SRCEEN.blit(text, (i * FONT_PX, drops[i] * FONT_PX))
drops[i] += 1
if drops[i] * 10 > 800 or random.random() > 0.95:
drops[i] = 0
pygame.display.flip()
实现效果图