-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtyping.py
145 lines (137 loc) · 6.68 KB
/
typing.py
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
145
import pygame
import sys
import math
import textwrap
import time
from Settings import settings
from Player import player
from Draw import draw
from Keys import keys
class typing:
"""Class to manage overall game assets and behaviours"""
def __init__(self):
pygame.init()
pygame.font.init()
self.settings = settings()
self.player = player()
self.draw = draw()
self.keys = keys()
self.screen = pygame.display.set_mode((self.settings.WIDTH, self.settings.HEIGHT))
pygame.display.set_caption(self.settings.CAPTION)
self.clock = pygame.time.Clock()
self.time_font = pygame.font.Font('freesansbold.ttf', 60)
self.text_font = pygame.font.Font('freesansbold.ttf', 20)
self.end_font = pygame.font.Font('freesansbold.ttf', 30)
self.start_font = pygame.font.Font('freesansbold.ttf', 100)
with open ("texts/text.txt") as f:
contents = f.readlines()
for n,line in enumerate(contents):
if line.startswith("line"):
contents[n] = "\n"+line.rstrip()
else:
contents[n]=line.rstrip()
self.contents = ' '.join(contents)
self.text_width, self.text_height = self.time_font.size(self.contents)
self.lines = int(math.ceil(self.text_width / self.settings.WIDTH /2 ))
self.groups = int(len(self.contents) / self.lines)
self.line_contents = textwrap.wrap(self.contents, self.groups)
for n, line in enumerate(self.line_contents):
if n < len(self.line_contents):
self.line_contents[n] = self.line_contents[n] + " "
def run_game(self):
"""Run the game"""
while True:
self.minutes = 0
self.seconds = 0
self.ms = 0
self.typed_letters = []
self.key_typed = ''
self.btn_hover = False
while True:
self.screen.fill(self.settings.bg_col)
self.draw.draw_start_text(self.screen, self.settings.text_col, self.start_font, self.btn_hover)
pygame.display.flip()
mousex, mousey = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if mousex > self.draw.play_x and mousex < self.draw.play_x + self.draw.play_width and mousey > self.draw.play_y and mousey < self.draw.play_y + self.draw.play_height:
self.btn_hover = True
if click[0] == 1:
break
else:
self.btn_hover = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
while True:
self.btn_hover = False
self.clock.tick(self.settings.FPS)
self.ms += 20
if self.ms >= 1000:
self.seconds += 1
self.ms = 0
if self.seconds >= 60:
self.minutes += 1
self.seconds = 0
str_minutes = str(self.minutes)
str_seconds = str(self.seconds)
str_ms = str(self.ms)
str_time = f"{str_minutes}:{str_seconds}:{str_ms}"
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
for key in self.keys.keys:
if event.key == pygame.K_BACKSPACE:
try:
self.typed_letters.pop()
except IndexError as e:
self.typed_letters = []
break
elif event.key == key:
self.key_typed = self.keys.keys[key]
self.typed_letters.append(self.key_typed)
mods = pygame.key.get_mods()
if mods == 1 or mods == 2:
self.typed_letters.pop()
self.key_typed = self.key_typed.upper()
self.typed_letters.append(self.key_typed)
self.key_typed = ''
mods = 0
break
self.key_typed = ''
break
self.screen.fill(self.settings.bg_col)
###Draw itsems here###
self.draw.draw_timer(self.screen, str_time, self.settings.font_col, self.time_font)
self.draw.draw_words(self.screen, self.line_contents, self.typed_letters, self.settings.text_col, self.settings.correct_col, self.settings.wrong_col, self.text_font)
#####################
pygame.display.flip()
if len(self.typed_letters) >= len(self.contents):
break
self.letters_right, self.wpm, self.typing_percentage, self.text_len, self.minutes = self.player.get_score(self.typed_letters, self.line_contents, self.minutes, self.seconds)
display_mins = self.minutes
if display_mins < 1:
display_mins = 0
display_mins = round(display_mins)
self.message_1 = (f"You got {self.letters_right} letters correct out of {self.text_len} letters in {display_mins}:{self.seconds}.{self.ms}")
self.message_2 = (f"You typed at {self.wpm} words per minute on {self.typing_percentage}% accuracy")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
self.screen.fill(self.settings.bg_col)
mousex, mousey = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if mousex > self.settings.play_again_x and mousex < self.settings.play_again_x + self.settings.play_again_width and mousey > self.settings.play_again_y and mousey < self.settings.play_again_y + self.settings.play_again_height:
self.btn_hover = True
if click[0] == 1:
break
else:
self.btn_hover = False
##Draw Items Here##
self.draw.draw_end_text(self.screen, self.message_1, self.message_2, self.settings.font_col, self.end_font, self.btn_hover)
###################
pygame.display.flip()
if __name__ == '__main__':
t = typing()
t.run_game()