-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.py
More file actions
144 lines (120 loc) · 5.22 KB
/
Main.py
File metadata and controls
144 lines (120 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import pygame
from pygame.locals import *
import Maker
import time
import math
import Game
SCREEN_WIDTH = 1400
SCREEN_HEIGHT = 700
pygame.init()
increment = False
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
screen.fill((40, 40, 40))
pygame.display.set_caption("Speed Typing Test")
running = True
current_screen = "menu"
previous_screen = ""
correct_words = 0
first_time = True
display_wpm = False
letter_to_type = 0
key_record = []
shift = 1
def main_menu(mouse_pos):
global current_screen
global increment
button_colour_1 = (20, 20, 20)
button_colour_2 = (255, 0, 0)
font = pygame.font.SysFont("Rockwell", 40)
# main game text
game_text = font.render("Speed Typing Test", False, (255, 255, 255))
screen.blit(game_text, (525, 100))
# button1 - player statistics
stats_button = Maker.button((SCREEN_WIDTH / 2) - 150, SCREEN_HEIGHT - 125, 100, 50, button_colour_1, "stats")
stats_button.mouse_over(mouse_pos)
stats_button.draw(screen)
# button1 - start test
play_button = Maker.button((SCREEN_WIDTH / 2) + 50, SCREEN_HEIGHT - 125, 100, 50, button_colour_1, "start")
play_button.mouse_over(mouse_pos)
play_button.draw(screen)
for menu_event in pygame.event.get():
if menu_event.type == MOUSEBUTTONDOWN:
if play_button.mouse_click(pygame.mouse.get_pos()):
current_screen = "game"
elif stats_button.mouse_click(pygame.mouse.get_pos()):
current_screen = "stats"
def stats_screen():
pass
while running:
# updates display
if previous_screen != current_screen:
#print("previous", previous_screen, "current", current_screen)
screen.fill((40, 40, 40))
if current_screen == "game":
previous_screen = current_screen
if first_time:
start_time = time.time()
button_keys, character_list, words, letters, time_left, wpm = Game.game_screen(screen, increment, start_time, correct_words)
if time_left < 0:
current_screen = "menu"
display_wpm = True
#print("letter to type", letters[letter_to_type])
increment = False
elif current_screen == "stats":
previous_screen = current_screen
stats_screen()
elif current_screen == "menu":
previous_screen = current_screen
main_menu(pygame.mouse.get_pos())
if display_wpm:
wpm_to_type = "WPM: {}".format(round(wpm, 1))
time_text = Maker.text((255, 255, 255), 625, 380)
time_text.draw(wpm_to_type, screen)
display_wpm = False
pygame.display.flip()
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key in button_keys.keys():
if event.key == K_RSHIFT or event.key == K_LSHIFT:
shift = 2
elif event.key == K_BACKSPACE:
increment = ((character_list[letter_to_type].get_info()[0] / 2) + 1 + (character_list[letter_to_type - 1].get_info()[0] / 2)) * -1
character_list[letter_to_type - 1].set_colour("normal")
key_record.pop()
letter_to_type -= 1
elif button_keys[event.key][shift] == letters[letter_to_type]:
if first_time:
start_time = time.time()
first_time = False
increment = ((character_list[letter_to_type].get_info()[0] / 2) + 1) + (character_list[letter_to_type + 1].get_info()[0] / 2)
character_list[letter_to_type].set_colour("correct")
button_keys[event.key][0].colour_change("correct")
key_record.append(button_keys[event.key][shift])
letter_to_type += 1
elif button_keys[event.key][shift] != letters[letter_to_type]:
if first_time:
start_time = time.time()
first_time = False
increment = ((character_list[letter_to_type].get_info()[0] / 2) + 1) + (character_list[letter_to_type + 1].get_info()[0] / 2)
character_list[letter_to_type].set_colour("wrong")
button_keys[event.key][0].colour_change("wrong")
key_record.append(button_keys[event.key][shift])
letter_to_type += 1
typed_words = "".join(key_record)
typed_words = typed_words.split()
#print("typed", typed_words)
#print("words", words)
correct_words = 0
checked = []
for i in typed_words:
if i in words:
correct_words += 1
#print(correct_words)
elif event.key == K_ESCAPE:
# quits the game is the ESCAPE key is pressed
running = False
elif event.type == KEYUP:
if event.key == K_LSHIFT or event.key == K_RSHIFT:
shift = 1
if event.key in button_keys.keys():
button_keys[event.key][0].colour_change("normal")