-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot_artist.py
286 lines (257 loc) · 8.68 KB
/
robot_artist.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
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
'''
This is the artist class for the robot. It will extend the Artist class in
the artist.py file. If need be, check the Artist class for more detail.
'''
from artist import Artist
import turtle
import math
import itertools
class RobotArtist(Artist):
# Constructor
# Parameters:
# r: radius for the GUI element
def __init__(self, r):
super().__init__(r)
self.x_pos = 0
self.y_pos = 0
self.is_alive = True
self.ori = 0
self.sense_angs = None
self.smell_sigs = None
self.water_batts = None
self.food_batts = None
self.orientation_turtle = turtle.Turtle()
self.water_battery_turtle = turtle.Turtle()
self.food_battery_turtle = turtle.Turtle()
self.sensor_turtles = []
self.sensor_sig_turtles = []
''' --------------Setters for build method (head)-------------- '''
def alive(self, alive):
self.is_alive = alive
return self
def orientation(self, ori):
self.ori = ori + math.pi / 2
return self
def sensor_angles(self, sense_angs):
self.sense_angs = sense_angs
self.sensor_turtles = [turtle.Turtle() for _ in sense_angs]
self.sensor_sig_turtles = [[turtle.Turtle() for _ in range(4)] for _ in sense_angs]
return self
def smell_signatures(self, smell_sigs):
self.smell_sigs = smell_sigs
return self
def water_battery(self, water_batts):
self.water_batts = water_batts
return self
def food_battery(self, food_batts):
self.food_batts = food_batts
return self
''' --------------Setters for build method (tail)-------------- '''
# Method for drawing the robot
# Parameters:
# ...
def draw(self):
self.artist.clear()
if self.is_alive:
# fill in the colour
self.fill_colour()
# draw the main body
self.draw_body()
# draw the details
self.draw_detail()
# Method for filling in the colour of the element
# Parameters:
# ...
def fill_colour(self):
self.col = "green"
super().fill_colour()
# Method for drawing in the details of the robot.
# These details include:
# orientation of the robot
# sensors
# battery levels
# food/water source remaining
# smell signature
# possibly genes if there is time
def draw_detail(self):
self.draw_orientation()
if self.smell_sigs is None:
self.draw_sensors()
else:
self.draw_smell_sig_sensors()
self.draw_batteries()
# Method for drawing the direction arrow of the robot
# Parameters:
# ...
def draw_orientation(self):
dis = self.radius * 30
t = self.orientation_turtle
t.clear()
t.radians()
t.pencolor("white")
t.pensize(3)
t.penup()
t.goto(self.x_pos, self.y_pos)
# Move ahead of the robot and save the position
t.setheading(self.ori)
t.forward(dis)
t.pendown()
(x, y) = t.position()
# draw the arrows
# left arrow
t.left(22 * math.pi/36)
t.forward(self.radius * 20)
t.penup()
# right arrow
t.goto(x, y)
t.setheading(self.ori)
t.pendown()
t.right(22 * math.pi/36)
t.forward(self.radius * 20)
t.penup()
t.hideturtle()
# STANDARD SENSORS (ie just a black block)
# Method for drawing the sensors on the robot
# Parameters:
# ...
def draw_sensors(self):
dis = self.radius * 22
for sa, t in zip(self.sense_angs, self.sensor_turtles):
t.clear()
t.radians()
t.pencolor("white")
t.fillcolor("black")
t.pensize(2)
t.shapesize(0.6, 0.3)
# go to where the robot is
t.penup()
t.goto(self.x_pos, self.y_pos)
# turn to the direction of where the sensor is
t.setheading(self.ori + sa)
# go to the edge of the robot
t.forward(dis)
t.pendown()
# draw a quadralateral
t.shape("square")
# SMELL SIGNATURE SENSORS (ie displays smeel signatures as colours)
# Method for drawing the sensors on the robot
# Parameters:
# ...
# Calculation for colours
# let si be element of smell signature
# xi = floor((si + 1) * 128)
# left-top rgb: x1, 255, x2
# left-bottom rgb: x3, x4, x5
# right-top rgb: 255 - x1, 0, 255 - x2
# right-bottom rgb: 255 - x3, 255 - x4, 255 - x5
def draw_smell_sig_sensors(self):
dis = self.radius * 22
counter = 0
for sa, turtles in zip(self.sense_angs, self.sensor_sig_turtles):
# calculate xi for colours
x_list = []
for s in self.smell_sigs[counter]:
x = math.floor((s + 1) * 127)
x_list.append(x)
# create the turtles for each of the sensor colours
for i, t in zip(range(0, 4), turtles):
t.clear()
t.radians()
t.pencolor("white")
t.pensize(0.3)
t.shapesize(0.3, 0.15)
# go to where the robot is
t.penup()
t.goto(self.x_pos, self.y_pos)
# turn to the direction of where the sensor is
t.setheading(self.ori + sa)
# go to the edge of the robot
t.forward(dis)
# go to the necessary division and fill colour
if i == 0:
# left top corner
# rgb = x1, 255, x2
t.left(math.pi/2)
t.forward(2)
t.right(math.pi/2)
t.forward(2)
t.fillcolor(x_list[0], 255, x_list[1])
elif i == 1:
# left bottom corner
# rgb = x3, x4, x5
t.left(math.pi/2)
t.forward(4)
t.left(math.pi/2)
t.forward(2)
t.fillcolor(x_list[2], x_list[3], x_list[4])
elif i == 2:
# right top corner
# rgb = 255 - x1, 0, 255 - x2
t.right(math.pi/2)
t.forward(4)
t.left(math.pi/2)
t.forward(2)
t.fillcolor(255 - x_list[0], 0, 255 - x_list[1])
elif i == 3:
# right bottom corner
# rgb = 255 - x3, 255 - x4, 255 - x5
t.right(math.pi/2)
t.forward(4)
t.right(math.pi/2)
t.forward(2)
t.fillcolor(255 - x_list[2], x_list[3], x_list[4])
t.pendown()
t.shape("square")
counter = counter + 1
# Method for drawing the battery level on the robot
# Parameters:
# ...
def draw_batteries(self):
dis = self.radius * 7
size = self.radius * 10
# draw food battery
tf = self.food_battery_turtle
tf.clear()
tf.radians()
tf.pencolor("black")
tf.fillcolor("yellow")
tf.pensize(2)
tf.shapesize(self.food_batts, 0.5)
# go to robot's right hand side body
tf.penup()
tf.goto(self.x_pos, self.y_pos)
tf.setheading(self.ori)
tf.right(math.pi/2)
tf.forward(dis)
tf.pendown()
tf.shape("square")
# draw water battery
tw = self.water_battery_turtle
tw.clear()
tw.radians()
tw.pencolor("black")
tw.fillcolor("blue")
tw.pensize(2)
tw.shapesize(self.water_batts, 0.5)
# go to robot's left hand side body
tw.penup()
tw.goto(self.x_pos, self.y_pos)
tw.setheading(self.ori)
tw.left(math.pi/2)
tw.forward(dis)
tw.pendown()
tw.shape("square")
def clear(self):
super().clear()
self.water_battery_turtle.clear()
self.food_battery_turtle.clear()
self.orientation_turtle.clear()
for t in itertools.chain(self.sensor_turtles, *self.sensor_sig_turtles):
t.clear()
def destroy(self):
super().destroy()
turtle.turtles().remove(self.water_battery_turtle)
turtle.turtles().remove(self.food_battery_turtle)
turtle.turtles().remove(self.orientation_turtle)
for t in itertools.chain(self.sensor_turtles, *self.sensor_sig_turtles):
turtle.turtles().remove(t)