上位机软件(C/Python/Java等)
直播中

中培

4年用户 90经验值
擅长:可编程逻辑 MEMS/传感技术 嵌入式技术 EMC/EMI设计 EDA/IC设计 处理器/DSP
私信 关注
[资料]

如何使用Python制作自己的打字导师应用程序?

  当普通人想要学习打字时,他们会使用诸如Typing master之类的软件 。但是程序员们可以使用他们的知识来编写自己的打字导师应用。一如既往,Python将会是最好的选择,因为它易于理解,并为我们的特定目的提供了很多库。下面让我们来看一下程序员如何使用Python制作自己的打字导师应用程序吧。为了检查打字速度和准确性,我们需要记录击键。为此,我们将使用一个称为tkinter的python库www.zpedu.com/it/rjyf/12670.html。由于tkinter已从Python 3.4及更高版本内置,因此您无需使用pip进行安装。
  这个应用程式如何运作?
  首先,我们将获取许多英文字母的数据集。然后,我们将从该数据集中向用户随机呈现单词。用户必须输入这些单词。如果他成功键入一个单词,他将得到下一个单词。同样,用户将获得特定的生命。一旦用户输入错误的字母,他将失去生命。如果他的生活结束了,比赛就结束了。而且,基于用户键入特定单词所花费的时间以及正确键入的字母数来计算分数。
  在此项目中使用的英语单词数据集可以在这里找到 。下载此文件并将其存储在存在python文件的相同位置。该文件包含大约10,000个英语单词。我们将从列表中随机选择给定数量的单词。对于分数计算,每当用户正确按下一个字母时,我们就将分数加1。还为每个单词分配了时间限制,该时间限制是单词长度的倍数。如果用户在时间限制之前输入单词,则剩余时间将添加到得分中。
  代码展示:
  1.   import tkinter as tk


  2.   import random


  3.   from os import system, name


  4.   import time


  5.   def clear():


  6.   if name == 'nt':


  7.   _ = system('cls')


  8.   else:


  9.   _ = system('clear')


  10.   index = 0


  11.   list_index = 0


  12.   list = []


  13.   word_count = 10


  14.   f = open('words.txt', 'r')


  15.   for line in f:


  16.   list.append(line.strip())


  17.   random.shuffle(list)


  18.   list = list[:word_count]


  19.   print("---WELCOME TO TYPING TUTOR---")


  20.   time.sleep(1)


  21.   clear()


  22.   print("Total words: ", len(list))


  23.   time.sleep(2)


  24.   clear()


  25.   print("Word "+str(list_index+1)+" out of "+str(word_count)+":

  26. "+list[list_index])


  27.   start_time = time.time()


  28.   end_time = 0


  29.   time_multiplier = 2


  30.   lives = 3


  31.   score = 0


  32.   def keypress(event):


  33.   global index


  34.   global list_index


  35.   global list


  36.   global lives


  37.   global score


  38.   global start_time


  39.   global end_time


  40.   global time_multiplier


  41.   word = list[list_index]


  42.   if event.char == word[index]:


  43.   index = index + 1


  44.   score = score + 1


  45.   else:


  46.   clear()


  47.   print("Word " + str(list_index + 1) + " out of " + str(word_count) + ": " +

  48. list[list_index])


  49.   print("wrong letter!")


  50.   lives = lives - 1


  51.   print("Lives left: ", lives)


  52.   if lives == 0:


  53.   print("Game Over!")


  54.   print("Final Score: ", score)


  55.   root.destroy()


  56.   return


  57.   if index == len(word):


  58.   clear()


  59.   print("right!")


  60.   index = 0


  61.   list_index = list_index + 1


  62.   end_time = time.time()


  63.   time_taken = int(end_time - start_time)


  64.   time_left = time_multiplier * len(word) - time_taken


  65.   score = score + time_left


  66.   print("time taken: " + str(time_taken) + " out of " +

  67. str(time_multiplier*len(word)) + " seconds.")


  68.   print("Current score: ", score)


  69.   time.sleep(1.5)


  70.   start_time = end_time


  71.   clear()


  72.   if list_index < len(list) and index == 0:


  73.   print("Word " + str(list_index + 1) + " out of " + str(word_count) + ": " +

  74. list[list_index])


  75.   elif list_index == len(list):


  76.   clear()


  77.   print("Congratulations! you have beaten the game!")


  78.   print("Final Score: ", score)


  79.   root.destroy()


  80.   root = tk.Tk()


  81.   root.bind_all('', keypress)


  82.   root.withdraw()


  83.   root.mainloop()


  这段代码复制到一个新的Python文件并将它命名为 app.py。可以使用以下命令设置游戏中显示的单词总数 word_count 变量。当前设置为100。 time_multiplier 变量控制分配给每个单词的时间。当前设置为2。这意味着对于长度为5的单词,时间限制为5 * 2 = 10秒。lives 变量定义玩家的生命数量。每当玩家错误地键入字母时,生命都会过去。
  要运行此代码,请打开命令提示符,将目录更改为该python文件的存储位置,然后键入: python app.py
  请注意,运行此代码后,命令提示符将不会保留为活动窗口。您不必担心。只需开始输入显示的单词的字母即可。当您继续正确输入时,乐谱将更新,接下来的单词将继续出现。另外,您实际上不会在屏幕上的任何地方看到要键入的单词。您只需要继续按正确的键即可。每当您按下任何不正确的键时,都会扣除生命。
  通过上述介绍,如何使用Python制作自己的打字导师应用程序相信大家也已经知晓了吧,想了解更多关于Python的信息,请继续关注我们。

更多回帖

发帖
×
20
完善资料,
赚取积分