用python制作应用程序
当普通人想要学习打字时,他们会使用Typing master之类的软件。 但是由于我们是程序员,所以我们可以使用我们的知识来编写自己的打字导师应用。 一如既往,Python将会是最好的选择,因为它易于理解,并为我们的特定目的提供了很多库。 让我们开始吧!
为了检查我们的打字速度和准确性,我们需要记录击键。 为此,我们将使用一个名为tkinter的python库。 由于tkinter已从Python 3.4及更高版本内置,因此您无需使用pip进行安装。
这个应用程式如何运作?
首先,我们将获取许多英文字母的数据集。 然后,我们将从该数据集中向用户随机呈现单词。 用户必须输入这些单词。 如果他成功键入一个单词,他将得到下一个单词。 同样,用户将获得特定数量的生命。 一旦用户输入错误的字母,他将失去生命。 如果他的生活结束了,比赛就结束了。 而且,基于用户键入特定单词所花费的时间以及正确键入的字母数来计算分数。
此项目中使用的英语单词的数据集可以在这里找到。 下载此文件并将其存储在存在python文件的相同位置。 该文件包含大约10,000个英语单词。 我们将从列表中随机选择给定数量的单词。 对于分数计算,每当用户正确按下一个字母时,我们就将分数加1。 还为每个单词分配了时间限制,该时间限制是单词长度的倍数。 如果用户在时间限制之前输入单词,则剩余时间将添加到得分中。
代码 。
import tkinter as tk
import random
from os import system, name
import time
def clear () :
if name == 'nt' :
_ = system( 'cls' )
else :
_ = system( 'clear' )
index = 0
list_index = 0
list = []
word_count = 10
f = open( 'words.txt' , 'r' )
for line in f:
list.append(line.strip())
random.shuffle(list)
list = list[:word_count]
print( "---WELCOME TO TYPING TUTOR---" )
time.sleep( 1 )
clear()
print( "Total words: " , len(list))
time.sleep( 2 )
clear()
print( "Word " +str(list_index+ 1 )+ " out of " +str(word_count)+ ": " +list[list_index])
start_time = time.time()
end_time = 0
time_multiplier = 2
lives = 3
score = 0
def keypress (event) :
global index
global list_index
global list
global lives
global score
global start_time
global end_time
global time_multiplier
word = list[list_index]
if event.char == word[index]:
index = index + 1
score = score + 1
else :
clear()
print( "Word " + str(list_index + 1 ) + " out of " + str(word_count) + ": " + list[list_index])
print( "wrong letter!" )
lives = lives - 1
print( "Lives left: " , lives)
if lives == 0 :
print( "Game Over!" )
print( "Final Score: " , score)
root.destroy()
return
if index == len(word):
clear()
print( "right!" )
index = 0
list_index = list_index + 1
end_time = time.time()
time_taken = int(end_time - start_time)
time_left = time_multiplier * len(word) - time_taken
score = score + time_left
print( "time taken: " + str(time_taken) + " out of " + str(time_multiplier*len(word)) + " seconds." )
print( "Current score: " , score)
time.sleep( 1.5 )
start_time = end_time
clear()
if list_index < len(list) and index == 0 :
print( "Word " + str(list_index + 1 ) + " out of " + str(word_count) + ": " + list[list_index])
elif list_index == len(list):
clear()
print( "Congratulations! you have beaten the game!" )
print( "Final Score: " , score)
root.destroy()
root = tk.Tk()
root.bind_all( '' , keypress)
root.withdraw()
root.mainloop()
将此代码复制到新的python文件,并将其命名为app.py。 可以使用word_count
变量设置游戏中显示的单词总数。 当前设置为time_multiplier
变量控制分配给每个单词的时间。 当前设置为2。这意味着对于长度为5的单词,时间限制为5 * 2 = 10秒。 lives
变量定义玩家获得的生活数量。 每当玩家错误地键入字母时,生命都会过去。
要运行此代码,请打开命令提示符,将目录更改为该python文件的存储位置,然后键入: python app.py
请注意,运行此代码后,命令提示符将不会保留为活动窗口。 您不必担心。 只需开始输入显示的单词的字母即可。 当您继续正确输入时,乐谱将更新,并且接下来的单词将继续出现。 另外,您实际上不会在屏幕上的任何地方看到要键入的单词。 您只需要继续按正确的键即可。 每当您按下任何不正确的键时,都会扣除生命。
而已。 希望您喜欢这篇文章。 如有任何疑问,请发表评论。 还可以查看我在Medium上的文章,并在Twitter上关注我。 还可以在此博客上查看我的其他文章!
翻译自: https://hackernoon.com/make-your-own-typing-tutor-app-using-python-6i19734se
用python制作应用程序