-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwordle.py
192 lines (159 loc) · 4.52 KB
/
wordle.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
# import the pygame module
from ast import Or
import time
import pygame
import random
pygame.init()
screen = pygame.display.set_mode((300, 300))
screen_x, screen_y = screen.get_size()
pygame.display.set_caption('Worlde')
words_list = []
for i in open("words.txt", "r").read().split('\n'):
if len(i) == 5:
words_list.append(i)
words = []
temp_word = []
rectangles = [[],[],[],[],[],[]]
possible_letters = []
for i in range (97,123):
possible_letters.append(chr(i))
current_row = 0
guesses = 0
end_word = words_list[random.randint(0,len(words_list))].strip()
# print(end_word)
FONT = pygame.font.SysFont("comicsans", 16)
canPress = True
isWon = False
background_colour = (255,255,255)
rect_color = (0,0,0)
green = (0,255,0)
yellow = (255,255,0)
white = (255,255,255)
black = (0,0,0)
gray = (211,211,211)
class Rectangle:
def __init__(self,x ,y,color):
self.x = x
self.y = y
self.color = color
self.rect = pygame.Rect(x, y, screen_x/5, screen_x/6)
def draw(self):
pygame.draw.rect(screen,self.color,self.rect)
def __setitem__(self,color):
self.color = color
'''
text = FONT.render(end_word, True, (0,0,255), (255,0,255))
text_box = text.get_rect()
text_box.center = ()
'''
def init_Rectangles():
x_step = screen_x / 5
y_step = screen_y / 6
for r in range(6):
for c in range(5):
rectangles[r].append(Rectangle(c * x_step, r * y_step, white))
#Rectangle(c * x_step, r * y_step, green)
def text(word, row):
step_x = screen_x / 5
multiplier = .5
for char in word:
text = FONT.render(char, True, (0,0,0))
text_box = text.get_rect()
text_box.center = (step_x * multiplier, (row + 0.5) * screen_y / 6)
multiplier += 1
screen.blit(text, text_box)
def draw_Lines():
x_diff = screen_x / 5
for i in range(0, screen_x, int(screen_x / 5)):
pygame.draw.line(screen, (0,0,0), (i, 0), (i, screen_y))
for i in range(0,screen_y, int(screen_y / 6)):
pygame.draw.line(screen,(0,0,0), (0, i), (screen_x, i))
def draw_Rects():
for r in range(6):
for c in range(5):
rectangles[r][c].draw()
def checkWordExists(word):
if str.lower(word) not in words_list:
return False
return True
def updateColors(row):
word = str.lower(words[-1])
letters = list(end_word) #find way to split word into array so I can make sure a letter isnt marked twice
# print(letters)
# print(canPress)
# print(word)
# print(end_word)
for i in range(5):
#rectangles[row][i].color = green
if word[i] in end_word and word[i] in letters:
if word[i] == end_word[i]:
rectangles[row][i].color = green
else:
rectangles[row][i].color = yellow
letters.remove(word[i])
else:
rectangles[row][i].color = gray
if word[i] in possible_letters:
possible_letters.remove(word[i])
def changeColor(color):
for r in range(6):
for c in range(5):
rectangles[r][c].color = color
def checkWin(word):
global isWon
isWon = str.lower(word.strip()) == str.lower(end_word.strip())
# Define the background colour
# using RGB color coding.
# Fill the background colour to the screen
#screen.fill(background_colour)
pygame.display.flip()
init_Rectangles()
# game loop
def reset():
global words, temp_word, rectangles, current_row, guesses, canPress, running, end_word, isWon
isWon = False
words = []
temp_word = []
rectangles = [[],[],[],[],[],[]]
current_row = 0
guesses = 0
end_word = words_list[random.randint(0,len(words_list))].strip()
canPress = True
running = True
init_Rectangles()
screen.fill(white)
pygame.display.set_caption('Worlde')
pygame.display.update()
running = True
while running:
draw_Rects()
row = 0
for word in words:
text(word, row)
row += 1
text(temp_word, current_row)
draw_Lines()
# for loop through the event queue
for event in pygame.event.get():
# Check for QUIT event
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_BACKSPACE and len(temp_word) > 0 and canPress:
temp_word.pop(-1)
elif event.key == pygame.K_RETURN and len(temp_word) == 5 and checkWordExists(''.join(temp_word)):
words.append(''.join(temp_word))
temp_word = []
guesses += 1
current_row += 1
updateColors(current_row - 1)
canPress = not checkWin(words[-1])
print(str.lower(" ".join(possible_letters)))
elif event.key < 123 and event.key >= 97 and len(temp_word) < 5 and canPress:
temp_word.append(str.upper(chr(event.key)))
elif event.key == pygame.K_RSHIFT:
reset()
if isWon or guesses > 5:
pygame.display.set_caption("The word was: " + end_word)
pygame.display.update()
print("The word was:" + end_word)