-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
252 lines (183 loc) · 6.59 KB
/
main.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
from random import shuffle
from typing import List
import pygame
import pygame_textinput
from data_structures import Rolling_hash
def main():
# init all packages
pygame.init()
# screen
screen_x = 930
screen_y = 700
screen = pygame.display.set_mode((screen_x, screen_y))
clock = pygame.time.Clock()
# Title and game icon
pygame.display.set_caption("Pygame Typing Challenge")
icon = pygame.image.load("images/profanity.png")
pygame.display.set_icon(icon)
# Backgroud music
pygame.mixer.music.load("background_music.wav")
pygame.mixer.music.play(-1)
# tick tock timer sound
omnious_sound = pygame.mixer.Sound("tick_tock.wav")
# Create TextInput-object
textinput = pygame_textinput.TextInput()
text_input_x = 7
text_input_y = screen_y - 77
# font
font = pygame.font.Font("FE.ttf", 32)
heading_font = pygame.font.Font("CQ.ttf", 40)
end_font = pygame.font.Font("Mallvirra.ttf", 75)
win_font = pygame.font.Font("Mallvirra.ttf", 50)
# number of words to be displayed, score and timer
number_of_words = 20
score = 0
total_time = 60 * 1000 # in milliseconds
rem_time = total_time
score_x, score_y = 10, 20
time_x, time_y = screen_x - 167, 20
valid_characters = set()
# add all alphanumeric to valid_characters
for i in range(26):
ch = chr(ord("a") + i)
caps = ch.upper()
valid_characters.add(ch)
valid_characters.add(caps)
for num in range(10):
valid_characters.add(num)
# load the word_corpus and get the word_list
with open("words_corpus.txt", "r") as f:
raw_word_list = f.readlines()
raw_word_list = list(map(lambda x: x.strip(), raw_word_list))
raw_word_list = list(filter(lambda x: x.isalnum(), raw_word_list))
shuffle(raw_word_list)
word_list = raw_word_list[:number_of_words]
word_clear = [False for _ in range(len(word_list))]
word_grid = []
for x in [15, 250, 500, 750]:
for y in range(100, text_input_y - 50, 100):
word_grid.append((x, y))
def draw_headers():
score_text = heading_font.render(
"Score: " + str(score), True, (0, 0, 0)
)
time_text = heading_font.render(
"Timer: " + str(rem_time // 1000), True, (0, 0, 0)
)
screen.blit(score_text, (score_x, score_y))
screen.blit(time_text, (time_x, time_y))
def draw_words(word_list: List[str], word_clear: List[bool]):
index = 0
for word_index, word in enumerate(word_list):
if word_clear[word_index]:
continue
word_pos = word_grid[index]
word_text = font.render(str(word), True, (0, 0, 0))
screen.blit(word_text, word_pos)
index += 1
# Hash object
hash_object = Rolling_hash(word_list)
running = True
time_over = False
win = False
# Game loop
while running:
if win:
screen.fill((175, 141, 19))
win_text_first_line = win_font.render(
"None are so empty as those",
True,
(0, 0, 0),
)
screen.blit(win_text_first_line, (200, screen_y // 2 - 100))
win_text_second_line = win_font.render(
"who are full of themselves",
True,
(0, 0, 0),
)
screen.blit(win_text_second_line, (200, screen_y // 2 - 50))
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
running = False
pygame.display.update()
clock.tick(30)
continue
if time_over:
screen.fill((227, 30, 30))
end_text = end_font.render("SORRY TIME OVER!!", True, (0, 0, 0))
screen.blit(end_text, (50, screen_y // 2 - 100))
score_text = heading_font.render(
"Score is: " + str(score), True, (10, 40, 200)
)
screen.blit(score_text, (325, screen_y // 2 + 100))
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
running = False
pygame.display.update()
clock.tick(30)
continue
is_backspace = False
is_left = False
is_right = False
is_alphanum = False
invalid_move = False
screen.fill((198, 199, 199))
rem_time = total_time - pygame.time.get_ticks()
if rem_time <= 0:
time_over = True
omnious_sound.stop()
lose_sound = pygame.mixer.Sound('lose_sound.wav')
lose_sound.play()
elif rem_time < 10 * 1000:
omnious_sound.play()
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_BACKSPACE:
is_backspace = True
elif event.key == pygame.K_LEFT:
is_left = True
elif event.key == pygame.K_RIGHT:
is_right = True
else:
try:
ch = event.unicode
if ch in valid_characters:
is_alphanum = True
else:
invalid_move = True
except Exception:
invalid_move = True
ret_index = -1
if is_backspace:
ret_index = hash_object.add_backspace()
elif is_left:
hash_object.add_left_movement()
elif is_right:
hash_object.add_right_movement()
elif is_alphanum:
ret_index = hash_object.add_character(ch)
# Feed it with events every frame
if not invalid_move:
textinput.update(events)
# Blit its surface onto the screen
screen.blit(textinput.get_surface(), (text_input_x, text_input_y))
# Check if the current string matches any in the list
if ret_index != -1:
word_clear[ret_index] = True
textinput.clear_text()
score += 1
win = score == len(word_list)
if win: # this sound effect will only play once
win_sound = pygame.mixer.Sound('ta_da.wav')
win_sound.play()
draw_headers()
draw_words(word_list, word_clear)
pygame.display.update()
clock.tick(30)
if __name__ == "__main__":
main()