用户:姚国栋udn0查看:0 回复:0 评论:0 创建时间:2023-12-29T19:13:04
【作品展示】

【作品介绍】
打字机游戏
【作品源代码】
import tkinter as tk
import random
from time import time
class TypingGame:
def __init__(self, root):
self.root = root
self.root.title("打字机游戏")
self.word_list = ["apple", "banana", "orange", "strawberry", "pineapple"]
self.current_word = tk.StringVar()
self.input_word = tk.StringVar()
self.start_time = 0
self.end_time = 0
self.create_widgets()
def create_widgets(self):
self.word_label = tk.Label(self.root, textvariable=self.current_word, font=("Arial", 24))
self.word_label.pack(pady=20)
self.entry = tk.Entry(self.root, textvariable=self.input_word, font=("Arial", 24))
self.entry.pack(pady=10)
self.entry.bind("", self.check_word)
self.start_button = tk.Button(self.root, text="开始", command=self.start_game)
self.start_button.pack(pady=10)
def start_game(self):
self.current_word.set(random.choice(self.word_list))
self.input_word.set("")
self.start_time = time()
def check_word(self, event):
if self.input_word.get() == self.current_word.get():
self.end_time = time()
elapsed_time = self.end_time - self.start_time
wpm = len(self.current_word.get()) / (elapsed_time / 60)
result = "你打字的速度为: {:.2f} 字/分钟".format(wpm)
self.current_word.set(result)
self.start_game()
if __name__ == "__main__":
root = tk.Tk()
game = TypingGame(root)
root.mainloop()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn