-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpygame_test.py
241 lines (153 loc) · 5.16 KB
/
pygame_test.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
"""Pygame test for multi agent modeling
Tutorials
https://zestedesavoir.com/tutoriels/846/pygame-pour-les-zesteurs/1381_a-la-decouverte-de-pygame/creer-une-simple-fenetre-personnalisable/#1-15425_creons-une-fenetre-basique
https://www.pygame.org/docs/ref/rect.html#pygame.Rect.move_ip
https://stackoverflow.com/questions/32061507/moving-a-rectangle-in-pygame
Ideas:
- Add circles
- Pathfinding algorithm
- Obstacles
- Colliders
- Clicking to add agent or wall
- Grid
- AutoMaze
- Raytracing
- Change Icon
- Heatmaps of where agents were located (for retail purposes)
Projects:
- Epidemiology
- See MESA or NetLogo examples
- Bunny & Rabbits
"""
import numpy as np
import pygame
import time
import uuid
# import os
# os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (320,240)
pygame.init()
pygame.display.set_caption("Multi Agent Modeling Environment")
# ecran = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
screen = pygame.display.set_mode((1000, 600))
simulation_on = True
# time.sleep(5)
background_colour = (0, 0, 0)
class RectangleAgent:
def __init__(self,width,height,x,y,screen = None):
# Rect left top width height
self.screen = screen
self.fig = pygame.rect.Rect((x,y,width,height))
# print(f"Initialized rect at {self.pos}")
self.change_direction()
self.agent_id = str(uuid.uuid1())
@property
def pos(self):
return self.fig.x,self.fig.y,self.fig.width,self.fig.height
def move_at(self,x,y):
self.x = x
self.y = y
def wander(self,dl):
self.move(angle = self.direction_angle,dl = dl)
def change_direction(self):
self.direction_angle = np.random.uniform(0,2*np.pi)
def move_towards(self):
pass
def collides(self,agents):
if len(agents) == 0:
collisions = []
else:
other_agents = [agent.fig for agent in agents if agent.agent_id != self.agent_id]
collisions = self.fig.collidelistall(other_agents)
if len(collisions) > 0:
return True,collisions
else:
return False,collisions
def if_collides(self,agents):
is_collision,collisions = self.collides(agents)
if is_collision:
self.direction_angle += np.pi
def move(self,dx = 0,dy = 0,angle = None,dl = None,colliders = None):
if angle is not None:
assert dl is not None
# Compute delta directions with basic trigonometry
dx = dl * np.cos(angle)
dy = dl * np.sin(angle)
self.move(dx = dx,dy = dy)
else:
screen_width = self.screen.get_width()
screen_height = self.screen.get_height()
old_x = self.fig.x
old_y = self.fig.y
if self.fig.x + dx > screen_width:
self.fig.x = 0
elif self.fig.x + dx < 0:
self.fig.x = screen_width
else:
self.fig.x = self.fig.x + dx
if self.fig.y + dy > screen_height:
self.fig.y = 0
elif self.fig.y + dy < 0:
self.fig.y = screen_height
else:
self.fig.y = self.fig.y + dy
if colliders is not None:
if self.collides(colliders):
self.fig.x = old_x
self.fig.y = old_y
# print(f"Position at {self.fig.x},{self.fig.y}")
def render(self,color = (180,20,150)):
pygame.draw.rect(self.screen,color,self.pos)
# pygame.draw.circle(self.screen,color,(self.fig.x,self.fig.y),10)
# pass
class Obstacle:
def __init__(self,width,height,x,y,screen = None):
# Rect left top width height
self.screen = screen
self.fig = pygame.rect.Rect((x,y,width,height))
# print(f"Initialized rect at {self.pos}")
self.agent_id = str(uuid.uuid1())
def render(self,color = (10,150,10)):
pygame.draw.rect(self.screen,color,self.pos)
@property
def pos(self):
return self.fig.x,self.fig.y,self.fig.width,self.fig.height
size = 10
n_rects = 500
rects = []
for i in range(n_rects):
new_rect = RectangleAgent(
size,size,
np.random.uniform(0,screen.get_width()),
np.random.uniform(0,screen.get_height()),
screen,
)
rects.append(new_rect)
i = 0
stop = 1000
obstacles = [
Obstacle(200,200,300,300,screen)
]
while simulation_on:
screen.fill(background_colour)
for rect in rects:
rect.wander(size)
rect.if_collides(rects + obstacles)
for rect in rects + obstacles:
rect.render()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
simulation_on = False
elif event.type == pygame.MOUSEBUTTONUP:
new_x,new_y = pygame.mouse.get_pos()
# new_rect = RectangleAgent(size,size,new_x,new_y,screen)
# rects.append(new_rect)
new_obs = Obstacle(20,20,new_x,new_y,screen)
obstacles.append(new_obs)
pygame.display.update()
# pygame.display.flip()
time.sleep(0.05)
if i == stop:
simulation_on = False
else:
i+=1
pygame.quit()