-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinal_Project_18146619_2018_CS001.py
222 lines (190 loc) · 10.4 KB
/
Final_Project_18146619_2018_CS001.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
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
# Final Project CS001 - Graham Keane 18146619:
# Import Turtle Module:
import turtle
from random import randint # Allows me to use the random integer function to assign values to variables
# Create graphics screen:
win = turtle.Screen()
win.setup(.99, .95, 0, 0) # Sets the width and height of the screen
win.bgcolor("black")
win.title("Final Project 18146619")
# Turtles: I will create six turtles to draw the complex hexagon.
don = turtle.Turtle()
raph = turtle.Turtle()
leo = turtle.Turtle()
mikey = turtle.Turtle()
splinter = turtle.Turtle()
shredder = turtle.Turtle()
names = [don, raph, leo, mikey, splinter,
shredder] # I'm using a list to handle the turtles when modifying their parameters in other functions
# Colour changer will allow turtles to change pen colour during each iteration of a for-loop
def colour_changer(name): # The first function creates rgb values generated by the randint function to vary the pen
# colour of each turtle for each call of the main draw function
r = randint(0, 255) # defines variables r,g,b whose value is an integer
g = randint(0, 255) # which is between 0 and 255. It is random, and
b = randint(0, 255) # changes every time the function runs.
win.colormode(255) # Sets the range for each of the rgb triplets in the graphics window
name.pencolor(r, g, b) # Individual turtle pen colour determined by colour triples generated by randint function
def multi_colour_changer(): # Meta-function cycles through the list 'names' and applies the colour changer function
for name in names:
colour_changer(name)
# The following turtle_int function is designed to initialise the turtles in each subsequent function.
# I was having a problem where calling a turtle after it had previously been used by another function caused it be
# unresponsive. So I'm using a function that sets the parameters of position, pensize, visibility, pen colour, speed
# and shape for each function.
#
# I will also define a multi_int function to allow me to initialise all six turtles simultaneously. In this particular
# case I want the turtles to be hidden and the speed and pensize to be controllable. I will include name, speed, and
# pen colour arguments that filter down from the main drawing function, through the multi_int function, to the
# individual turtle_int functions. I can make any changes to the properties of the turtles in these int_functions rather
# than manually changing or entering those properties at each iteration.
def turtle_int(name, speed, pensize,
shape): # Parameter setting function can be used within other functions at any time
name.penup()
multi_colour_changer()
name.shape(shape)
name.pensize(pensize) # Pensize determined by function input
name.speed(speed) # Speed determined by function input
name.setposition(0,
0) # Not necessary for the complex hexagon, but useful when calling the turtle to other functions
name.pendown()
# Multi-function to call turtles simultaneously with name arguments, and speed and pensize arguments that are inputted
# from the main function.
def multi_int6(speed, pensize, shape):
for name in names: # I had to call the hideturtle function outside of the turtle_int function as inside the
# function it was cycling through all the commands first so the turtles were visible when
# I didn't want them to be
name.hideturtle()
for name in names:
turtle_int(name, speed, pensize, shape)
def turtle_seth(x, y, z, w, r, q): # Set heading of turtles from 0 - 360 degrees
don.seth(x)
leo.seth(y)
raph.seth(z)
mikey.seth(w)
splinter.seth(r)
shredder.seth(q)
def move_6turtles_forward(x): # Move turtles forward X units
don.forward(x)
leo.forward(x)
raph.forward(x)
mikey.forward(x)
splinter.forward(x)
shredder.forward(x)
def move_6turtles_left(x): # Turn turtles left X degrees
don.left(x)
leo.left(x)
raph.left(x)
mikey.left(x)
splinter.left(x)
shredder.left(x)
def move_6turtles_right(x): # Turn turtles right X degrees
don.right(x)
leo.right(x)
raph.right(x)
mikey.right(x)
splinter.right(x)
shredder.right(x)
# Main drawing function: This function will use six turtles to create a complex hexagon. Each turtle will trace an
# equilateral triangle by tracing a sine-wave which increases in base length after each iteration. The effect of the
# base modifier 'c' is to extend the base of each subsequent level of the triangle as the turtles move out from the
# origin. For an equilateral triangle we have each side being of equal length. As the turtles are tracing subunit
# triangles within a final larger triangle, the base needs to be increased at the end of each iteration to keep the
# ratio of the sides equivalent.
def complex_hexagon(speed, pensize, displacement):
multi_int6(speed, pensize, "circle") # Sets the initial parameters of the turtles
turtle_seth(0, 60, 120, 180, 240, 300) # Set the heading of each turtle
y = 120 # arc one
z = 60 # arc two
c = 1 # base length modifier
for i in range(displacement): # Sine-wave routine
if i % 2 == 0: # Alternates the background colour by selection. If the modulo 2 of i is even, bg is black;
# modulo 3 of i is even, bg is red; otherwise bg is blue
win.bgcolor("black")
elif i % 3 == 0:
win.bgcolor("red")
else:
win.bgcolor("blue")
# step 1: turtles move forward displacement units
move_6turtles_forward(displacement)
# step 2: turtles turn y degrees left relative to forward-position
move_6turtles_left(y)
# step 3.1: base length extension of c * displacement
move_6turtles_forward(c * displacement)
# step 3.2: base length modifier increases by 1 so now c = 2
c += 1
# step 4: turtles turn z degrees right relative to forward-position
move_6turtles_right(z)
# step 5: turtles move forward displacement units
move_6turtles_forward(displacement)
# step 6: turtles turn y degrees right relative to forward-position
move_6turtles_right(y)
# step 7.1: base length extension of c * displacement
move_6turtles_forward(c * displacement)
# step 7.2: base length modifier increases by 1 so now c = 3
c += 1
# step 8: turtles turn z degrees left relative to forward-position
move_6turtles_left(z)
# I defined a function to draw a pastel-coloured spiral using a modified square-drawing routine and a while loop.
# I used a while loop so I could cycle the colours of each turtle more effectively. The parameters of the function
# are similar to the complex_hexagon function with the addition of 'arc' and 'limit', which define the rotation and
# end-of-loop respectively, that is the while loop continues until 'i' is equal to 'limit' at which point the loop stops
def pastel_spiral(speed, pensize, displacement, arc, limit):
i = 1 # displacement modifier
multi_int6(speed, pensize, "circle") # Set the turtles parameters
while i < limit:
if i % 2 == 0: # Alternates the background colour by selection. If the modulo 2 of i is even, bg is black;
# modulo 3 of i is even, bg is red; otherwise bg is blue
win.bgcolor("black")
elif i % 3 == 0:
win.bgcolor("red")
elif i % 5 == 0:
win.bgcolor("white")
else:
win.bgcolor("blue")
multi_colour_changer()
move_6turtles_forward(displacement + i) # displacement increases by i each cycle
move_6turtles_right(arc)
i += 1 # displacement modifier increases by 1 each cycle
# This is the same as the previous pastel_spiral except the direction of rotation is opposite
def pastel_spiral2(speed, pensize, displacement, arc, limit):
i = 1 # displacement modifier
multi_int6(speed, pensize, "circle") # Set the turtles parameters
while i < limit:
if i % 2 == 0: # Alternates the background colour by selection. If the modulo 2 of i is even, bg is black;
# modulo 3 of i is even, bg is red; otherwise bg is blue
win.bgcolor("black")
elif i % 3 == 0:
win.bgcolor("red")
elif i % 5 == 0:
win.bgcolor("white")
else:
win.bgcolor("blue")
multi_colour_changer()
move_6turtles_forward(displacement + i) # displacement increases by i each cycle
move_6turtles_left(arc)
i += 1 # displacement modifier increases by 1 each cycle
# I was inspired by stephen's use of the write function so I decided to incorporate it into mine.
def screen_string(name, message):
name.hideturtle()
name.penup()
name.pencolor("white")
name.color("white")
name.goto(0, 0)
name.pendown()
name.write(str(message), False, "center", ("Dyuthi", 80, "bold")) # This is the function that writes text to the
# screen. I'm using Dyuthi font in bold and I've centred the text on the graphics
# When calling the complex_hexagon function we need to provide the arguments speed, pensize, and displacement.
# Each argument takes an integer value as its input in order to work. An input of type string or float will return a
# type error for the displacement argument. (It's ok for the pensize and speed though)
complex_hexagon(20, 5, 13)
# This function speeds up the animation process. The first variable achieves this by having values
# slightly less than 1.0
# From here on, I am calling all of my functions to produce my psychedelic spiral design!
win.tracer(.95, 0) # Increases the animation rate as per the arguments set out in the parentheses
pastel_spiral(10, 5, 5, 99, 700) # Draws pastel_spiral as per the arguments defined in the parentheses
screen_string(don, "!!!! DON'T PANIC !!!!") # Writes string "!!!! Don't Panic !!!!
pastel_spiral2(15, 5, 5, 84, 800) # Draws pastel_spiral2 as per the arguments defined in the parentheses
win.tracer(1, 0) # Decreases the animation rate as per the arguments set out in the parentheses
complex_hexagon(20, 5, 15) # Draws complex_hexagon as per the arguments defined in the parentheses
screen_string(don, "FIN") # Writes string "FIN"
win.exitonclick() # Exits the graphics window upon mouse-click