我正在为一个项目进行仿真,当男性和女性相遇时,需要让细胞繁殖。我不确定将代码放在哪里,也不确定首先需要将其放入数组的位置。我一直坚持下去。还要根据细胞的发育阶段改变细胞的颜色。
import random
import matplotlib.pyplot as plt
import numpy as np
import time
from shrimp import Shrimp
XMAX = 1500
YMAX = 1500
def main():
monkeys = []
for i in range(70):
randX = random.randint(0,XMAX)
randY = random.randint(0,YMAX)
monkeys.append(Shrimp([randX,randY]))
# print(monkeys[i])
for i in range(50):
print("\n ### TIMESTEP ",i, "###")
xvalues = []
yvalues = []
sizes = []
for m in monkeys:
m.stepChange()
#print(m)
xvalues.append(m.pos[0])
yvalues.append(m.pos[1])
sizes.append(m.getSize())
print(m)
plt.scatter(xvalues, yvalues, s=sizes) # Note plt origin is bottom left
plt.xlim(0,XMAX)
plt.ylim(0,YMAX)
plt.show()
time.sleep(0)
if __name__ == "__main__":
main()
import random
YELLOW = (225,225,0)
RED = (0,0,225)
BLACK = (0,0,0)
""""
class Shrimp():
time2hatch = 4
states = ["egg","subadults", "adult","dead"]
deathrate = {"egg": 0.01, "subadult":0.02, "adult":0.02}
def __init__(self, pos,):
self.pos = pos
self.state = self.states[0]
self.age = 0
self.time2hatch = random.randint(4,20)
def __str__(self):
return self.state + " @ " + str(self.pos)
def stepChange(self):
self.age += 1
if self.state == "egg":
self.pos[1] -= 5
if self.age > self.time2hatch:
self.state = "subadults"
else:
xmov = random.randint (-3,3)
ymov = random.randint (-3,3)
self.pos[0] += xmov
self.pos[1] += ymov
if self.state == "subadults":
if self.age > self.time2hatch + 15:
adultsex = ["male", "female"]
self.state = random.choice(adultsex)
#print(self.state)
else:
xmov = random.randint (-10,10)
ymov = random.randint (-10,10)
self.pos[0] += xmov
self.pos[1] += ymov
def getColour(m):
if m.state == "egg":
colour = YELLOW
elif m.state =="subadult":
colour = RED
else:colour = BLACK
return colour
def getSize(m):
if m.state == "egg":
size = 5
elif m.state == "subadults" :
size = 10
elif m.state == "adult":
size = 30
else:
size = 100
return size