赞
踩
import os
import sys
import pygame
import random
def main(argv):
code_rain = CodeRain()
code_rain.play()
class CodeRain(object):
def __init__(self):
self.current_path = os.path.dirname(os.path.realpath(__file__))
self.fullscreen = False
self.suface_height = 18
self.font_size = 20
self.WIDTH = 1100
self.HEIGHT = 600
self.init_width = self.WIDTH
self.init_height = self.HEIGHT
self.screen = None
self.surface = None
self.font = None
self.lst = None
print('current_path: ', self.current_path)
def play(self):
pygame.init()
self.font = pygame.font.SysFont('宋体', self.font_size)
# 内容
str = [chr(i) for i in range(48, 58)] + [chr(i) for i in range(97, 123)]
texts = [ self.font.render(i, True, (0, 255, 0)) for i in str]
self.lst = list(range(99))
self.screen_setup()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.KEYDOWN:
# 按F11切换全屏,或窗口
if event.key == pygame.K_F11:
print("检测到按键F11")
self.fullscreen = not self.fullscreen
self.screen_setup()
elif event.key == pygame.K_ESCAPE:
# 按ESC退出
exit()
pygame.time.delay(50)
self.screen.blit(self.surface, (0, 0))
for i in range(len(self.lst)):
text = random.choice(texts)
self.screen.blit(text, (i * 20, self.lst[i] * 20))
self.lst[i] += 1
if random.random() < 0.05:
self.lst[i] = 0
pygame.display.flip()
# 全屏效果,参数重设
def screen_setup(self):
if self.fullscreen:
# 全屏效果,参数重设
# size = self.init_width, self.init_height = pygame.display.list_modes()[0]
size = self.init_width, self.init_height = self.screen.get_width(),self.screen.get_height()
self.screen = pygame.display.set_mode(size, pygame.FULLSCREEN)
# self.screen = pygame.display.set_mode(size, pygame.FULLSCREEN | pygame.HWSURFACE)
# self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
else:
size = self.init_width, self.init_height = self.WIDTH, self.HEIGHT
# self.screen = pygame.display.set_mode((self.WIDTH, self.HEIGHT))
self.screen = pygame.display.set_mode(size)
# 图像对象重新创建
self.surface = pygame.Surface((self.init_width, self.init_height), flags=pygame.SRCALPHA)
pygame.Surface.convert(self.surface)
self.surface.fill((0, 0, 0, 10))
self.screen.fill((0, 0, 0, 10))
if __name__ == "__main__":
main(sys.argv[1:])
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。