python 打字小游戏
最近随便用python的pygame编了这个打字小游戏
只要有字母调到窗口底部就结束
上代码:
import pygame.freetype import sys import random pygame.init() screen = pygame.display.set_mode((500, 400)) pygame.display.set_caption('game') f = pygame.freetype.Font(None, 60) f2 = pygame.freetype.Font(None, 30) p = 0 w = [50, 140, 230, 320, 410] l = [] t = 0 s = 0 while True: pygame.time.delay(20) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if p == 1: for i in l: if ord(i[0]) == event.key - 32: l.remove(i) s += 1 elif event.type == pygame.MOUSEBUTTONDOWN: if p == 0: p = 1 for i in range(0, 5): l.append([chr(random.randint(65, 80)), w[i], 50]) screen.fill((0, 0, 0)) if p == 0: f.render_to(screen, [180, 250], "start", fgcolor=(255, 255, 255)) if p == 1: t += 1 if t == 100: t = 0 for i in range(0, 5): l.append([chr(random.randint(65, 80)), w[i], 50]) for i in l: f.render_to(screen, [i[1], i[2]], i[0], fgcolor=(255, 255, 255)) for i in range(0, len(l)): l[i][2] += 1 if l[i][2] >= 400: p = 2 f2.render_to(screen, [5, 5], "score: %d" % s, fgcolor=(255, 255, 255)) if p == 2: f.render_to(screen, [100, 100], "game over", fgcolor=(255, 255, 255)) f.render_to(screen, [100, 250], "score: %d" % s, fgcolor=(255, 255, 255)) pygame.display.update()
可以训练人的打字能力和反应速度
截图