-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordleGraphics.py
More file actions
301 lines (249 loc) · 9.26 KB
/
WordleGraphics.py
File metadata and controls
301 lines (249 loc) · 9.26 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File: WordleGraphics.py
"""
This module implements the WordleGWindow class, which manages
the graphical display for the Wordle project.
"""
# Implementation notes
# --------------------
# This WordleGWindow class is implemented as a subclass of
# the GWindow class in the Portable Graphics Library.
from pgl import GWindow, GLabel, GRect, GCompound
N_ROWS = 6 # Number of rows
N_COLS = 5 # Number of columns
GWINDOW_WIDTH = 500 # Width of the graphics window
GWINDOW_HEIGHT = 700 # Height of the graphics window
SQUARE_SIZE = 60 # Size of each square
SQUARE_SEP = 5 # Separation between squares
TOP_MARGIN = 30 # Top margin
BOTTOM_MARGIN = 30 # Bottom margin
MESSAGE_SEP = 20 # Space between board and message center
SQUARE_FONT = "bold 44px 'Helvetica Neue',sans-serif"
MESSAGE_FONT = "bold 20px 'Helvetica Neue',sans-serif"
KEY_FONT = "18px 'Helvetica Neue',sans-serif"
ENTER_FONT = "14px 'Helvetica Neue',sans-serif"
CORRECT_COLOR = "#66BB66" # A shade of green
PRESENT_COLOR = "#CCBB66" # A shade of brownish yellow
MISSING_COLOR = "#999999" # A shade of gray
UNKNOWN_COLOR = "#FFFFFF" # White
KEY_COLOR = "#DDDDDD"
KEY_WIDTH = 40
KEY_HEIGHT = 60
KEY_CORNER = 9
KEY_XSEP = 5
KEY_YSEP = 7
ASCENT_FRACTION = 0.75 # Used to improve vertical alignment
KEY_LABELS = [
[ "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P" ],
[ "A", "S", "D", "F", "G", "H", "J", "K", "L" ],
[ "ENTER", "Z", "X", "C", "V", "B", "N", "M", "DELETE" ]
]
# Derived constants
SQUARE_DELTA = SQUARE_SIZE + SQUARE_SEP
BOARD_WIDTH = N_COLS * SQUARE_SIZE + (N_COLS - 1) * SQUARE_SEP
BOARD_HEIGHT = N_ROWS * SQUARE_SIZE + (N_ROWS - 1) * SQUARE_SEP
MESSAGE_X = GWINDOW_WIDTH / 2
MESSAGE_Y = TOP_MARGIN + BOARD_HEIGHT + MESSAGE_SEP
class WordleGWindow(GWindow):
"""This class creates the Wordle window."""
def __init__(self):
"""Creates the Wordle window."""
def create_grid():
grid = [ ]
for row in range(N_ROWS):
line = [ ]
for col in range(N_COLS):
sq = WordleSquare(self, row, col)
self.add(sq)
line.append(sq)
grid.append(line)
return grid
def create_keyboard():
keys = { }
nk = len(KEY_LABELS[0])
h = KEY_HEIGHT
y0 = GWINDOW_HEIGHT - BOTTOM_MARGIN - 3 * KEY_HEIGHT - 2 * KEY_YSEP
for row in range(len(KEY_LABELS)):
y = y0 + row * (KEY_HEIGHT + KEY_YSEP)
x = (GWINDOW_WIDTH - nk * KEY_WIDTH - (nk - 1) * KEY_XSEP) / 2
if row == 1:
x += (KEY_WIDTH + KEY_XSEP) / 2
for col in range(len(KEY_LABELS[row])):
label = KEY_LABELS[row][col]
w = KEY_WIDTH
if len(label) > 1:
w += (KEY_WIDTH + KEY_XSEP) / 2
key = WordleKey(w, h, label)
self.add(key, x, y)
keys[label] = key
x += w + KEY_XSEP
return keys
def create_message():
msg = WordleMessage()
self.add(msg, GWINDOW_WIDTH / 2, MESSAGE_Y)
return msg
def key_action(e):
if isinstance(e, str):
letter = e.upper()
else:
letter = e.get_key().upper()
if letter == "<BACKSPACE>" or letter == "<DELETE>":
self.show_message("")
if self._row < N_ROWS and self._col > 0:
self._col -= 1
sq = self._grid[self._row][self._col]
sq.set_square_label(" ")
elif letter == "<RETURN>" or letter == "<ENTER>":
self.show_message("")
for fn in self._listeners:
fn()
elif letter.isalpha():
self.show_message("")
if self._row < N_ROWS and self._col < N_COLS:
sq = self._grid[self._row][self._col]
sq.set_square_label(letter)
self._col += 1
def click_action(e):
key = find_key(e.getX(), e.getY())
if key:
key_action(key.get_key())
def find_key(x, y):
for key in self._keys.values():
if key.get_frame().contains(x, y):
return key
return None
def delete_window():
"""Closes the window and exits from the event loop."""
root.destroy()
GWindow.__init__(self, GWINDOW_WIDTH, GWINDOW_HEIGHT)
self._grid = create_grid()
self._message = create_message()
self._keys = create_keyboard()
self._listeners = [ ]
self.add_event_listener("key", key_action)
self.add_event_listener("click", click_action)
self._row = 0
self._col = 0
def get_square_label(self, row, col):
return self._grid[row][col].get_square_label()
def set_square_label(self, row, col, letter):
self._grid[row][col].set_square_label(letter)
def get_square_state(self, row, col):
return self._grid[row][col].get_state()
def set_square_state(self, row, col, state):
self._grid[row][col].set_state(state)
def get_key_state(self, letter):
return self._keys[letter].get_state()
def set_key_state(self, letter, state):
self._keys[letter].set_state(state)
def get_current_row(self):
return self._row
def set_current_row(self, row):
self._row = row
self._col = 0
for col in range(N_COLS):
self.set_square_label(row, col, "")
self.set_square_state(row, col, "UNKNOWN")
def add_enter_listener(self, fn):
self._listeners.append(fn)
def show_message(self, msg, color="Black"):
self._message.set_text(msg, color)
class WordleSquare(GCompound):
def __init__(self, gw, row, col):
GCompound.__init__(self)
x = (GWINDOW_WIDTH - BOARD_WIDTH) / 2 + col * SQUARE_DELTA
y = TOP_MARGIN + row * SQUARE_DELTA
self._letter = " "
self._state = "UNKNOWN"
self._frame = GRect(SQUARE_SIZE, SQUARE_SIZE)
self._frame.set_filled(True)
self._frame.set_fill_color("White")
self._label = GLabel("")
self._label.set_font(SQUARE_FONT)
self.add(self._frame)
self.add(self._label)
self.set_location(x, y)
def get_square_label(self):
return self._letter
def set_square_label(self, letter):
self._letter = letter
self._label.set_label(letter)
x = (SQUARE_SIZE - self._label.get_width()) / 2
y = (SQUARE_SIZE + ASCENT_FRACTION * self._label.get_ascent()) / 2
self._label.set_location(x, y)
def get_state(self):
return self._state
def set_state(self, state):
state = state.upper()
self._state = state
fg = "White"
if state == "UNKNOWN":
fg = "Black"
bg = "White"
elif state == "CORRECT":
bg = CORRECT_COLOR
elif state == "PRESENT":
bg = PRESENT_COLOR
elif state == "MISSING":
bg = MISSING_COLOR
else:
raise ValueError("Illegal letter state " + str(state))
self._frame.set_fill_color(bg)
self._label.set_color(fg)
class WordleKey(GCompound):
def __init__(self, width, height, key):
GCompound.__init__(self)
if len(key) == 1:
self._key = key
else:
self._key = "<" + key + ">"
self._state = "UNKNOWN"
font = KEY_FONT
if key == "ENTER":
font = ENTER_FONT
if key == "DELETE":
key = "\u232B"
self._frame = GRect(width, height)
self._frame.set_filled(True)
self._frame.set_fill_color("White")
self._label = GLabel(key)
self._label.set_font(font)
x = (width - self._label.get_width()) / 2
y = (height + ASCENT_FRACTION * self._label.get_ascent()) / 2
self.add(self._frame)
self.add(self._label, x, y)
def get_key(self):
return self._key
def get_frame(self):
return self._frame
def get_state(self):
return self._state
def set_state(self, state):
self._state = state
fg = "White"
if state == "UNKNOWN":
fg = "Black"
bg = "White"
elif state == "CORRECT":
bg = CORRECT_COLOR
elif state == "PRESENT":
bg = PRESENT_COLOR
elif state == "MISSING":
bg = MISSING_COLOR
else:
raise ValueError("Illegal letter state " + str(state))
self._frame.set_fill_color(bg)
self._label.set_color(fg)
class WordleMessage(GCompound):
def __init__(self):
GCompound.__init__(self)
self._msg = GLabel("")
self._msg.set_font(MESSAGE_FONT)
self.add(self._msg)
def get_text(self):
return self._text
def set_text(self, text, color="Black"):
self._text = text
self._msg.setLabel(text)
self._msg.setColor(color)
self._msg.setLocation(-self._msg.get_width() / 2,
ASCENT_FRACTION * self._msg.get_ascent() / 2)