少儿学编程系列---开发一个打字游戏

少儿学编程系列---开发一个打字游戏

首页休闲益智你会打字吗更新时间:2024-04-15
简介

怎么使用python的turtle开发一个打字游戏呢? 然后通过此游戏提高键盘输入的速度。

本文就开发一个这样的打字游戏,用户根据屏幕上掉下来的字母,快速敲出正确的字母,正确率越高,得分越高,如果字母到达屏幕底部你还没有敲出来,则整个游戏结束。

开发思路

根据设定的字母数N,随时都有N个随机的字母从屏幕上掉下来。当你从键盘上敲出一个正确的字母时,这个字母就会消失,取而代之的是一个从顶部随机掉落的新字母。同时,你的分数也会增加1分。当你击中一个错误的字母时,你的分数将减少1分。

为了增加游戏的难度,这些字母以随机的速度下落,也让字母下落的速度随着时间的推移逐渐增加。

当有字母到达屏幕底部你还没有敲出来,则整个游戏结束

效果

源代码

import turtle import random class LetterTypeGame: min_speed = 5 max_speed = 5 letters = [] speeds = [] pos = [] lts = [] n = 15 game_over = False score = 0 colors = ["red", "purple", "blue", "green", "orange", "yellow"] def __init__(self): self.screen = turtle.Screen() self.screen.setup(width=800, height=600) self.screen.title('打字母游戏 - cloudcoder出品') self.screen.bgcolor('sky blue') self.screen.tracer(0, 0) turtle.hideturtle() turtle.up() turtle.color('red') turtle.goto(350, 400) turtle.write('Score: ', align='center', font=('Courier', 25, 'normal')) self.score_turtle = turtle.Turtle() self.score_turtle.color('black') self.score_turtle.up() self.score_turtle.hideturtle() def increase_difficulty(self): self.min_speed = 1 self.max_speed = 1 self.screen.ontimer(self.increase_difficulty, 10000) def draw_game_over(self): turtle.goto(0, 0) turtle.color('red') turtle.write('游戏结束', align='center', font=('songti', 50, 'normal')) turtle.goto(0, -150) turtle.color('orange') turtle.write('总得分:{}'.format(self.score), align='center', font=('songti', 40, 'normal')) self.draw_score() self.screen.update() def draw_score(self): if not self.game_over: self.score_turtle.clear() self.score_turtle.goto(220, 200) self.score_turtle.write('当前得分是:{}'.format(self.score), align='center', font=('songti', 25, 'normal')) self.screen.update() def draw_letters(self): if not self.game_over: for i in range(len(self.letters)): self.lts[i].clear() self.lts[i].goto(self.pos[i]) self.lts[i].write(self.letters[i], align='center', font=('songti', 25, 'normal')) self.pos[i][1] -= self.speeds[i] if self.pos[i][1] < -300: self.game_over = True self.draw_game_over() return self.screen.update() self.screen.ontimer(self.draw_letters, 50) def f(self, c): # handle keyboard press if c in self.letters: self.score = 1 k = self.letters.index(c) while True: l = chr(ord('a') random.randrange(26)) if l not in self.letters: self.letters[k] = l break self.pos[k] = [random.randint(-350, 350), 300] self.speeds[k] = random.randint(self.min_speed, self.max_speed) else: self.score -= 1 self.draw_score() def registerLister(self): self.screen.onkey(lambda: self.f('a'), 'a') self.screen.onkey(lambda: self.f('b'), 'b') self.screen.onkey(lambda: self.f('c'), 'c') self.screen.onkey(lambda: self.f('d'), 'd') self.screen.onkey(lambda: self.f('e'), 'e') self.screen.onkey(lambda: self.f('f'), 'f') self.screen.onkey(lambda: self.f('g'), 'g') self.screen.onkey(lambda: self.f('h'), 'h') self.screen.onkey(lambda: self.f('i'), 'i') self.screen.onkey(lambda: self.f('j'), 'j') self.screen.onkey(lambda: self.f('k'), 'k') self.screen.onkey(lambda: self.f('l'), 'l') self.screen.onkey(lambda: self.f('m'), 'm') self.screen.onkey(lambda: self.f('n'), 'n') self.screen.onkey(lambda: self.f('o'), 'o') self.screen.onkey(lambda: self.f('p'), 'p') self.screen.onkey(lambda: self.f('q'), 'q') self.screen.onkey(lambda: self.f('r'), 'r') self.screen.onkey(lambda: self.f('s'), 's') self.screen.onkey(lambda: self.f('t'), 't') self.screen.onkey(lambda: self.f('u'), 'u') self.screen.onkey(lambda: self.f('v'), 'v') self.screen.onkey(lambda: self.f('w'), 'w') self.screen.onkey(lambda: self.f('x'), 'x') self.screen.onkey(lambda: self.f('y'), 'y') self.screen.onkey(lambda: self.f('z'), 'z') def start(self): for _ in range(self.n): self.lts.append(turtle.Turtle()) while True: l = chr(ord('a') random.randrange(26)) if l not in self.letters: self.letters.append(l) break self.speeds.append(random.randint(self.min_speed, self.max_speed)) self.pos.append([random.randint(-350, 350), random.randint(250, 300)]) for i in range(self.n): self.lts[i].speed(0) self.lts[i].hideturtle() self.lts[i].color(self.colors[random.randint(0, 6) % 6]) self.lts[i].up() self.draw_letters() self.increase_difficulty() self.registerLister() self.screen.listen() self.screen.mainloop() if __name__ == '__main__': game = LetterTypeGame() game.start()

查看全文
大家还看了
也许喜欢
更多游戏

Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved