今天写一个简单的python程序,调用的模块都是默认存在的。包括os time random threading
先写一个简单的是通过0-9的随机数字在运行面板中生成。
import osimport randomimport timeimport threadingarr = [0,1,' ']alpha = '1234567890'column = 80lines = 24picture = []#清屏def init():os.system('cls')def play(arr):init()while True:draw(alpha)time.sleep(0.08)os.system('cls')def draw(arr):produceline(alpha)for i in picture:print(i)def produceline(alpha):newline = ''global picturefor i in range(column//4):newline = newline+str(str(random.choice(alpha))+' ')picture.insert(0,newline)picture = picture[:lines]if __name__ == '__main__':t = threading.Thread(target=play,args=(alpha,))t.start()t.join()
这段代码使用了多线程,启动了一个名为 t 的新线程,运行 play 函数。在 play 函数中,不断地调用 draw 函数,每次调用时生成一行随机数字并将其插入到 picture 列表的开头,然后打印出整个 picture 列表。这样就形成了一个数字雨的效果。在主线程中,使用 input 函数等待用户输入。
现在写一个黑客帝国里经典的代码雨,这里需要先在终端内安装pygame,以下是源代码:
pip install pygameimport pygameimport randomdef main():# 初始化pygamepygame.init()# 默认不全屏fullscreen = False# 窗口未全屏宽和高WIDTH, HEIGHT = 1000, 600init_width, init_height = WIDTH, HEIGHT# 字块大小,宽,高suface_height = 9# 字体大小font_size = 10# 创建一个窗口screen = pygame.display.set_mode((init_width, init_height))# 字体font = pygame.font.Font('msyh.ttf', font_size)# 创建一个图像对象bg_suface = pygame.Surface((init_width, init_height), flags=pygame.SRCALPHA)pygame.Surface.convert(bg_suface)bg_suface.fill(pygame.Color(0, 0, 0, 28))# 用纯色填充背景screen.fill((0, 0, 0))# 显示的字符letter = ['g', 'a', 'o', 'e', '3', 'r', '2', '7t', 'x521', 'u', 'gorge', 'o', 'p6', 'a', 's', 'd', 'f', 'g', 'h','j', 'k', 'l', 'z', 'x','top','v', 'b', 'n', 'm', '1', '0']texts = [font.render(str(letter[i]), True, (0, 255, 0)) for i in range(26)]# 也可以替换成0 1 显示# texts = [# font.render('0',True,(0,255,0)),font.render('1',True,(0,255,0))# ]# 生成的列数column = int(init_width / suface_height)drops = [0 for i in range(column)]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")fullscreen = not fullscreenif fullscreen:# 全屏效果,参数重设size = init_width, init_height = pygame.display.list_modes()[0]screen = pygame.display.set_mode(size, pygame.FULLSCREEN | pygame.HWSURFACE)else:init_width, init_height = WIDTH, HEIGHTscreen = pygame.display.set_mode((WIDTH, HEIGHT))# 图像对象重新创建bg_suface = pygame.Surface((init_width, init_height), flags=pygame.SRCALPHA)pygame.Surface.convert(bg_suface)bg_suface.fill(pygame.Color(0, 0, 0, 28))column = int(init_width / suface_height)drops = [0 for i in range(column)]elif event.key == pygame.K_ESCAPE:# 按ESC退出exit()# 延时pygame.time.delay(14)# 图像对象放到窗口的原点坐标上screen.blit(bg_suface, (0, 0))for i in range(len(drops)):# 随机字符text = random.choice(texts)# 把字符画到该列的下雨的位置screen.blit(text, (i * suface_height, drops[i] * suface_height))# 更新下雨的坐标drops[i] += 1# 超过界面高度或随机数,下雨位置置0if drops[i] * suface_height > init_height or random.random() > 0.95:drops[i] = 0# 更新画面pygame.display.flip()if __name__ == '__main__':main()
这个调用了pygame的模块,并有一个按键侦听在按下F11后会全屏,再次按下F11会退出全屏。详细的代码注释已列出。感兴趣的同学在集成环境里尝试运行一下看看效果如何。
下面是按下F11全屏后的效果。