-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolony_game.py
184 lines (134 loc) · 4.47 KB
/
colony_game.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
from ant import Ant
import sys
import world as w
import numpy as np
from area import CircArea, CircAreaGranular
import pygame
import time
t1 = time.time()
t=0
ants=[Ant(np.array([w.W/2,w.H/2]),10,2*np.pi/100*i,1,t) for i in range(0,500)]
pygame.init()
clock = pygame.time.Clock()
gameDisplay = pygame.display.set_mode((w.W,w.H))
col = (0,255,0)
green = (0,255,0)
red = (255,0,0)
Lmax = 6000
home = CircArea(green,np.array([w.W/2,w.H/2]),10)
food_granular1 = CircAreaGranular(red,[20,20],20,w.N,w.M)
food_granular1.create()
food_granular2 = CircAreaGranular(red,[w.W-20,20],20,w.N,w.M)
food_granular2.create()
food_granular3 = CircAreaGranular(red,[w.W-20,w.H-20],20,w.N,w.M)
food_granular3.create()
food_granular4 = CircAreaGranular(red,[20,w.H-20],20,w.N,w.M)
food_granular4.create()
#obstacle=np.zeros()
def draw():
x,y=pygame.mouse.get_pos()
state = pygame.mouse.get_pressed(num_buttons=3)
if state[0]==True:
print(x,y)
arr[x,y]=1
x0,y0 = pygame.mouse.get_pos()
rect = pygame.Rect(x,y,5,5)
pygame.draw.rect(gameDisplay,(255,0,0),rect)
trail_scout=np.zeros((w.N,w.M))
trail_return =np.zeros((w.N,w.M))
def plot_rect_ant(ant):
if ant.scout==True:
c =(0,0,255)
else:
c = red
rect = pygame.Rect(ant.r[0],ant.r[1],1,1)
pygame.draw.rect(gameDisplay,c,rect)
def plot_rect2(arr,Lmax):
if len(arr)>0:
els = arr[-1]
for el in els:
r,m = el
if m == True:
c = green
else:
c = red
rect = pygame.Rect(r[0],r[1],1,1)
pygame.draw.rect(gameDisplay,c,rect)
def plot_trail(trail1,trail2):
N,M = trail1.shape
arr = np.zeros((N,M,3))
if trail1.max()!=0:
arr[:,:,0]=trail1/trail1.max()*2550
else:
arr[:,:,0] = trail1
if trail2.max()!=0:
arr[:,:,2]=trail2/trail2.max()*2550
else:
arr[:,:,2]=trail2
arr=arr.astype('int')
pygame.surfarray.blit_array(gameDisplay,arr)
return arr
count_home = 0
pause = False
while 1:
#gameDisplay.fill((0,0,0))
#food.drag(pygame)
#home.drag(pygame)
#draw()
if pause==False:
food_granular1.plot(pygame,gameDisplay)
food_granular2.plot(pygame,gameDisplay)
food_granular3.plot(pygame,gameDisplay)
food_granular4.plot(pygame,gameDisplay)
arr = plot_trail(trail_scout,trail_return)
for ant in ants:
#plot_rect_ant(ant)
if (food_granular1.inside(ant.r) or food_granular2.inside(ant.r) or food_granular3.inside(ant.r) or food_granular4.inside(ant.r))and ant.scout==True:
ant.scout = False
ant.dr = -ant.dr
ant.t = t
col = red
if home.inside(ant.r) and ant.scout==False:
count_home += 1
print('home {}'.format(count_home))
ant.dr = -ant.dr
ant.scout = True
col =green
ant.t = t
oldpos = ant.r
if ant.scout == True:
ant.mark_trail(trail_scout)
ant.bite(food_granular1.arr)
ant.bite(food_granular2.arr)
ant.bite(food_granular3.arr)
ant.bite(food_granular4.arr)
ant.move()
ant.decide(trail_return)
else:
ant.mark_trail(trail_return)
ant.bite(food_granular1.arr)
ant.bite(food_granular2.arr)
ant.bite(food_granular3.arr)
ant.bite(food_granular4.arr)
ant.move()
ant.decide(trail_scout)
w.evap(trail_scout, Lmax,t)
w.evap(trail_return, Lmax,t)
trail_scout=w.diffuse(trail_scout)
trail_return=w.diffuse(trail_return)
t += 1
if t%100==0:
print(t)
#pygame.draw.circle(gameDisplay,red,tuple(food.r0),food.R)
pygame.draw.circle(gameDisplay,green,tuple(home.r0),home.R)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
t = 0
ants=[Ant(np.array([w.W/2,w.H/2]),10,2*np.pi/100*i,1,t) for i in range(0,100)]
pause = not(pause)
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
#clock.tick(120)