Answer to Question #245622 in Python for vikash

Question #245622

Speed Typing Test

In this assignment, let's build a Speed Typing Test by applying the concepts we learned till now.

Refer to the below image.

https://assets.ccbp.in/frontend/content/dynamic-webapps/speed-typing-test-output.gif



1
Expert's answer
2021-10-03T04:01:19-0400
import pygame
from pygame.locals import *
import sys
import time
import random


class Type:


   def __init__(api):
        api.w=600
        api.h=400
        api.reset=True
        api.active = False
        api.in_text=''
        api.word = ''
        api.Ts = 0
        api.Tt= 0
        api.accuracy = '0%'
        api.res = 'Time:0 Accuracy:0 % Wpm:0 '
        api.wpm = 0
        api.end = False
        api.TOP_C = (255,213,102)
        api.TXT_C = (240,240,240)
        api.OUTPUT_C = (255,70,70)




        pygame.init()
        api.open_img = pygame.image.load(r'C:/Users/Faith Chela/Desktop/type-speed-open.png')#the image directory
        api.open_img = pygame.transform.scale(api.open_img, (api.w,api.h))




        api.bg = pygame.image.load(r'C:/Users/Faith Chela/Desktop/background.jpg')#image directory
        api.bg = pygame.transform.scale(api.bg, (400,600))


        api.screen = pygame.display.set_mode((api.w,api.h))
        pygame.display.set_caption('Type Speed test')
        
        
   def write_text(api, show, message, y ,fsize, color):
    font = pygame.font.Font(None, fsize)
    text = font.render(message, 1,color)
    text_rect= text.get_rect(center=(api.w/2, y))
    show.blit(text, text_rect)
    pygame.display.update()
    
    
   def output_sentence(self):
    f = open(r'C:/Users/Faith Chela/Desktop/sentences.txt').read()
    sentences = f.split('\n')
    sentence = random.choice(sentences)
    return sentence




   def display_results(api, show):
    if(not api.end):
        api.Tt = time.time() - api.time_start
        count = 0
        for i,c in enumerate(api.word):
            try:
                if api.in_text[i] == c:
                    count += 1
            except:
                pass
        api.accuracy = count/len(api.word)*100
        api.wpm = len(api.in_text)*60/(5*api.Tt)
        api.end = True
        print(api.Tt)
        api.res = 'Time:'+str(round(api.Tt)) +" secs Accuracy:"+ str(round(api.accuracy)) + "%" + ' Wpm: ' + str(round(api.wpm))
        api.time_img = pygame.image.load('icon.png')
        api.time_img = pygame.transform.scale(api.time_img, (130,130))
        show.blit(api.time_img, (api.w/2-75,api.h-140))
        api.draw_text(show,"Reset", api.h - 60, 28, (120,120,120))
        print(api.res)
        pygame.display.update()
        
        
   def run(api):
    api.reset_game()
    api.run=True
    while(api.run):
        clock = pygame.time.Clock()
        api.screen.fill((0,0,0), (50,250,650,50))
        pygame.draw.rect(api.screen,api.TOP_C, (50,250,650,50), 2)
        api.write_text(api.screen, api.input_text, 274, 26,(250,250,250))
        pygame.display.update()
        for event in pygame.event.get():
            if event.type == QUIT:
                api.run = False
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONUP:
                x,y = pygame.mouse.get_pos()
                if(x>=50 and x<=650 and y>=250 and y<=300):
                    api.active = True
                    api.in_text = ''
                    api.Ts = time.time()
                    if(x>=310 and x<=510 and y>=390 and api.end):
                      api.reset_game()
                      x,y = pygame.mouse.get_pos()
            elif event.type == pygame.KEYDOWN:
                if api.active and not api.end:
                    if event.key == pygame.K_RETURN:
                        print(self.input_text)
                        api.screen_res(api.screen)
                        print(api.res)
                        api.draw_text(api.screen, api.results,350, 28, api.OUTPUT_C)
                        api.end = True
                    elif event.key == pygame.K_BACKSPACE:
                        api.in_text = api.input_text[:-1]
                    else:
                        try:
                            api.in_text += event.unicode
                        except:
                            pass
        pygame.display.update()
    clock.tick(60)
    
    
   def reset_game(api):
        api.screen.blit(api.open_img, (0,0))
        pygame.display.update()
        time.sleep(1)
        api.reset=False
        api.end = False
        api.input_text=''
        api.word = ''
        api.Ts = 0
        api.Tt = 0
        api.wpm = 0
        api.word = api.output_sentence()
        if (not api.word): api.reset_game()
        api.screen.fill((0,0,0))
        api.screen.blit(api.bg,(0,0))
        message = "Typing Speed Test"
        api.write_text(api.screen, message,50, 50,api.TOP_C)
        pygame.draw.rect(api.screen,(255,192,25), (50,250,650,50), 2)
        api.write_text(api.screen, api.word,200, 28,api.TXT_C)
        pygame.display.update()
Type().run()



Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS