-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrawing.py
More file actions
484 lines (383 loc) · 12.2 KB
/
drawing.py
File metadata and controls
484 lines (383 loc) · 12.2 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# Classwork - Turtle Drawing
import turtle
import math
import random
from time import sleep
from random import randrange
# A forgiving implementation of a Turtle(), which allows turning left/right by 90°, deg when calling
# left() or right() with no arguments.
class ForgivingTurtle(turtle.Turtle):
def __init__(self, shape: str = "classic", undobuffersize: int = 1000,
visible: bool = True) -> None:
super().__init__(shape, undobuffersize, visible)
def right(self, angle = 90):
super().right(angle)
def left(self, angle = 90):
super().left(angle)
def main():
# Set window title
turtle.Screen().title("Turtle Draws Me A House...")
# Get turtle object to draw with
t = ForgivingTurtle()
# Setup turtle properties. E.g. size, shape, color
t.shape("turtle")
t.shapesize(0.5, 0.5, 0.5)
t.fillcolor("green")
t.speed(0)
# Don't draw just yet
t.penup()
# Drawing code/functions goes here!
draw_sun(t)
draw_grass(t)
draw_clouds(t)
draw_house(t)
draw_garage(t)
# draw_window(t, 70, -75, 60)
draw_compound_window(t, 70, -75, 30)
# draw_window(t, -130, -75, 60)
draw_compound_window(t, -130, -75, 30)
draw_door(t)
# Finally, after everything is done, put the turtle out
# to roam the fields for the rest of eternity
# freeze()
graze(
t, x = -200, y = -270,
x_begin = -400, x_end = 400,
y_begin = -240, y_end = -400
)
def draw_grass(t: turtle.Turtle):
# Goto beginning of grass pos
t.right()
t.forward(240)
t.right()
t.forward(500)
t.right(180)
t.right(75 / 2)
# Save pos to go back to later
x, y = t.pos()
# Set grass color and start drawing
t.pen(pencolor="green", fillcolor="lime", pensize=3)
t.pendown()
t.begin_fill()
# Zig Zag to make triangle grass
for _ in range(35):
t.left(75)
t.forward(20)
t.right(75)
t.forward(20)
t.setheading(-90)
t.forward(200)
t.goto(x, y - 200)
t.goto(x, y)
t.end_fill()
# Reset & go home
t.pen(pencolor="black", fillcolor="", pensize=3)
t.penup()
t.home()
def draw_door(t: turtle.Turtle):
# Goto door position
t.back(30)
heading = t.heading()
t.setheading(-90)
t.forward(225)
t.setheading(heading)
# Draw door rectangle
t.pendown()
t.forward(60)
t.left()
t.forward(150)
t.left()
t.forward(60)
t.left()
t.forward(150)
# Goto door knob
t.penup()
t.left()
t.forward(60)
t.left()
t.forward(90)
t.left()
t.forward(10)
# Draw door knob
t.pendown()
t.circle(1)
# Go home
t.penup()
t.home()
def draw_compound_window(t: turtle.Turtle, x=0, y=0, z=100):
# Draws a window out of 4 smaller windows
draw_window(t, x, y, z)
draw_window(t, x + z, y, z)
draw_window(t, x, y - z, z)
draw_window(t, x + z, y - z, z)
def draw_window(t: turtle.Turtle, x=0, y=0, z=100):
# Goto right window pos
t.goto(x, y)
t.pendown()
# Draw one side of the window outline, with center frame
for _ in range(2):
t.forward(z / 2)
t.right()
t.forward(z)
t.back(z)
t.left()
t.forward(z / 2)
t.right()
# Finish drawing window outline
for _ in range(2):
t.forward(z)
t.right()
# Reset turt
t.penup()
t.home()
def draw_house(t: turtle.Turtle, a=350):
# Set color, and pen size
t.pen(pencolor="black", fillcolor="", pensize=3)
# Draw square house
t.pendown()
t.forward(a / 2)
t.right()
t.forward(a * 0.66)
t.right()
t.forward(a)
t.right()
t.forward(a * 0.66)
t.right()
t.forward(a / 2)
t.penup()
t.home()
# Draw triangle roof
t.forward(a / 2)
t.pendown()
t.goto(0, a * 0.40)
t.penup()
t.home()
t.back(a / 2)
t.pendown()
t.goto(0, a * 0.40)
t.penup()
# Go home
t.penup()
t.home()
def draw_garage(t: turtle.Turtle):
t.home()
t.forward(175)
# Draw garage outline
t.pendown()
t.forward(250)
t.right()
t.forward(231)
t.right()
t.forward(250)
# Goto garage door pos
t.penup()
t.right()
t.forward(25)
t.right()
t.forward(25)
t.pendown()
# Draw garage door outline
for i in range(4):
t.forward(200 if i % 2 == 0 else 140)
t.left()
t.left()
# Draw garage door slats
for _ in range(13):
t.forward(10)
t.right()
t.forward(200)
t.back(200)
t.left()
# Go to top of garage door
t.penup()
t.forward(30)
t.right()
t.forward(52)
# Save previous pen state
pen = t.pen()
# Set pen to bright pink
t.screen.colormode(255)
t.pencolor(255, 56, 175)
# Write "GARAGE" above garage
t.write('"GARAGE"', align="left", font=("Arial", 15, "normal"))
# Restore previous pen state
t.pen(pen)
# Reset turt after drawing
t.penup()
t.home()
def draw_sun(t: turtle.Turtle):
# Move to position of the sun
t.goto(390, 270)
# Draw the sun, as a yellow circle with orange outline
t.pendown()
t.pen(pencolor="orange", fillcolor="yellow", pensize=5)
t.begin_fill()
t.circle(60)
t.end_fill()
# Reset turt
t.penup()
t.home()
# Freezes the main window in its current state. Use to
# prevent the window from insta-closing after drawing
def freeze():
turtle.Screen().mainloop()
# Sets the turtles properties to be more suited for looks & animations,
# then infinitely makes the turtle walk around a given patch or position.
def graze(t: turtle.Turtle, x = 0, y = 0, x_begin = -300, x_end = 300, y_begin = -300, y_end = 300):
# Set turtle properties for animated grazing instead of drawing
t.penup()
# Set size, pos, & speed
t.shapesize(1.5, 1.5, 1.5)
t.goto(x, y)
t.speed(3)
# Set colors
t.screen.colormode(255)
t.pen(pencolor=(6, 54, 0), fillcolor=(141, 207, 0), pensize=5)
#t.pen(pencolor="green", fillcolor="lime", pensize=5)
# Forever make turtle walk around randomly (within given bounds)
# Moving it back to its starting position if it roams too far
while True:
# Check bounds
current_x, current_y = t.pos()
if not (x_begin > current_x > x_end or y_begin > current_y > y_end):
print("Reset Turtle")
t.goto(x, y)
continue
# Pick a random action (turn left/right or walk)
td = randrange(0, 5)
td = t.left if td == 0 else t.right if td == 1 else None
if td is None:
# If walk, walk a random amount of steps
t.forward(randrange(5, 15))
else:
# Else turn in the randomly chosen direction
td()
# Then sleep for a random amount of time, between s_min and s_max
s_min, s_max = 1, 9
sleep(float(
"0." + str(randrange(s_min, s_max))
))
# Draws a semi-random bunch of scattered clouds, centered around
# the midpoint of the given "sky" bounds
def draw_clouds(t: turtle.Turtle, n = 12, x_min = -360, x_max = 325, x_clamp = 55,
y_min = 250, y_max = 330, size_min = 1, size_max = 4):
# X-Pos variables
x_points = []
x_diff = x_max - x_min
x_2_diff = round(x_diff / 2)
x = x_min + x_2_diff
# Get a new random x position to place a cloud at, between x_min &
# x_max, centered around the middle of the sky, and not within
# x_clamp units of another cloud. This ensures clouds group around
# the middle of the sky, and spread out instead of clumping together
def get_new_x():
new_x = x + randrange(-x_2_diff, x_2_diff)
for p in x_points.copy():
try:
# If too close to another cloud, try again
if p - x_clamp < new_x < p + x_clamp:
return get_new_x()
except RecursionError: # If we're out of tries
# Find the difference between the min and max clamps
n_diff, p_diff = new_x - (p - x_clamp), (p + x_clamp) - new_x
# and push the x position to the closer of the two
new_x = new_x + p_diff if n_diff > p_diff else new_x - n_diff
x_points.append(new_x)
return new_x
# Y-Pos variables
y_diff = y_max - y_min
y_2_diff = round(y_diff / 2)
y = y_min + y_2_diff
# Get a new random y position between y_min & y_max, centered
# around the middle of the sky, to place a cloud at.
def get_new_y():
return y + randrange(-y_2_diff, y_2_diff)
# Actual cloud generation...
clouds = []
for _ in range(n):
# Generate and save all the values we'll be using to draw the clouds first
clouds.append({
"size": randrange(size_min, size_max + 1),
"x": get_new_x(),
"y": get_new_y()
})
# Then, ONLY when ALL values are known, do we draw the clouds,
# from biggest to smallest
for size in range(size_max, size_min - 1, -1):
for cloud in clouds:
if cloud["size"] == size:
draw_cloud(t, cloud["x"], cloud["y"], cloud["size"])
# Some cloud drawing code I found online and stuck in a function :)
# Modified slightly to fit the scene and allow size/position args
#
# Tutorial: Drawing Clouds with Python Turtle
# (https://pythonturtle.academy/tutorial-drawing-clouds-with-python-turtle/)
def draw_cloud(t: turtle.Turtle, x = -340, y = 250, size = 1):
t.pencolor("blue")
t.pensize(2)
resolution = 500
# X,Y is the center of ellipse, a is radius on x-axis, b is radius on y-axis
# ts is the starting angle of the ellipse, te is the ending angle of the ellipse
# P is the list of coordinates of the points on the ellipse
def ellipse(x, y, a, b, ts, te, p):
t = ts
for _ in range(resolution):
_x = a * math.cos(t)
_y = b * math.sin(t)
p.append((_x + x, _y + y))
t += (te - ts) / (resolution - 1)
# computes Euclidean distance between p1 and p2
def dist(p1, p2):
return ((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2) ** 0.5
# draws an arc from p1 to p2 with extent value ext
def draw_arc(p1, p2, ext):
t.penup()
t.goto(p1)
t.seth(t.towards(p2))
a = t.heading()
b = 360 - ext
c = (180 - b) / 2
d = a - c
e = d - 90
r = ( # r is the radius of the arc
dist(p1, p2) / 2 / math.sin(math.radians(b / 2))
)
t.seth(e) # e is initial heading of the circle
t.pendown()
t.circle(r, ext, 100)
# returns the landing position of the circle
# this position should be extremely close to p2 but may not be exactly the same
# return this for continuous drawing to the next point
return (t.xcor(), t.ycor())
def cloud(p):
step = resolution // 10 # draw about 10 arcs on top and bottom part of cloud
a = 0 # a is index of first point
b = a + random.randint(step // 2, step * 2) # b is index of second point
p1 = p[a] # p1 is the position of the first point
p2 = p[b] # p2 is the position of the second point
t.fillcolor("cyan")
t.begin_fill()
p3 = draw_arc(
p1, p2, random.uniform(70, 180)
) # draws the arc with random extension
while b < len(p) - 1:
p1 = p3 # start from the end of the last arc
if b < len(p) / 2: # first half is top, more ragged
ext = random.uniform(70, 180)
b += random.randint(step // 2, step * 2)
else: # second half is bottom, more smooth
ext = random.uniform(30, 70)
b += random.randint(step, step * 2)
b = min(b, len(p) - 1) # make sure to not skip past the last point
p2 = p[b] # second point
p3 = draw_arc(p1, p2, ext) # draws an arc and return the end position
t.end_fill()
p = [] # starting from empty list
ellipse(x, y, 37.5 * size, 25 * size, 0, math.pi, p) # taller top half
ellipse(x, y, 37.5 * size, 6.25 * size, math.pi, math.pi * 2, p) # shorter bottom half
cloud(p)
# Reset turt
t.penup()
t.home()
if __name__ == "__main__":
main()