-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
248 lines (210 loc) · 8.66 KB
/
main.py
File metadata and controls
248 lines (210 loc) · 8.66 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
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
import random
import pygame
from settings import *
from sprites import *
class Game:
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(title)
self.clock = pygame.time.Clock()
self.create_word_list()
self.letters_text = UIElement(100, 70, "Not Enough Letters", WHITE)
def create_word_list(self):
with open("words.txt", "r") as file:
self.words_list = file.read().splitlines()
def new(self):
self.word = random.choice(self.words_list).upper()
print(self.word)
self.text = ""
self.current_row = 0
self.tiles = []
self.create_tiles()
self.flip = True
self.not_enough_letters = False
self.timer = 0
def create_tiles(self):
for row in range(6):
self.tiles.append([])
for col in range(5):
self.tiles[row].append(Tile((col * (TILESIZE + GAPSIZE)) + MARGIN_X, (row * (TILESIZE + GAPSIZE)) + MARGIN_Y))
def run(self):
self.playing = True
while self.playing:
self.clock.tick(FPS)
self.events()
self.update()
self.draw()
def update(self):
self.add_letter()
def add_letter(self):
# empty all the letter in the current row
for tile in self.tiles[self.current_row]:
tile.letter = ""
# add the letters typed to the current row
for i, letter in enumerate(self.text):
self.tiles[self.current_row][i].letter = letter
self.tiles[self.current_row][i].create_font()
def draw_tiles(self):
for row in self.tiles:
for tile in row:
tile.draw(self.screen)
def draw(self):
self.screen.fill(BGCOLOUR)
# display the not enough letters text
if self.not_enough_letters:
self.timer += 1
self.letters_text.fade_in()
if self.timer > 90:
self.not_enough_letters = False
self.timer = 0
else:
self.letters_text.fade_out()
self.letters_text.draw(self.screen)
self.draw_tiles()
pygame.display.flip()
def row_animation(self):
# row shaking if not enough letters is inputted
self.not_enough_letters = True
start_pos = self.tiles[0][0].x
amount_move = 4
move = 3
screen_copy = self.screen.copy()
screen_copy.fill(BGCOLOUR)
for row in self.tiles:
for tile in row:
if row != self.tiles[self.current_row]:
tile.draw(screen_copy)
while True:
while self.tiles[self.current_row][0].x < start_pos + amount_move:
self.screen.blit(screen_copy, (0, 0))
for tile in self.tiles[self.current_row]:
tile.x += move
tile.draw(self.screen)
self.clock.tick(FPS)
pygame.display.flip()
while self.tiles[self.current_row][0].x > start_pos - amount_move:
self.screen.blit(screen_copy, (0, 0))
for tile in self.tiles[self.current_row]:
tile.x -= move
tile.draw(self.screen)
self.clock.tick(FPS)
pygame.display.flip()
amount_move -= 2
if amount_move < 0:
break
def box_animation(self):
# tile scale animation for every letter inserted
for tile in self.tiles[self.current_row]:
if tile.letter == "":
screen_copy = self.screen.copy()
for start, end, step in ((0, 6, 1), (0, -6, -1)):
for size in range(start, end, 2*step):
self.screen.blit(screen_copy, (0, 0))
tile.x -= size
tile.y -= size
tile.width += size * 2
tile.height += size * 2
surface = pygame.Surface((tile.width, tile.height))
surface.fill(BGCOLOUR)
self.screen.blit(surface, (tile.x, tile.y))
tile.draw(self.screen)
pygame.display.flip()
self.clock.tick(FPS)
self.add_letter()
break
def reveal_animation(self, tile, colour):
# reveal colours animation when user input the whole word
screen_copy = self.screen.copy()
while True:
surface = pygame.Surface((tile.width + 5, tile.height + 5))
surface.fill(BGCOLOUR)
screen_copy.blit(surface, (tile.x, tile.y))
self.screen.blit(screen_copy, (0, 0))
if self.flip:
tile.y += 6
tile.height -= 12
tile.font_y += 4
tile.font_height = max(tile.font_height - 8, 0)
else:
tile.colour = colour
tile.y -= 6
tile.height += 12
tile.font_y -= 4
tile.font_height = min(tile.font_height + 8, tile.font_size)
if tile.font_height == 0:
self.flip = False
tile.draw(self.screen)
pygame.display.update()
self.clock.tick(FPS)
if tile.font_height == tile.font_size:
self.flip = True
break
def check_letters(self):
# algorithm to check if the letters inputted correspond to any of the letters in the actual word
copy_word = [x for x in self.word]
for i, user_letter in enumerate(self.text):
colour = LIGHTGREY
for j, letter in enumerate(copy_word):
if user_letter == letter:
colour = YELLOW
if i == j:
colour = GREEN
copy_word[j] = ""
break
# reveal animation
self.reveal_animation(self.tiles[self.current_row][i], colour)
def events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit(0)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
if len(self.text) == 5:
# check all letters
self.check_letters()
# if the text is correct or the player has used all his turns
if self.text == self.word or self.current_row + 1 == 6:
# player lose, lose message is sent
if self.text != self.word:
self.end_screen_text = UIElement(100, 700, f"THE WORD WAS: {self.word}", WHITE)
# player win, send win message
else:
self.end_screen_text = UIElement(100, 700, "YOU GUESSED RIGHT", WHITE)
# restart the game
self.playing = False
self.end_screen()
break
self.current_row += 1
self.text = ""
else:
# row animation, not enough letters message
self.row_animation()
elif event.key == pygame.K_BACKSPACE:
self.text = self.text[:-1]
else:
if len(self.text) < 5 and event.unicode.isalpha():
self.text += event.unicode.upper()
self.box_animation()
def end_screen(self):
play_again = UIElement(85, 750, "PRESS ENTER TO PLAY AGAIN", WHITE, 30)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit(0)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
return
self.screen.fill(BGCOLOUR)
self.draw_tiles()
self.end_screen_text.fade_in()
self.end_screen_text.draw(self.screen)
play_again.fade_in()
play_again.draw(self.screen)
pygame.display.flip()
game = Game()
while True:
game.new()
game.run()