今天写一个简单的python程序,调用的模块都是默认存在的。包括os time random threading
先写一个简单的是通过0-9的随机数字在运行面板中生成。
import osimport randomimport timeimport threading
arr = [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 picture for 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 pygame
import pygameimport random
def main(): # 初始化pygame pygame.init()
# 默认不全屏 fullscreen = False # 窗口未全屏宽和高 WIDTH, HEIGHT = 1000, 600
init_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 fullscreen if 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, HEIGHT screen = 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
# 超过界面高度或随机数,下雨位置置0 if 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全屏后的效果。
领取专属 10元无门槛券
私享最新 技术干货