-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2267199.py
More file actions
323 lines (260 loc) · 10.1 KB
/
2267199.py
File metadata and controls
323 lines (260 loc) · 10.1 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
"""Allows patches to be animated when moving"""
import time
from graphix import Window, Circle, Rectangle, Polygon, Point, Line
def main():
"""Main function"""
colour1, colour2, colour3, win, window_size = validation()
drawn_patches = []
patch_positions = []
drawn_patches, patch_positions = draw_patchwork(window_size, win, colour1, colour2, colour3)
while True:
challenge_feature(win, drawn_patches, patch_positions, colour1, colour2, colour3)
def undraw_patch(drawn_patches, index):
"""Undraws shapes"""
patch = drawn_patches[index]
if isinstance(patch, list):
for item in patch:
item.undraw()
else:
patch.undraw()
drawn_patches.pop(index)
drawn_patches.insert(index,[])
return drawn_patches
def check_patch_status(drawn_patches, index):
"""Checks if patch is empty or not"""
patch = drawn_patches[index]
if patch:
return False
else:
return True
def move_patch(win, tl, drawn_patches, patch_positions, index, direction):
"""Moves a patch in the direction inputted using the arrow keys"""
movey = 0
movex = 0
match direction:
case "Up":
movey = -100
case "Left":
movex = -100
case "Down":
movey = 100
case "Right":
movex = 100
if(tl.y + movey < 0) \
or (tl.x + movex < 0) \
or (tl.y + movey == win.width) \
or (tl.x + movex == win.width):
return index, None, drawn_patches, None, False
else:
adjacent_point = Point(tl.x + movex, tl.y + movey)
tl, adjacent_index = choose_patch(patch_positions, adjacent_point)
adjacent_status = check_patch_status(drawn_patches, adjacent_index)
if adjacent_status:
patch = drawn_patches[index]
if isinstance(patch, list):
for item in patch:
time.sleep(0.001)
item.move(movex, movey)
else:
patch.move(movex, movey)
return index, adjacent_index, drawn_patches, patch, adjacent_status
else:
adjacent_status = False
return index, adjacent_index, drawn_patches, None, adjacent_status
def match_key(win, square, drawn_patches, patch_positions, tl, index, colour1, colour2, colour3):
"""Completes a function when a certain key is pressed"""
empty_patch = check_patch_status(drawn_patches, index)
chosen_key = win.get_key()
br = Point(tl.x + 100, tl.y + 100)
if empty_patch:
match chosen_key:
case '1':
drawn_patches[index] = draw_penultimate_patch(win, tl.x, tl.y, colour1)
case '2':
drawn_patches[index] = draw_penultimate_patch(win, tl.x, tl.y, colour2)
case '3':
drawn_patches[index] = draw_penultimate_patch(win, tl.x, tl.y, colour3)
case '4':
drawn_patches[index] = draw_final_patch(win, tl.x, tl.y, colour1)
case '5':
drawn_patches[index] = draw_final_patch(win, tl.x, tl.y, colour2)
case '6':
drawn_patches[index] = draw_final_patch(win, tl.x, tl.y, colour3)
case '7':
drawn_patches[index] = draw_square(win, tl, br, colour1)
case '8':
drawn_patches[index] = draw_square(win, tl, br, colour2)
case '9':
drawn_patches[index] = draw_square(win, tl, br, colour3)
case "Escape":
square.undraw()
else:
keys = ["Up", "Left", "Down", "Right"]
if chosen_key in keys:
index, adjacent_index, drawn_patches, patch, status = move_patch(win, tl, drawn_patches, patch_positions, index, chosen_key)
if status:
drawn_patches.pop(index)
drawn_patches.insert(index,[])
drawn_patches[adjacent_index] = patch
elif chosen_key == 'x':
drawn_patches = undraw_patch(drawn_patches, index)
elif chosen_key == "Escape":
square.undraw()
return chosen_key, drawn_patches
def challenge_feature(win, drawn_patches, patch_positions, colour1, colour2, colour3):
"""Completes all the challenge tasks"""
patch_point = win.get_mouse()
tl, index = choose_patch(patch_positions, patch_point)
br = Point(tl.x + 100, tl.y + 100)
key_escape = ""
while key_escape != "Escape":
square = draw_selected_patch(win, tl, br)
key_escape, drawn_patches = match_key(win, square, drawn_patches, patch_positions, tl, index, colour1, colour2, colour3)
square.undraw()
def choose_patch(patch_positions, point):
"""Chooses a patch"""
x = int((point.x // 100)*100)
y = int((point.y // 100)*100)
tl = Point(x, y)
chosen_patch_position = str(tl)
for item in patch_positions:
positions = str(item)
if positions == chosen_patch_position:
index = patch_positions.index(item)
return tl, index
def validation():
"""Validates colours and window sizes"""
picked_colours = ""
window_size = check_window_size()
window_size *= 100
for _ in range (3):
colours = check_previous_colours(picked_colours)
picked_colours = f"{picked_colours}{colours} "
picked_colours = picked_colours.split(' ')
colour1 = picked_colours[0]
colour2 = picked_colours[1]
colour3 = picked_colours[2]
win = Window("2267199", window_size, window_size)
return colour1, colour2, colour3, win, window_size
def check_window_size():
"""Checks inputted window sizes are 5, 7, or 9"""
while True:
size = str(input("Please enter the patchwork size from 5, 7 or 9: "))
if size == "5" or size == "7" or size == "9":
return int(size)
elif size.lower() == "five":
return 5
elif size.lower() == "seven":
return 7
elif size.lower() == "nine":
return 9
else:
print("Invalid input, please try again")
def check_colour():
"""Checks if a colour entered is allowed"""
valid_colours = ["red", "green", "blue", "magenta", "orange", "purple"]
while True:
chosen_colour = str(input("Please enter a colour: "))
colour = chosen_colour.lower()
for item in valid_colours:
if colour == item:
return colour
def check_previous_colours(picked_colours):
"""Checks if a colour has been previously inputted"""
check = True
while check:
colours = check_colour()
if colours not in picked_colours:
return colours
else:
print("colour already inputted")
def draw_final_patch(win, x, y, colour):
"""Draws the final patch"""
shapes_drawn = []
for i in range(0, 100, 10):
top_start = Point(x + i, y)
right_end = Point(x + 100, y + i + 10)
shapes_drawn.append(draw_line(win, top_start, right_end, colour))
left_start = Point(x, y + i)
bottom_end = Point(x + i + 10, y + 100)
shapes_drawn.append(draw_line(win, left_start, bottom_end, colour))
return shapes_drawn
def draw_penultimate_patch(win, in_x, in_y, colour):
"""Draws the penultimate patch"""
shapes_drawn = []
alt_square = True
alt_circle = True
radius = 10
for x in range(0, 100, 20):
for y in range(0, 100, 20):
if alt_square:
tl = Point(in_x + x, in_y + y)
br = Point(in_x + x + 20, in_y + y + 20)
centre = Point(tl.x + 10, tl.y + 10)
shapes_drawn.append(draw_square(win, tl, br, colour))
else:
centre = Point(in_x + x + radius, in_y + y + radius)
shapes_drawn.append(draw_circle(win, centre, radius, colour))
if alt_circle:
triangle_list = [centre, Point(centre.x - 10, centre.y - 10), Point(centre.x - 10, centre.y + 10)]
else:
triangle_list = [centre, Point(centre.x + 10, centre.y - 10), Point(centre.x + 10, centre.y + 10)]
shapes_drawn.append(draw_triangle(win, triangle_list))
alt_square = not alt_square
alt_circle = not alt_circle
return shapes_drawn
def draw_patchwork(window_size, win, colour1, colour2, colour3):
"""Draws all the patches in the specified pattern"""
tile = 100
patches_drawn = []
patch_positions = []
for y in range(0, window_size, 100):
for x in range(0, window_size, 100):
tl = Point(x, y)
br = Point(x + 100, y + 100)
patch_positions.append(tl)
if x == y:
patches_drawn.append(draw_final_patch(win, x, y, colour1))
elif y == 0 or x == window_size - tile:
patches_drawn.append(draw_final_patch(win, x, y, colour2))
elif x >= 100 and x > y:
patches_drawn.append(draw_square(win, tl, br, colour2))
elif x == 0 or y == window_size - tile:
patches_drawn.append(draw_square(win, tl, br, colour3))
else:
patches_drawn.append(draw_penultimate_patch(win, x, y, colour3))
return patches_drawn, patch_positions
def draw_line(win, start_point, end_point, colour):
"""Draws lines for the final patch"""
line = Line(start_point, end_point)
line.fill_colour = colour
line.draw(win)
return line
def draw_circle(win, centre, radius, colour):
"""Draws a circle"""
circ = Circle(centre, radius)
circ.outline_colour = colour
circ.fill_colour = colour
circ.draw(win)
return circ
def draw_square(win, x, y, colour):
"""Draws a square"""
square = Rectangle(x, y)
square.fill_colour = colour
square.outline_colour = colour
square.draw(win)
return square
def draw_triangle(win, triangle_list):
"""Draws a triangle"""
triangle = Polygon(triangle_list)
triangle.fill_colour = "white"
triangle.outline_colour = "white"
triangle.draw(win)
return triangle
def draw_selected_patch(win, tl, br):
"""Draws an outline around the selected patch"""
patch = Rectangle(tl, br)
patch.outline_width = 2
patch.draw(win)
return patch
main()