Python
Python
import sys
import time
import random
# 750 x 500
class Game:
def __init__(self):
self.w=750
self.h=500
self.reset=True
self.active = False
self.input_text=''
self.word = ''
self.time_start = 0
self.total_time = 0
self.accuracy = '0%'
self.wpm = 0
self.end = False
self.HEAD_C = (255,213,102)
self.TEXT_C = (240,240,240)
self.RESULT_C = (255,70,70)
pygame.init()
self.open_img = pygame.image.load('type-speed-open.png')
self.bg = pygame.image.load('background.jpg')
screen.blit(text, text_rect)
pygame.display.update()
def get_sentence(self):
f = open('sentences.txt').read()
sentences = f.split('\n')
sentence = random.choice(sentences)
return sentence
if(not self.end):
#Calculate time
#Calculate accuracy
count = 0
try:
if self.input_text[i] == c:
count += 1
except:
pass
self.accuracy = count/len(self.word)*100
self.wpm = len(self.input_text)*60/(5*self.total_time)
self.end = True
print(self.total_time)
self.time_img = pygame.image.load('icon.png')
#screen.blit(self.time_img, (80,320))
screen.blit(self.time_img, (self.w/2-75,self.h-140))
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_C, (50,250,650,50), 2)
pygame.display.update()
if event.type == QUIT:
self.running = False
sys.exit()
x,y = pygame.mouse.get_pos()
self.active = True
self.input_text = ''
self.time_start = time.time()
self.reset_game()
x,y = pygame.mouse.get_pos()
if event.key == pygame.K_RETURN:
print(self.input_text)
self.show_results(self.screen)
print(self.results)
self.end = True
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_img, (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
self.word = self.get_sentence()
#drawing heading
self.screen.fill((0,0,0))
self.screen.blit(self.bg,(0,0))
pygame.draw.rect(self.screen,(255,192,25), (50,250,650,50), 2)
pygame.display.update()
Game().run()