@@ -30,7 +30,7 @@ def talk(self):
30
30
pass
31
31
32
32
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 :
34
34
self .x += self .dx
35
35
self .y += self .dy
36
36
else :
@@ -49,34 +49,53 @@ def update_direction(self): #TODO stuck in corner and border.
49
49
elif self .angle >= 270 and self .angle < 360 :
50
50
self .dx = 1 / math .sqrt (1 + self .k ** 2 )
51
51
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
+
53
56
54
57
# First set up the figure, the axis, and the plot element we want to animate
55
58
fig = plt .figure ()
56
59
ax = plt .axes (xlim = (- 12 , 12 ), ylim = (- 12 , 12 ))
57
60
plt .grid (True )
58
61
# 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 ]
60
64
61
65
# initialization function: plot the background of each frame
62
66
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
+
66
75
67
76
# animation function. This is called sequentially
68
77
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
+
77
96
78
97
# call the animator. blit=True means only re-draw the parts that have changed.
79
98
anim = animation .FuncAnimation (fig , animate , init_func = init ,
80
- frames = 200 , interval = 1000 , blit = True )
99
+ frames = 200 , interval = 10 , blit = True )
81
100
82
101
plt .show ()
0 commit comments