Skip to content

Commit dd6b7fb

Browse files
author
superPershing
committed
add 4 robot random walk, but return objects todo
1 parent ba25388 commit dd6b7fb

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed

wedge_animation.py

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def talk(self):
3030
pass
3131

3232
def update_direction(self): #TODO stuck in corner and border.
33-
if (self.x + self.dx) < 10 and (self.x + self.dx) > -10 and (self.y + self.dy) < 10 and (self.y + self.dx) > -10:
33+
if (self.x + self.dx) < 10 and (self.x + self.dx) > -10 and (self.y + self.dy) < 10 and (self.y + self.dy) > -10:
3434
self.x += self.dx
3535
self.y += self.dy
3636
else:
@@ -49,34 +49,53 @@ def update_direction(self): #TODO stuck in corner and border.
4949
elif self.angle >= 270 and self.angle < 360:
5050
self.dx = 1 / math.sqrt(1 + self.k ** 2)
5151
self.dy = - math.sqrt(1 - self.dx ** 2)
52-
robot0 = Robot(random.randint(-10, 10), random.randint(-10, 10), 0, 1)
52+
53+
# robot0 = Robot(random.randint(-10, 10), random.randint(-10, 10), 0, 1)
54+
robots = [Robot(random.randint(-10, 10), random.randint(-10, 10), 0, 1) for i in range(4)]
55+
5356

5457
# First set up the figure, the axis, and the plot element we want to animate
5558
fig = plt.figure()
5659
ax = plt.axes(xlim=(-12, 12), ylim=(-12, 12))
5760
plt.grid(True)
5861
# patch_one = matplotlib.patches.Circle((0, 0), 1)
59-
patch_one = matplotlib.patches.Wedge((robot0.x,robot0.y), 1, robot0.angle - 25, robot0.angle + 25)
62+
# patch_one = matplotlib.patches.Wedge((robot0.x,robot0.y), 1, robot0.angle - 25, robot0.angle + 25)
63+
patchs = [matplotlib.patches.Wedge((robot_i.x,robot_i.y), 1, robot_i.angle - 25, robot_i.angle + 25) for robot_i in robots]
6064

6165
# initialization function: plot the background of each frame
6266
def init():
63-
patch_one.radius = 1
64-
ax.add_patch(patch_one)
65-
return patch_one,
67+
# patch_one.radius = 1
68+
# ax.add_patch(patch_one)
69+
# return patch_one,
70+
for i in range(4):
71+
patchs[i].radius = 1
72+
ax.add_patch(patchs[i])
73+
return patchs[0],patchs[1],patchs[2],patchs[3],
74+
6675

6776
# animation function. This is called sequentially
6877
def animate(frame):
69-
robot0.update_direction()
70-
# robot0.random_walk()
71-
patch_one.set_center((robot0.x, robot0.y))
72-
patch_one.set_theta1(robot0.angle - 25)
73-
patch_one.set_theta2(robot0.angle + 25)
74-
patch_one._recompute_path()
75-
print(robot0.angle)
76-
return patch_one,
78+
# robot0.update_direction()
79+
# # robot0.random_walk()
80+
# patch_one.set_center((robot0.x, robot0.y))
81+
# patch_one.set_theta1(robot0.angle - 25)
82+
# patch_one.set_theta2(robot0.angle + 25)
83+
# patch_one._recompute_path()
84+
# print(robot0.angle)
85+
# return patch_one,
86+
for i in range(4):
87+
robots[i].update_direction()
88+
# robot0.random_walk()
89+
patchs[i].set_center((robots[i].x, robots[i].y))
90+
patchs[i].set_theta1(robots[i].angle - 25)
91+
patchs[i].set_theta2(robots[i].angle + 25)
92+
patchs[i]._recompute_path()
93+
# print(robots[i].angle)
94+
return patchs[0],patchs[1],patchs[2],patchs[3],
95+
7796

7897
# call the animator. blit=True means only re-draw the parts that have changed.
7998
anim = animation.FuncAnimation(fig, animate, init_func=init,
80-
frames=200, interval=1000, blit=True)
99+
frames=200, interval=10, blit=True)
81100

82101
plt.show()

0 commit comments

Comments
 (0)