- '''
- 该项目需安装 pygame 模块
- '''
- import pygame
- import sys
- import random
- # 初始化
- pygame.init()
- # 创建窗口
- size = width,height = 1500,700
- screen = pygame.display.set_mode(size)
- bg_surface = pygame.Surface((width,height)) # (width,height) 这是一个元组
- pygame.Surface.convert(bg_surface)
- # 字体对象
- font = pygame.font.SysFont('SimHei',15)
- bg_surface.fill(pygame.Color(0,0,0,20))
- screen.fill((0,0,0))
- # 转成多少列
- column = int(width / 20)
- drops = [0 for i in range(column)] #列表推导式
- letter = ['0', '1', '0', '1', '0', '1', '0', '1', '1', '0', '1', '0', '1', '0', '1', '1', '0', '1', 'l', '0', '1', '0','1', '0', '1', '1']
- texts = [font.render(str(letter[i]), True, (0, 255, 0)) for i in range(26)]
- # 窗口死循环
- while True:
- # 获得事件 pygame.event.get()
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- sys.exit(0)
- pygame.time.delay(30)
- # 重新编辑图像第二个参数是左上角坐标
- screen.blit(bg_surface,(0,0))
- for i in range(len(drops)):
- text = random.choice(texts)
- res = (i * 20,drops[i]*20)
- screen.blit(text,res)
- drops[i] += 1
- if drops[i] * 10> height or random.random()> 0.95:
- drops[i] = 0
- pygame.display.flip()
运行结果:
文章到这里就结束了! 希望大家能多多支持 Python(系列)! 六个月带大家学会 Python, 私聊我, 可以问关于本文章的问题! 以后每天都会发布新文章, 喜欢的点点关注! 一个陪伴你学习 Python 的新青年! 不管多忙都会更新下去! 美好生活是靠自己掌握的! 一起加油!
喜欢 Python 的朋友, 可以点开我的个人简介看看哟!
来源: http://www.jianshu.com/p/70dcc9eb05e3