Repository navigation
Expand file tree
/
Copy pathSlime5.py
More file actions
302 lines (262 loc) · 9.13 KB
/
Copy pathSlime5.py
File metadata and controls
302 lines (262 loc) · 9.13 KB
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
# -*- coding: utf-8 -*-
"""
Created on Wed May 03 14:03:42 2017
@author: Fantasia
"""
import random
import pygame
import math
class Unit:
def __init__(self, c, state, c2, life):
self.c = c # 信息素浓度
self.c2 = c2
self.s = state # 状态:0-空位 1-生长细胞 2-物体 3-捕食细胞
self.l = life # 生命值
class Ground:
def __init__(self, n):
self.ground = []
self.n = n
self.l = []
for i in range(n):
self.ground.append([])
for l in self.ground:
for i in range(n):
l.append(Unit(0, 0, 0, 0))
def show(self):
for i in range(self.n):
print([u.s for u in self.ground[i]])
def randinit(self):
for i in range(self.n):
for j in range(self.n):
self.ground[i][j] = Unit(random.random(), 0, 0, 0)
def addslime(self, x, y):
self.ground[x][y].s = 1
self.ground[x][y].l = 50 # 满100
def addglass(self, x, y):
self.ground[x][y].s = 2
self.ground[x][y].l = 15 # 护甲会降解
def addpredator(self, x, y):
self.ground[x][y].s = 3
self.ground[x][y].l = 100
def inrange(self, x, y):
return (x in range(n)) and (y in range(n))
def addc(self, x, y, n):
sc = self.ground[x][y].c
# if sc<10000:
self.ground[x][y].c = sc+n
def addc2(self, x, y, n):
sc2 = self.ground[x][y].c2
self.ground[x][y].c2 = sc2+n
def release(self, x, y):
sc = self.ground[x][y].c
k = 0.01
try:
dc = k*sc/self.abalcount(x, y)
except:
dc = 0.0
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
if self.inrange(x+i, y+j):
if not (i == j and i == 0):
self.addc(x+i, y+j, dc)
def nrelease(self, x, y):
sc = self.ground[x][y].c # 根据要捕食的细胞/自己的信息素浓度施放自己的信息素
k = 0.9
try:
dc = k*sc/self.abalcount(x, y)
except:
dc = 0.0
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
if self.inrange(x+i, y+j):
if not (i == j and i == 0):
self.addc2((x+i), (y+j), dc)
def move(self, x, y, nx, ny):
thes = self.ground[x][y].s
thel = self.ground[x][y].l
self.ground[x][y].s = 0
x = nx
y = ny
self.ground[x][y].s = thes
self.ground[x][y].l = thel
def die(self, x, y):
self.ground[x][y].s = 0
self.ground[x][y].l = 0
def upperslime(self, x, y):
nx = x
ny = y
myc = self.ground[x][y].c
myc2 = self.ground[x][y].c2
k2 = 7
totalmyc = myc-k2*myc2
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
if not (i == j and i == 0):
if self.inrange(x+i, y+j):
nc = self.ground[x+i][y+j].c
nc2 = self.ground[x+i][y+j].c2
if self.ground[x+i][y+j].s != 0:
pass
elif self.ground[x+i][y+j].s == 0:
if self.ground[x][y].l >= 100:
if self.ground[x+i][y+j].c2 > 0.0001: # 如果信息素c2浓度超过就产生护甲
self.addglass(x+i, y+j)
else:
self.addslime(x+i, y+j)
self.ground[x][y].l = 50
else:
if nc-k2*nc2 <= totalmyc:
pass
elif nc-k2*nc2 > totalmyc:
myc = self.ground[x+i][y+j].c
myc2 = self.ground[x+i][y+j].c2
totalmyc = myc-k2*myc2
nx = x+i
ny = y+j
self.move(x, y, nx, ny)
self.ground[nx][ny].l += 1.5
if self.ground[nx][ny].l >= 100:
self.ground[nx][ny].l = 100
self.release(nx, ny)
def upperpredator(self, x, y):
nx = x
ny = y
mc = self.ground[x][y].c # 按照自己/对方的信息素浓度梯度移动
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
if not (i == j and i == 0):
if self.inrange(x+i, y+j):
if self.ground[x+i][y+j].s == 1:
self.ground[x+i][y+j].s = 3
self.ground[x+i][y+j].l = 100
elif self.ground[x+i][y+j].s == 0:
if self.ground[x+i][y+j].c <= mc:
pass
else:
mc = self.ground[x+i][y+j].c
nx = x+i
ny = y+j
self.move(x, y, nx, ny)
self.nrelease(nx, ny)
self.ground[nx][ny].l -= 1
if self.ground[nx][ny].l <= 0:
self.die(nx, ny)
def upperglass(self, x, y):
if self.ground[x][y].l <= 0:
self.ground[x][y].l = 0
self.ground[x][y].s = 0
else:
self.ground[x][y].l -= 1
def z(self, x, y):
return x, y-1
def y(self, x, y):
if y != self.n-1:
return x, y+1
else:
return x, 0
def s(self, x, y):
return x-1, y
def x(self, x, y):
if x != self.n-1:
return x+1, y
else:
return 0, y
def zs(self, x, y):
return self.s(*self.z(x, y))
def zx(self, x, y):
return self.x(*self.z(x, y))
def ys(self, x, y):
return self.s(*self.y(x, y))
def yx(self, x, y):
return self.x(*self.y(x, y))
fl = [z, y, s, x, zs, zx, ys, yx]
def abalcount(self, x, y):
# count=0
# for i in [-1,0,1]:
# for j in [-1,0,1]:
# if self.inrange(x+i,y+j):
# if self.ground[x+i][y+j].s!=2:
# count+=1
# return count
return 8
def kuosan1(self, x, y):
rate = 0.9
deltaout = self.ground[x][y].c*rate
deltain = 0
for f in self.fl:
tx, ty = f(self, x, y)
try:
d = self.ground[tx][ty].c*rate/self.abalcount(tx, ty)
except:
d = 0.0
deltain += d
self.ground[x][y].c += deltain
self.ground[x][y].c -= deltaout
def kuosan2(self, x, y):
rate = 0.9
deltaout = self.ground[x][y].c2*rate
deltain = 0
for f in self.fl:
tx, ty = f(self, x, y)
try:
d = self.ground[tx][ty].c2*rate/self.abalcount(tx, ty)
except:
d = 0.0
deltain += d
self.ground[x][y].c2 += deltain
self.ground[x][y].c2 -= deltaout
def decoposeC(self, x, y): # 信息素c的降解
c = self.ground[x][y].c
k = 0.0000000001
self.ground[x][y].c -= k*c
def decomposeC2(self, x, y): # c2的降解
c2 = self.ground[x][y].c2
k = 0.1
self.ground[x][y].c2 -= k*c2
def uppergroundunit(self, x, y):
self.kuosan1(x, y)
self.kuosan2(x, y)
if self.ground[x][y].s == 1:
self.upperslime(x, y)
elif self.ground[x][y].s == 3:
self.upperpredator(x, y)
elif self.ground[x][y].s == 2:
self.upperglass(x, y)
self.decomposeC2(x, y)
self.decoposeC(x, y)
def update(self):
for i in range(self.n):
for j in range(self.n):
self.uppergroundunit(i, j)
n = 170
g = Ground(n)
g.randinit()
for i in range(500):
g.addslime(random.randint(0, g.n-1), random.randint(0, g.n-1))
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
YELLOW = (0, 255, 0)
RED = (255, 0, 0)
ge = 6
g.addpredator(random.randint(0, g.n-1), random.randint(0, g.n-1))
def drawone(i, j, c):
if c.ground[i][j].s == 0:
pygame.draw.rect(dissurf, BLACK, (i*ge, j*ge, ge-2, ge-2))
elif c.ground[i][j].s == 1:
pygame.draw.rect(dissurf, YELLOW, (i*ge, j*ge, ge-2, ge-2))
elif c.ground[i][j].s == 2:
pygame.draw.rect(dissurf, BLUE, (i*ge, j*ge, ge-2, ge-2))
elif c.ground[i][j].s == 3:
pygame.draw.rect(dissurf, RED, (i*ge, j*ge, ge-2, ge-2))
pygame.init()
dissurf = pygame.display.set_mode((n*ge, n*ge))
dissurf.fill(BLACK)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
g.update()
for i in range(n):
for j in range(n):
drawone(i, j, g)
pygame.display.update()