defget_random_word(): """ 获取随机的字母,使用ascii码 :return: x, y, 字母, 字母颜色 """ color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) # 颜色随机 x = random.randint(100, 800 - 100) # x坐标从左右边距各100之间随机 y = 0 word = random.randint(65, 90) return [x, y, word, color]
arr = [get_random_word()] # 创建初始列表
state = 1# 1.进行中 2.失败 flash = 1 whileTrue: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit()
if state == 1andlen(arr) > 0and event.type == KEYDOWN: if event.key == arr[0][2] + 32: # 大小写字母差32 arr.pop(0) # 如果打字正确,从列表中弹出
screen.fill((255, 255, 255)) # 白色背景
for i inrange(len(arr)): # 绘制这些字母 x, y, word, c = arr[i] if i == 0and flash: c = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) # 随机颜色 textImage = myfont.render(chr(word), True, c) screen.blit(textImage, (x, y))
if state == 2: # 失败,结束程序 sys.exit()
if state == 1: if pygame.time.get_ticks() >= word_ticks: # 计时增加新字母 word_ticks += word_diff_ticks arr.append(get_random_word())
if pygame.time.get_ticks() >= ticks: ticks += diff_ticks flash = 1 - flash for i inrange(len(arr)): arr[i][1] += 1 iflen(arr) > 0and arr[0][1] > 600: # 闪烁的字母的y坐标大于600 state = 2