用Python键入速度跟踪器
我一直在学习如何使用pygame制作打字速度计算器的教程,每当我运行代码时,所有内容都会按应有的方式呈现出来,但当我尝试单击文本字段输入一些文本时,它只是冻结,什么也没有发生。我试图在网上搜索或观看教程,但结果都是空的用Python键入速度跟踪器,python,pygame,Python,Pygame,我一直在学习如何使用pygame制作打字速度计算器的教程,每当我运行代码时,所有内容都会按应有的方式呈现出来,但当我尝试单击文本字段输入一些文本时,它只是冻结,什么也没有发生。我试图在网上搜索或观看教程,但结果都是空的 import sys import time import random import pygame from pygame.locals import * """Python Script for a typing speed test game
import sys
import time
import random
import pygame
from pygame.locals import *
"""Python Script for a typing speed test game"""
class Game(object):
def __init__(self):
self.h = 720
self.w = 1280
self.reset = True
self.active = False
self.word = ""
self.input_text = ""
self.time_start = 0
self.total_time = 0
self.accuracy = "0%"
self.wpm = 0
self.results = "Speed(WPM):0 Accuracy:0% Time:0"
self.end = False
self.head_col = (255,213,102)
self.text_col = (255,255,255)
self.result_color = (53,209,29)
pygame.init()
self.open_image = pygame.image.load("splash.jpg")
self.open_image = pygame.transform.scale(self.open_image, (self.w,self.h))
self.background = pygame.image.load("background.jpg")
self.background = pygame.transform.scale(self.background, (self.w, self.h))
self.screen = pygame.display.set_mode((self.w,self.h))
pygame.display.set_caption = "Type Speed Test"
def draw_text(self, screen, msg, y, fsize, color):
font = pygame.font.SysFont("consolas", fsize)
text = font.render(msg, 1, color)
text_rect = text.get_rect(center=(self.w/2, y))
screen.blit(text, text_rect)
pygame.display.update()
def get_sentences(self):
f = open("sentences.txt").read()
sentence = f.split("\n")
sentences = random.choice(sentence)
return sentences
def show_results(self, screen):
if(not self.end):
#Calculate Time
self.total_time = time.Time() - self.time_start
#Calcualte Accuracy
count = 0
for i,c in enumarate(self.word):
try:
if self.input_text[i] == c:
count += 1
except:
pass
self.accuracy = count/len(self.word)*100
#Calculate wpm
self.wpm = len(self.input_text)*60/(5*self.total_time)
self.end = True
print(self.total_time)
self.results = "Time:" + str(round(self.total_time))
+ " secs Accuracy: " + str(round(self.accuracy)) + "%"
+ " WPM: " + str(round(self.wpm))
#restart icon
self.time_img = pygame.image.load("icon.png")
self.time_img = pygame.transform.scale(self.time_img, (80,320))
#screen.blit(self.time_img, (80,320))
screen.blit(self.time_img, (self.w/2-75, self.h-140))
self.draw_text(screen, "Reset", self/h - 70, 26, (100,100,100))
print(self.results)
pygame.display.update()
def run(self):
self.reset_game()
self.running=True
while(self.running):
clock = pygame.time.Clock()
self.screen.fill((0,0,0), (50,250,650,50))
pygame.draw.rect(self.screen,self.head_col, (50,250,650,50), 2)
# update the text of user input
self.draw_text(self.screen, self.input_text, 274, 26,(250,250,250))
pygame.display.update()
for event in pygame.event.get():
if event.type == QUIT:
self.running = False
sys.exit()
elif event.type == pygame.MOUSEBUTTONUP:
x,y = pygame.mouse.get_pos()
# position of input box
if(x>=50 and x<=650 and y>=250 and y<=300):
self.active = True
self.input_text = ''
self.time_start = time.time()
# position of reset box
if(x>=310 and x<=510 and y>=390 and self.end):
self.reset_game()
x,y = pygame.mouse.get_pos()
elif event.type == pygame.KEYDOWN:
if self.active and not self.end:
if event.key == pygame.K_RETURN:
print(self.input_text)
self.show_results(self.screen)
print(self.results)
self.draw_text(self.screen, self.results,350, 28, self.RESULT_C)
self.end = True
elif event.key == pygame.K_BACKSPACE:
self.input_text = self.input_text[:-1]
else:
try:
self.input_text += event.unicode
except:
pass
pygame.display.update()
clock.tick(60)
def reset_game(self):
self.screen.blit(self.open_image, (0,0))
pygame.display.update()
time.sleep(1)
self.reset=False
self.end = False
self.input_text=''
self.word = ''
self.time_start = 0
self.total_time = 0
self.wpm = 0
# Get random sentence
self.word = self.get_sentences()
if (not self.word): self.reset_game()
#drawing heading
self.screen.fill((0,0,0))
self.screen.blit(self.background,(0,0))
msg = "Typing Speed Test"
self.draw_text(self.screen, msg,80, 80,self.head_col)
# draw the rectangle for input box
pygame.draw.rect(self.screen,(255,192,25), (50,250,650,50), 2)
# draw the sentence string
self.draw_text(self.screen, self.word,200, 28,self.text_col)
pygame.display.update()
Game().run()
导入系统
导入时间
随机输入
导入pygame
从pygame.locals导入*
“”“键入速度测试游戏的Python脚本”“”
班级游戏(对象):
定义初始化(自):
self.h=720
self.w=1280
self.reset=True
self.active=False
self.word=“”
self.input_text=“”
self.time\u start=0
self.total_time=0
self.accurity=“0%”
self.wpm=0
self.results=“速度(WPM):0精度:0%时间:0”
self.end=False
self.head_col=(255213102)
self.text_col=(255255)
self.result_color=(53209,29)
pygame.init()
self.open_image=pygame.image.load(“splash.jpg”)
self.open_image=pygame.transform.scale(self.open_image,(self.w,self.h))
self.background=pygame.image.load(“background.jpg”)
self.background=pygame.transform.scale(self.background,(self.w,self.h))
self.screen=pygame.display.set_模式((self.w,self.h))
pygame.display.set_caption=“类型速度测试”
def绘图文本(自身、屏幕、消息、y、fsize、颜色):
font=pygame.font.SysFont(“控制台”,fsize)
text=font.render(消息,1,颜色)
text_rect=text.get_rect(中心=(self.w/2,y))
screen.blit(text,text_rect)
pygame.display.update()
def get_语句(自我):
f=打开(“句子.txt”).read()
句子=f.split(“\n”)
句子=随机。选择(句子)
复句
def显示结果(自身、屏幕):
如果(不是self.end):
#计算时间
self.total\u time=time.time()-self.time\u start
#计算精度
计数=0
对于枚举中的i,c(self.word):
尝试:
如果self.input_text[i]==c:
计数+=1
除:
通过
self.accurity=计数/长度(self.word)*100
#计算wpm
self.wpm=len(自输入文本)*60/(5*自总时间)
self.end=True
打印(总打印时间)
self.results=“Time:+str(舍入(self.total_Time))
+“秒精度:”+str(舍入(自身精度))+“%”
+WPM:+str(圆形(self.WPM))
#重新启动图标
self.time\u img=pygame.image.load(“icon.png”)
self.time\u img=pygame.transform.scale(self.time\u img,(80320))
#屏幕。blit(自时间\u img,(80320))
屏幕blit(自我时间(自我w/2-75,自我h-140))
自绘制文本(屏幕,“重置”,自/h-70,26,(100100))
打印(self.results)
pygame.display.update()
def运行(自):
self.reset_游戏()
self.running=True
同时(自运行):
clock=pygame.time.clock()
自我筛选填充((0,0,0),(50250650,50))
pygame.draw.rect(self.screen,self.head_col,(50250650,50),2)
#更新用户输入的文本
self.draw_文本(self.screen,self.input_文本,274,26,(250250))
pygame.display.update()
对于pygame.event.get()中的事件:
如果event.type==退出:
self.running=False
sys.exit()
elif event.type==pygame.MOUSEBUTTONUP:
x、 y=pygame.mouse.get_pos()
#输入框的位置
如果(x>=50,x=250,y=310,x=390和self.end):
self.reset_游戏()
x、 y=pygame.mouse.get_pos()
elif event.type==pygame.KEYDOWN:
如果self.active而非self.end:
如果event.key==pygame.K_返回:
打印(自行输入文本)
self.show_结果(self.screen)
打印(self.results)
self.draw_文本(self.screen,self.results,350,28,self.RESULT_C)
self.end=True
elif event.key==pygame.K_退格:
self.input_text=self.input_text[:-1]
其他:
尝试:
self.input_text+=event.unicode
除:
通过
pygame.display.update()
时钟滴答(60)
def重置_游戏(自我):
self.screen.blit(self.open_图像,(0,0))
pygame.display.update()
时间。睡眠(1)
self.reset=False
self.end=False
self.input_text=“”
self.word=''
self.time\u start=0
self.total_time=0
self.wpm=0
#随机造句
self.word=self.get_句子()
if(非self.word):self.reset_game()
#图纸标题
自我筛选填充((0,0,0))
自我屏幕blit(自我背景,(0,0))
msg=“打字速度测试”
self.draw\u text(self.screen,msg,80,80,self.head\u col)
#为输入框绘制矩形
pygame.draw.rect(self.screen,(255192,25),(50250650,50),2)
#画句子串
self.draw_text(self.screen,self.word,200,28,self.text_col)
pygame.display.update()
Game().run()
如何修复此问题?在Game.reset\u Game中,您反复调用Game.reset\u Game(),但不退出 取消对self.reset_游戏的调用,以便可以重置游戏 <
class Game(object):
...
def run(self):
...
while(self.running):
...
for event in pygame.event.get():
if event.type == QUIT:
...
elif event.type == pygame.MOUSEBUTTONUP:
...
elif event.type == pygame.KEYDOWN:
...
class Game(object):
...
def run(self):
...
while(self.running):
...
for event in pygame.event.get():
if event.type == QUIT:
...
elif event.type == pygame.MOUSEBUTTONUP:
...
elif event.type == pygame.KEYDOWN:
...