-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.py
136 lines (122 loc) · 5.7 KB
/
display.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
import pygame
import numpy as np
KEYBOARD = ['qwertyuiop', 'asdfghjkl', 'zxcvbnm']
# Given display settings, return (screen, board)
def start(display_settings):
# Width of the keyboard portion the screen and width of the whole screen,
# respectively
keyboard_width = max([len(x) for x in KEYBOARD]) / 2
width = (display_settings['board_dimensions'][1] + keyboard_width) * display_settings['square_size'] + display_settings['keyboard_gap']
# Initialize the display
pygame.init()
screen = pygame.display.set_mode(
(
width,
(display_settings['board_dimensions'][0] + 1.5) * display_settings['square_size']
)
)
return (screen, np.full(display_settings['board_dimensions'], ''))
# Update the display given the current game board
def display_board(game_board, screen, display_settings, word, empty_row):
assert len(word) == game_board.shape[1], "Incorrect word length for board."
# Draw the squares containing each letter
for row in range(display_settings['board_dimensions'][0]):
for col in range(display_settings['board_dimensions'][1]):
# Color this square appropriately based on Wordle rules
if game_board[row, col] == '':
color = display_settings['empty_color']
elif game_board[row, col] == word[col]:
color = display_settings['hit_color']
elif game_board[row, col] in word:
color = display_settings['misplace_color']
else:
color = display_settings['miss_color']
# Draw this square
draw_square(
screen,
(
col * display_settings['square_size'],
row * display_settings['square_size']
),
display_settings['square_size'], color, game_board[row, col],
display_settings['font_size'], display_settings['font_color']
)
# Draw horizontal lines to divide the squares visually
for row in range(1, display_settings['board_dimensions'][0]):
pygame.draw.line(
screen,
display_settings['border_color'],
(0, row * display_settings['square_size']),
(
display_settings['board_dimensions'][1] * display_settings['square_size'],
row * display_settings['square_size']
)
)
# Draw vertical lines to divide the squares visually
for col in range(1, display_settings['board_dimensions'][1]):
pygame.draw.line(
screen,
display_settings['border_color'],
(col * display_settings['square_size'], 0),
(
col * display_settings['square_size'],
display_settings['square_size'] * display_settings['board_dimensions'][0]
)
)
# Display the keyboard showing which letters have been used and which have
# been successful
for r, row in enumerate(KEYBOARD):
for c, letter in enumerate(row):
# Color the square for this letter appropriately
if empty_row == 0:
color = display_settings['empty_color']
elif any([(game_board[empty_row - 1, x] == letter) and (game_board[empty_row - 1, x] == word[x]) for x in range(game_board.shape[1])]):
color = display_settings['hit_color']
elif any([(game_board[empty_row - 1, x] == letter) and (game_board[empty_row - 1, x] in word) for x in range(game_board.shape[1])]):
color = display_settings['misplace_color']
elif letter in game_board:
color = display_settings['miss_color']
else:
color = display_settings['empty_color']
# Display the square for this keyboard letter
draw_square(
screen,
(
c * display_settings['square_size'] / 2 + display_settings['square_size'] * display_settings['board_dimensions'][1] + display_settings['keyboard_gap'],
r * display_settings['square_size'] / 2
),
display_settings['square_size'] / 2, color, letter,
display_settings['font_size'] / 2, display_settings['font_color']
)
pygame.display.update()
# Display a colored square containing a centered letter and return None.
#
# screen: the display, created with 'pygame.display.set_mode'
# start: a 2-tuple of integers giving the top left coordinates of the square
# square_size: side length of the square
# square_color: a 3-tuple of integers, each in [0, 255], giving RGB color for
# the square
# letter: a character to display in the square
# font_size: font size (integer) of the letter to pass to
# 'pygame.font.SysFont'
# font_color: a 3-tuple of integers, each in [0, 255], giving RGB color for
# the letter in the square
def draw_square(screen, start, square_size, square_color, letter, font_size, font_color):
# Draw this square
pygame.draw.rect(
screen,
square_color,
pygame.Rect(start[0], start[1], square_size, square_size)
)
# Add the letter in the center of the square
text = pygame.font.SysFont(None, int(font_size)).render(
letter.upper(), True, font_color
)
if text != '':
screen.blit(
text,
(
start[0] + 0.5 * square_size - font_size / 4,
start[1] + 0.5 * square_size - font_size / 4
)
)