-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnants_dots.py
312 lines (279 loc) · 14.5 KB
/
nants_dots.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/usr/bin/env python3
from math import pi, sin, cos, atan2, radians, degrees
from random import randint
import pygame as pg
'''
NAnts - Old attempt to find food using dot pheromone trails. Barely kinda works.
Copyright (c) 2021 Nikolaus Stromberg [email protected]
'''
FLLSCRN = False # True for Fullscreen, or False for Window.
ANTS = 80 # Number of Ants to spawn.
WIDTH = 1200 # default 1200
HEIGHT = 800 # default 800
FPS = 60 # 48-90
class Ant(pg.sprite.Sprite):
def __init__(self, drawSurf, nest):
super().__init__()
self.drawSurf = drawSurf
self.nest = nest
self.image = pg.Surface((12, 21)).convert()#, pg.HWSURFACE)
self.image.set_colorkey(0)
cBrown = (80,42,42)
# Draw Ant
pg.draw.aaline(self.image, cBrown, [0, 5], [11, 15])
pg.draw.aaline(self.image, cBrown, [0, 15], [11, 5]) # legs
pg.draw.aaline(self.image, cBrown, [0, 10], [12, 10])
pg.draw.aaline(self.image, cBrown, [2, 0], [4, 3]) # antena l
pg.draw.aaline(self.image, cBrown, [9, 0], [7, 3]) # antena r
pg.draw.ellipse(self.image, cBrown, [3, 2, 6, 6]) # head
pg.draw.ellipse(self.image, cBrown, [4, 6, 4, 9]) # body
pg.draw.ellipse(self.image, cBrown, [3, 13, 6, 8]) # rear
# save drawing for later
self.orig_img = pg.transform.rotate(self.image.copy(), -90)
self.rect = self.image.get_rect(center=self.nest)
self.ang = randint(0, 360)
self.desireDir = pg.Vector2(cos(radians(self.ang)),sin(radians(self.ang)))
self.pos = pg.Vector2(self.rect.center)
self.vel = pg.Vector2(0,0)
self.last_phero = nest
self.mode = 0
def update(self, dt): # behavior
curW, curH = self.drawSurf.get_size()
mid_result = left_result = right_result = [0,0,0]
randAng = randint(0,360)
accel = pg.Vector2(0,0)
wandrStr = .14
maxSpeed = 11 # more than 11 may stretch pheros too much
steerStr = 3 # 2-4
if self.mode == 0 and self.pos.distance_to(self.nest) > 42:
self.mode = 1
#mid_sensr = vec2round(self.pos + pg.Vector2(20, 0).rotate(self.ang))#.normalize() # directional vec forward
mid_sensL = Vec2.vint(self.pos + pg.Vector2(21, -3).rotate(self.ang))
mid_sensR = Vec2.vint(self.pos + pg.Vector2(21, 3).rotate(self.ang))
left_sensr1 = Vec2.vint(self.pos + pg.Vector2(18, -14).rotate(self.ang))
left_sensr2 = Vec2.vint(self.pos + pg.Vector2(16, -21).rotate(self.ang))
right_sensr1 = Vec2.vint(self.pos + pg.Vector2(18, 14).rotate(self.ang))
right_sensr2 = Vec2.vint(self.pos + pg.Vector2(16, 21).rotate(self.ang))
# either mid sensor needs to be a bit in front, or side sensors need to be more back..
#pg.draw.circle(self.drawSurf, (200,0,200), mid_sensL, 1)
#pg.draw.circle(self.drawSurf, (200,0,200), mid_sensR, 1)
#pg.draw.circle(self.drawSurf, (200,0,200), left_sensr1, 1)
#pg.draw.circle(self.drawSurf, (200,0,200), left_sensr2, 1)
#pg.draw.circle(self.drawSurf, (200,0,200), right_sensr1, 1)
#pg.draw.circle(self.drawSurf, (200,0,200), right_sensr2, 1)
if self.drawSurf.get_rect().collidepoint(mid_sensL) and self.drawSurf.get_rect().collidepoint(mid_sensR):
ms_rL = self.drawSurf.get_at(mid_sensL)[:3]
ms_rR = self.drawSurf.get_at(mid_sensR)[:3]
mid_result = (max(ms_rL[0], ms_rR[0]), max(ms_rL[1], ms_rR[1]), max(ms_rL[2], ms_rR[2]))
if self.drawSurf.get_rect().collidepoint(left_sensr1) and self.drawSurf.get_rect().collidepoint(left_sensr2):
ls_r1 = self.drawSurf.get_at(left_sensr1)[:3]
ls_r2 = self.drawSurf.get_at(left_sensr2)[:3]
left_result = (max(ls_r1[0], ls_r2[0]), max(ls_r1[1], ls_r2[1]), max(ls_r1[2], ls_r2[2]))
if self.drawSurf.get_rect().collidepoint(right_sensr1) and self.drawSurf.get_rect().collidepoint(right_sensr2):
rs_r1 = self.drawSurf.get_at(right_sensr1)[:3]
rs_r2 = self.drawSurf.get_at(right_sensr2)[:3]
right_result = (max(rs_r1[0], rs_r2[0]), max(rs_r1[1], rs_r2[1]), max(rs_r1[2], rs_r2[2]))
if self.pos.distance_to(self.last_phero) > 22 and self.mode != 3: # 20-25 seems best, too high they get distracted
pheromones.add(Trail(self.pos, self.mode)) # + pg.Vector2(-5, 0).rotate(self.ang) # self.groups()[0]
self.last_phero = pg.Vector2(self.rect.center)
if self.mode == 1:
if mid_result == (2,150,2): # if food
self.desireDir += pg.Vector2(-1,0).rotate(self.ang).normalize()
self.mode = 2
elif mid_result[1] > max(left_result[1], right_result[1]) and (mid_result[0],mid_result[2]) == (0,0):
self.desireDir += pg.Vector2(1,0).rotate(self.ang).normalize()
wandrStr = 0
elif left_result[1] > right_result[1] and (left_result[0],left_result[2]) == (0,0):
self.desireDir += pg.Vector2(1,-2).rotate(self.ang).normalize() #left (0,-1)
wandrStr = 0
elif right_result[1] > left_result[1] and (right_result[0],right_result[2]) == (0,0):
self.desireDir += pg.Vector2(1,2).rotate(self.ang).normalize() #right (0, 1)
wandrStr = 0
elif self.mode == 2:
if self.pos.distance_to(self.nest) < 32:
self.desireDir += pg.Vector2(-1,0).rotate(self.ang).normalize()
self.mode = 1
elif mid_result[2] > max(left_result[2], right_result[2]) and mid_result[:2] == (0,0):
self.desireDir += pg.Vector2(1,0).rotate(self.ang).normalize()
wandrStr = 0
elif left_result[2] > right_result[2] and left_result[:2] == (0,0):
self.desireDir += pg.Vector2(1,-2).rotate(self.ang).normalize() #left (0,-1)
wandrStr = 0
elif right_result[2] > left_result[2] and right_result[:2] == (0,0):
self.desireDir += pg.Vector2(1,2).rotate(self.ang).normalize() #right (0, 1)
wandrStr = 0
else:
self.desireDir += pg.Vector2(self.nest - self.pos).normalize() * .5
wandrStr = .1 #pg.Vector2(self.desireDir + (1,0)).rotate(pg.math.Vector2.as_polar(self.nest - self.pos)[1])
elif self.mode == 3:
if mid_result == (2,150,2): # if food
self.desireDir += pg.Vector2(-1,0).rotate(self.ang).normalize()
self.mode = 2
elif mid_result[1] > max(left_result[1], right_result[1]) and (mid_result[0],mid_result[2]) == (0,0):
self.desireDir += pg.Vector2(1,0).rotate(self.ang).normalize()
wandrStr = 0
elif left_result[1] > right_result[1] and (left_result[0],left_result[2]) == (0,0):
self.desireDir += pg.Vector2(1,-2).rotate(self.ang).normalize() #left (0,-1)
wandrStr = 0
elif right_result[1] > left_result[1] and (right_result[0],right_result[2]) == (0,0):
self.desireDir += pg.Vector2(1,2).rotate(self.ang).normalize() #right (0, 1)
wandrStr = 0
if self.pos.distance_to(self.nest) < 32:
self.desireDir += pg.Vector2(-1,0).rotate(self.ang).normalize()
self.mode = 1
wallColor = (50,50,50) # avoid walls of this color
if left_result == wallColor:
self.desireDir += pg.Vector2(0,1).rotate(self.ang).normalize()
wandrStr = .1
steerStr = 4
if self.mode == 1 : self.mode = 3
elif right_result == wallColor:
self.desireDir += pg.Vector2(0,-1).rotate(self.ang).normalize()
wandrStr = .1
steerStr = 4
if self.mode == 1 : self.mode = 3
elif mid_result == wallColor:
self.desireDir += pg.Vector2(-1,0).rotate(self.ang).normalize()
wandrStr = .1
steerStr = 4
if self.mode == 1 : self.mode = 3
# Avoid edges
if not self.drawSurf.get_rect().collidepoint(left_sensr2) and self.drawSurf.get_rect().collidepoint(right_sensr2):
self.desireDir += pg.Vector2(0,1).rotate(self.ang)
wandrStr = 0
steerStr = 4
elif not self.drawSurf.get_rect().collidepoint(right_sensr2) and self.drawSurf.get_rect().collidepoint(left_sensr2):
self.desireDir += pg.Vector2(0,-1).rotate(self.ang)
wandrStr = 0
steerStr = 4
elif not self.drawSurf.get_rect().collidepoint(Vec2.vint(self.pos + pg.Vector2(21, 0).rotate(self.ang))):
self.desireDir += pg.Vector2(-1,0).rotate(self.ang)
wandrStr = 0
steerStr = 5
'''
margin = 42
if min(self.pos.x, self.pos.y, curW - self.pos.x, curH - self.pos.y) < margin:
if self.pos.x < margin : self.desireDir = pg.Vector2(self.desireDir + (1,0)).normalize()
elif self.pos.x > curW - margin : self.desireDir = pg.Vector2(self.desireDir + (-1,0)).normalize()
if self.pos.y < margin : self.desireDir = pg.Vector2(self.desireDir + (0,1)).normalize()
elif self.pos.y > curH - margin : self.desireDir = pg.Vector2(self.desireDir + (0,-1)).normalize()
if self.mode == 1 : self.mode = 3
'''
randDir = pg.Vector2(cos(radians(randAng)),sin(radians(randAng)))
self.desireDir = pg.Vector2(self.desireDir + randDir * wandrStr).normalize()
dzVel = self.desireDir * maxSpeed
dzStrFrc = (dzVel - self.vel) * steerStr
accel = dzStrFrc if pg.Vector2(dzStrFrc).magnitude() <= steerStr else pg.Vector2(dzStrFrc.normalize() * steerStr)
velo = self.vel + accel * dt
self.vel = velo if pg.Vector2(velo).magnitude() <= maxSpeed else pg.Vector2(velo.normalize() * maxSpeed)
self.pos += self.vel * dt
self.ang = degrees(atan2(self.vel[1],self.vel[0]))
# adjusts angle of img to match heading
self.image = pg.transform.rotate(self.orig_img, -self.ang)
self.rect = self.image.get_rect(center=self.rect.center) # recentering fix
# actually update position
self.rect.center = self.pos
class Vec2():
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def vint(self):
return (int(self.x), int(self.y))
class Trail(pg.sprite.Sprite):
def __init__(self, pos, phero_type):
super().__init__()
self.type = phero_type
self.image = pg.Surface((8, 8))
self.image.fill(0)
self.image.set_colorkey(0)
self.rect = self.image.get_rect(center=pos)
self.str = 420
# maybe if ontop of same color, add color to self, using surface.get_at()
def update(self, dt):
self.str -= ((dt/10)*FPS) * (60/FPS)
if self.str < 0:
return self.kill()
evap = self.str/420
self.image.fill(0)
if self.type == 1 : pg.draw.circle(self.image, [0, 0, 90*evap+10], [4, 4], 4)
if self.type == 2 : pg.draw.circle(self.image, [0, 90*evap+10, 0], [4, 4], 4)
class Food(pg.sprite.Sprite):
def __init__(self, pos):
super().__init__()
self.pos = pos
self.image = pg.Surface((16, 16))
self.image.fill(0)
self.image.set_colorkey(0)
pg.draw.circle(self.image, [2,150,2], [8, 8], 4)
self.rect = self.image.get_rect(center=pos)
def pickup(self):
self.kill()
pheromones = pg.sprite.Group()
def main():
pg.init() # prepare window
pg.display.set_caption("NAnts")
try: pg.display.set_icon(pg.img.load("nants.png"))
except: print("FYI: nants.png icon not found, skipping..")
# setup fullscreen or window mode
if FLLSCRN: #screen = pg.display.set_mode((0,0), pg.FULLSCREEN)
currentRez = (pg.display.Info().current_w, pg.display.Info().current_h)
screen = pg.display.set_mode(currentRez, pg.SCALED) # pg.FULLSCREEN | #pg.display.toggle_fullscreen()
#pg.mouse.set_visible(False)
else: screen = pg.display.set_mode((WIDTH, HEIGHT), pg.RESIZABLE)# | pg.DOUBLEBUF)
cur_w, cur_h = screen.get_size()
nest = (cur_w/3, cur_h/2)
#background = pg.img.load("background.png").convert_alpha()
workers = pg.sprite.Group()
for n in range(ANTS):
workers.add(Ant(screen, nest))
foodList = []
foods = pg.sprite.Group()
clock = pg.time.Clock()
fpsChecker = 0
# main loop
while True:
for e in pg.event.get():
if e.type == pg.QUIT or e.type == pg.KEYDOWN and e.key == pg.K_ESCAPE:
return
elif e.type == pg.MOUSEBUTTONDOWN:
if e.button == 1:
foodBits = 100
fRadius = 32
mousepos = pg.mouse.get_pos()
for i in range(0, foodBits):
dist = pow(i / (foodBits - 1.0), 0.5) * fRadius # (i / (foodBits - 1.0)**.5 #sqrt(i / (foodBits - 1.0))
angle = 2 * pi * 0.618033 * i
#angle = 3 * 2 * pi * (i / foodBits) ** 0.5
#dist = 100 * (i / foodBits) ** 0.5
fx = mousepos[0] + dist * cos(angle)
fy = mousepos[1] + dist * sin(angle)
foods.add(Food((fx,fy)))
foodList.extend(foods.sprites())
if e.button == 3:
mousepos = pg.mouse.get_pos()
for fbit in foodList:
if pg.Vector2(fbit.rect.center).distance_to(mousepos) < fRadius:
fbit.pickup()
foodList = foods.sprites()
dt = clock.tick(FPS) / 100
#screen.fill(0) # enable this to show sensor debug spots
pheromones.update(dt)
workers.update(dt)
screen.fill(0) # fill MUST be after sensors update, so previous draw is visible to them
pheromones.draw(screen)
foods.draw(screen)
pg.draw.circle(screen, [30,10,10], (nest[0],nest[1]+6), 6, 3)
pg.draw.circle(screen, [40,20,20], (nest[0],nest[1]+4), 9, 4)
pg.draw.circle(screen, [50,30,30], (nest[0],nest[1]+2), 12, 4)
pg.draw.circle(screen, [60,40,40], nest, 16, 5)
#pg.draw.rect(screen, (50,50,50), [1000, 300, 42, 400])
workers.draw(screen)
pg.display.update()
fpsChecker+=1 #fpsChecker = 0 # must go before main loop
if fpsChecker>=FPS: # quick debug to see fps in terminal
print(round(clock.get_fps(),2))
#print((dt/10)*FPS)
fpsChecker=0
if __name__ == '__main__':
main() # by Nik
pg.quit()