-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
277 lines (237 loc) · 10.4 KB
/
game.py
File metadata and controls
277 lines (237 loc) · 10.4 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
"""Module providing a simple word game called Wordle."""
import sys
import random
import csv
import pygame
#from words import *
pygame.init()
# load words from the CSV file
def load_words_from_csv(filepath):
words = []
with open(filepath, newline='') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
words.append(row[0])
return words
words = load_words_from_csv('assets/FreqWords.csv')
# Constants
WIDTH, HEIGHT = 633, 900
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
BACKGROUND = pygame.image.load("assets\Starting Tiles.png")
BACKGROUND_RECT = BACKGROUND.get_rect(center=(317, 300))
ICON = pygame.image.load("assets\Icon.png")
pygame.display.set_caption("Wordle")
pygame.display.set_icon(ICON)
GREEN = "#6aaa64"
YELLOW = "#c9b458"
GRAY = "#787c7e"
OUTLINE = "#d3d6da"
FILLED_OUTLINE = "#878a8c"
CORRECT_WORD = random.choice(words).lower()
ALPHABET = ["QWERTYUIOP", "ASDFGHJKL", "ZXCVBNM"]
GUESSED_LETTER_FONT = pygame.font.Font("assets/FreeSansBold.otf", 50)
AVAILABLE_LETTER_FONT = pygame.font.Font("assets/FreeSansBold.otf", 25)
SCREEN.fill("white")
SCREEN.blit(BACKGROUND, BACKGROUND_RECT)
pygame.display.update()
LETTER_X_SPACING = 85
LETTER_Y_SPACING = 12
LETTER_SIZE = 75
GUESSES_COUNT = 0
# guess is a 2D list that will store user's guesses. A guess will contain list of letters.
# the list will be iterated over and each letter in each guess
guesses = [[]] * 6
current_guess = []
current_guess_string = ""
current_letter_bg_x = 109
# a list storing all the letter object with a button with all the letters you can click
letters = []
GAME_RESULT = ""
message_display_time = 0 # time whe the message was displayed
MESSAGE_DISPLAY_DURATION = 2500 # duration of the message display
class Letter:
def __init__(self, text, bg_position):
# initializes the letter object
self.text = text
self.bg_position = bg_position
self.bg_color = "white"
self.text_color = "black"
self.bg_x = bg_position[0]
self.bg_y = bg_position[1]
self.bg_rect = (bg_position[0], self.bg_y, LETTER_SIZE, LETTER_SIZE)
self.position = (self.bg_x + 36, self.bg_position[1] + 34)
self.text_surface = GUESSED_LETTER_FONT.render(self.text, True, self.text_color)
self.text_rect = self.text_surface.get_rect(center=self.position)
def draw(self):
# puts the letter and text on the correct position on the screen
pygame.draw.rect(SCREEN, self.bg_color, self.bg_rect)
if self.bg_color == "white":
pygame.draw.rect(SCREEN, FILLED_OUTLINE, self.bg_rect, 3)
self.text_surface = GUESSED_LETTER_FONT.render(self.text, True,self.text_color)
SCREEN.blit(self.text_surface, self.text_rect)
pygame.display.update()
def delete(self):
# fills the letter's spot with the default square, and resets the text and text color
pygame.draw.rect(SCREEN, "white", self.bg_rect)
pygame.draw.rect(SCREEN, OUTLINE, self.bg_rect, 3)
pygame.display.update()
class Keyboard:
def __init__(self, x, y, letter):
# initializes the indicator object
self.x = x
self.y = y
self.text = letter
self.rect = (self.x, self.y, 57, 75)
self.bg_color = OUTLINE
def draw(self):
# puts the indicator and its text on the correct position on the screen
pygame.draw.rect(SCREEN, self.bg_color, self.rect)
self.text_surface = AVAILABLE_LETTER_FONT.render(self.text, True, "white")
self.text_rect = self.text_surface.get_rect(center=(self.x + 27, self.y + 30))
SCREEN.blit(self.text_surface, self.text_rect)
pygame.display.update()
# drawing the keyboards on the screen
keyboard_x, keyboard_y = 20, 600
for i in range(3):
for letter in ALPHABET[i]:
keyboard = Keyboard(keyboard_x, keyboard_y, letter)
letters.append(keyboard)
keyboard.draw()
keyboard_x += 60
keyboard_y += 100 # moves the keyboard row down
if i == 0:
keyboard_x = 50
elif i == 1:
keyboard_x = 105
def check_guess(guess_to_check):
# checks the guess and updates uers' guess with the correct colors
global current_guess, current_guess_string, GUESSES_COUNT, current_letter_bg_x, GAME_RESULT
game_decided = False
for i in range(5):
lowercase_letter = guess_to_check[i].text.lower()
if lowercase_letter in CORRECT_WORD: # if the letter is in the correct word
if lowercase_letter == CORRECT_WORD[i]: # if the letter is in the correct position
guess_to_check[i].bg_color = GREEN
for indicator in letters:
if indicator.text == lowercase_letter.upper():
indicator.bg_color = GREEN
indicator.draw()
guess_to_check[i].text_color = "white" # if the background is green, than the text should be white
if not game_decided:
GAME_RESULT = "W"
else: # letter is in the word but not in the correct position
guess_to_check[i].bg_color = YELLOW
for indicator in letters:
if indicator.text == lowercase_letter.upper():
indicator.bg_color = YELLOW
indicator.draw()
guess_to_check[i].text_color = "white"
GAME_RESULT = ""
game_decided = True
else: # letter is not in the word
guess_to_check[i].bg_color = GRAY
for indicator in letters:
if indicator.text == lowercase_letter.upper():
indicator.bg_color = GRAY
indicator.draw()
guess_to_check[i].text_color = "white"
GAME_RESULT = ""
game_decided = True
# draw the letter on the screen
guess_to_check[i].draw()
pygame.display.update()
GUESSES_COUNT += 1
current_guess = []
current_guess_string = ""
current_letter_bg_x = 109
if GUESSES_COUNT == 6 and GAME_RESULT == "":
GAME_RESULT = "L"
def play_again():
# puts the play again text on the screen
pygame.draw.rect(SCREEN, "white", (10, 600, 1000, 600))
play_again_font = pygame.font.Font("assets/FreeSansBold.otf", 40)
play_again_text = play_again_font.render("Press ENTER to play again!", True, "black")
play_again_rect = play_again_text.get_rect(center = (WIDTH/2, 700))
word_was_text = play_again_font.render(f"The word was {CORRECT_WORD.upper()}", True, "black")
word_was_rect = word_was_text.get_rect(center = (WIDTH/2, 650))
SCREEN.blit(word_was_text, word_was_rect)
SCREEN.blit(play_again_text, play_again_rect)
pygame.display.update()
def reset():
# resets all variables to their default states
global GUESSES_COUNT, CORRECT_WORD, guesses, current_guess, current_guess_string, GAME_RESULT
SCREEN.fill("white")
SCREEN.blit(BACKGROUND, BACKGROUND_RECT)
GUESSES_COUNT = 0
CORRECT_WORD = random.choice(words).lower()
guesses = [[]] * 6
current_guess = []
current_guess_string = ""
GAME_RESULT = ""
pygame.display.update()
for indicator in letters:
indicator.bg_color = OUTLINE
indicator.draw()
def create_new_letter(key_pressed):
# creates a new letter and adds it to the guess
global current_guess_string, current_letter_bg_x
current_guess_string += key_pressed
new_letter = Letter(key_pressed, (current_letter_bg_x, GUESSES_COUNT * 100 + LETTER_Y_SPACING))
current_letter_bg_x += LETTER_X_SPACING
guesses[GUESSES_COUNT].append(new_letter)
current_guess.append(new_letter)
for guess in guesses:
for letter in guess:
letter.draw()
def delete_letter():
# deletes a letter from the screen
global current_guess_string, current_letter_bg_x
guesses[GUESSES_COUNT][-1].delete()
guesses[GUESSES_COUNT].pop() # removes letter from the guess list
current_guess_string = current_guess_string[:-1]
current_guess.pop()
current_letter_bg_x -= LETTER_X_SPACING
def redraw_keyboard():
# redraws the keyboard
for letter in letters:
letter.draw()
while True:
if GAME_RESULT != "":
play_again()
if message_display_time > 0:
current_time = pygame.time.get_ticks()
if current_time - message_display_time >= MESSAGE_DISPLAY_DURATION:
# clear the message
pygame.draw.rect(SCREEN, "white", (10, 600, 1000, 600))
# redraw the keyboard
redraw_keyboard()
pygame.display.update()
message_display_time = 0 # reset the message display time
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
if GAME_RESULT != "":
reset()
else:
if len(current_guess_string) == 5 and current_guess_string.lower() in words: # checking for valid guess
check_guess(current_guess)
else:
# display the message and set the display time
pygame.draw.rect(SCREEN, "white", (10, 600, 1000, 600))
wrong_word_font = pygame.font.Font("assets/FreeSansBold.otf", 40)
wrong_word_text = wrong_word_font.render(f"{current_guess_string} is not in the word list.", True, "black")
wrong_word_rect = wrong_word_text.get_rect(center = (WIDTH / 2, 700))
SCREEN.blit(wrong_word_text, wrong_word_rect)
pygame.display.update()
message_display_time = pygame.time.get_ticks()
elif event.key == pygame.K_BACKSPACE:
if len(current_guess_string) > 0:
delete_letter()
else:
key_pressed = event.unicode.upper()
if key_pressed in "QWERTYUIOPASDFGHJKLZXCVBNM" and key_pressed != "":
if len(current_guess_string) < 5:
create_new_letter(key_pressed)