-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathturtlehack.py
116 lines (89 loc) · 3.09 KB
/
turtlehack.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
import turtle
import random
# A function that takes a turtle, a radius, a color, and an optional
# thickness and draws a circle
def colored_circle(turtle, radius, color, thickness=1):
turtle.width(thickness)
turtle.color(color)
turtle.circle(radius)
# A function that takes a side length and a color and makes a square.
def colored_square(turtle, side_length, color):
turtle.color(color)
for i in range(4):
turtle.forward(side_length)
turtle.left(90)
turtle.left(90)
# A function that take a turtle, dot size, color, and number of dots in
# line to make a dotted line
def dotted_line(turtle, number_of_dots, dot_size, color):
for i in range(number_of_dots):
turtle.dot(dot_size, color)
turtle.penup()
turtle.forward(dot_size * 2)
turtle.pendown()
# A function that takes a number and makes that many random sized circles
def random_circle(turtle, number_of_circles, max_size=100):
for i in range(number_of_circles):
turtle.circle(random.randint(0, max_size + 1))
# A function that changes the turtle's color to a random color
def random_color():
'''
returns a random hex value
'''
color_value = format(random.randint(0, 16777215), '06x')
return "#" + color_value
# A function that takes a turtle and a pair of numbers and sets the turtle
# to a random location from x to -x and y to -y
def random_location(turtle, x, y, relative=False):
'''
Moves a turtle to a random location within x,y bounds
takes turtle, x bound, y bound, uselrelative location (boolean)
'''
if relative is False:
random_x = random.randint(-x, x)
random_y = random.randint(-y, y)
turtle.setpos(random_x, random_y)
if relative is True:
random_x = turtle.xcor() + random.randint(-x, x)
random_y = turtle.ycor() + random.randint(-y, y)
turtle.setpos(random_x, random_y)
else:
raise Exception("expected boolean")
# A function that makes n random colored and located dots inside x, y
# Thanks to Stacey Mantooth for the idea
def pox(turtle, x, y, n=10):
origx = turtle.xcor()
origy = turtle.ycor()
turtle.penup()
for i in range(n):
random_location(turtle, x, y, True)
turtle.dot(random.randint(3, 10), random_color())
turtle.setpos(origx, origy)
turtle.pendown()
# A function that draws an n-sided polygon
def n_sided_polygon(
turtle,
n,
color="#FFFFFF",
line_thickness=1,
line_length=80):
'''
Draw an n-sided polygon
input: turtle, number of sides, line color, line thickness, line length
'''
# for n times:
# Draw a line, then turn 360/n degrees and draw another
# set initial parameters
turtle.degrees()
turtle.pensize(line_thickness)
turn_angle = (360 / n)
# Draw each line segment and turn
for i in range(0, n):
turtle.color(color)
turtle.pendown()
turtle.forward(line_length)
turtle.penup()
turtle.left(turn_angle)
# return the turtle to its original starting location
turtle.left(turn_angle)
return 0