/
example.py
61 lines (37 loc) · 1.5 KB
/
example.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
import random
import simu
########################################################################
# Example of an agent, which moves randomly until mouse is pressed
# after which it tries to go to that position. This agent is rather
# stupid so it will get stuck if there are obstacles in its path.
class my_agent(simu.agent):
def __init__(self, name, color):
super(my_agent, self).__init__(name, color)
# Add possible initialization below this line.
def execute(self):
mouse_pos = self.mouse()
if mouse_pos != None:
self.params['target'] = mouse_pos
direction = None
if 'target' in self.params:
target = self.params['target']
pos = self.get_position()
if pos[0] - target[0] < 0:
direction = 'right'
elif pos[0] - target[0] > 0:
direction = 'left'
elif pos[1] - target[1] < 0:
direction = 'up'
elif pos[1] - target[1] > 0:
direction = 'down'
self.move(direction)
else:
self.move(random.choice(['up', 'down', 'left', 'right']))
# Initialize the simulator
S = simu.simu(walls = [[13, 25, 30, 25], [10, 15, 25, 15], [11, 5, 11, 16], [24, 5, 24, 16]])
# Add some agents of class my_agent to the bottom of the window starting
# from origo..
for i in xrange(20):
S.add_agent(my_agent('stupid_agent' + str(i), (255, 255, 0)), (i, 0))
# Run the simulation
S.run()