0% found this document useful (0 votes)
6 views6 pages

Pygame Typing Speed Test Game

This document contains code for a typing speed test game created with the Pygame library. The Game class initializes attributes like the screen size and resets game variables. Methods include drawing text, getting a random sentence, showing results, running the main loop, and resetting the game. Key events handle user input and reset. On return or reset, results are calculated and displayed.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Pygame Typing Speed Test Game

This document contains code for a typing speed test game created with the Pygame library. The Game class initializes attributes like the screen size and resets game variables. Methods include drawing text, getting a random sentence, showing results, running the main loop, and resetting the game. Key events handle user input and reset. On return or reset, results are calculated and displayed.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import pygame

from [Link] import *

import sys

import time

import random

# 750 x 500

class Game:

def __init__(self):

self.w=750

self.h=500

[Link]=True

[Link] = False

self.input_text=''

[Link] = ''

self.time_start = 0

self.total_time = 0

[Link] = '0%'

[Link] = 'Time:0 Accuracy:0 % Wpm:0 '

[Link] = 0

[Link] = False

self.HEAD_C = (255,213,102)

self.TEXT_C = (240,240,240)

self.RESULT_C = (255,70,70)

[Link]()

self.open_img = [Link]('[Link]')

self.open_img = [Link](self.open_img, (self.w,self.h))

[Link] = [Link]('[Link]')

[Link] = [Link]([Link], (500,750))


[Link] = [Link].set_mode((self.w,self.h))

[Link].set_caption('Type Speed test')

def draw_text(self, screen, msg, y ,fsize, color):

font = [Link](None, fsize)

text = [Link](msg, 1,color)

text_rect = text.get_rect(center=(self.w/2, y))

[Link](text, text_rect)

[Link]()

def get_sentence(self):

f = open('[Link]').read()

sentences = [Link]('\n')

sentence = [Link](sentences)

return sentence

def show_results(self, screen):

if(not [Link]):

#Calculate time

self.total_time = [Link]() - self.time_start

#Calculate accuracy

count = 0

for i,c in enumerate([Link]):

try:

if self.input_text[i] == c:

count += 1

except:

pass

[Link] = count/len([Link])*100

#Calculate words per minute

[Link] = len(self.input_text)*60/(5*self.total_time)
[Link] = True

print(self.total_time)

[Link] = 'Time:'+str(round(self.total_time)) +" secs Accuracy:"+ str(round([Link])) + "%" + '


Wpm: ' + str(round([Link]))

# draw icon image

self.time_img = [Link]('[Link]')

self.time_img = [Link](self.time_img, (150,150))

#[Link](self.time_img, (80,320))

[Link](self.time_img, (self.w/2-75,self.h-140))

self.draw_text(screen,"Reset", self.h - 70, 26, (100,100,100))

print([Link])

[Link]()

def run(self):
self.reset_game()

[Link]=True

while([Link]):

clock = [Link]()

[Link]((0,0,0), (50,250,650,50))

[Link]([Link],self.HEAD_C, (50,250,650,50), 2)

# update the text of user input

self.draw_text([Link], self.input_text, 274, 26,(250,250,250))

[Link]()

for event in [Link]():

if [Link] == QUIT:

[Link] = False

[Link]()

elif [Link] == [Link]:

x,y = [Link].get_pos()

# position of input box


if(x>=50 and x<=650 and y>=250 and y<=300):

[Link] = True

self.input_text = ''

self.time_start = [Link]()

# position of reset box

if(x>=310 and x<=510 and y>=390 and [Link]):

self.reset_game()

x,y = [Link].get_pos()

elif [Link] == [Link]:

if [Link] and not [Link]:

if [Link] == pygame.K_RETURN:

print(self.input_text)

self.show_results([Link])

print([Link])

self.draw_text([Link], [Link],350, 28, self.RESULT_C)

[Link] = True

elif [Link] == pygame.K_BACKSPACE:

self.input_text = self.input_text[:-1]

else:

try:

self.input_text += [Link]

except:

pass

[Link]()

[Link](60)

def reset_game(self):

[Link](self.open_img, (0,0))

[Link]()

[Link](1)
[Link]=False

[Link] = False

self.input_text=''

[Link] = ''

self.time_start = 0

self.total_time = 0

[Link] = 0

# Get random sentence

[Link] = self.get_sentence()

if (not [Link]): self.reset_game()

#drawing heading

[Link]((0,0,0))

[Link]([Link],(0,0))

msg = "Typing Speed Test"

self.draw_text([Link], msg,80, 80,self.HEAD_C)

# draw the rectangle for input box

[Link]([Link],(255,192,25), (50,250,650,50), 2)

# draw the sentence string

self.draw_text([Link], [Link],200, 28,self.TEXT_C)

[Link]()

Game().run()

You might also like