如何将我的模拟刷新到开始?
How to refresh my simulation to its beginning?
我创建了一个冠状病毒模拟,每次单击屏幕时我都想将所有内容刷新到开头。如果您能帮助我,我将不胜感激。谢谢!
我的代码:
x= [ ]
y= [ ]
h= [False, True] #False=> infected
infected=1
healthy=24
runs=0
def setup():
size(500,500)
#Setting up the ... random coordinates
for i in range(25):
x.append(random(0,500))
y.append(random(0,500))
h.append(True) #All healthy, h is health
textSize(12);
def distance(x1, x2, y1, y2):
a=(x1-x2)
b=(y1-y2)
c= sqrt(a**2 + b**2)
return c
def draw():
global x, y, infected, healthy
background(255)
#Drawing the individuals
for individuals in range(len(x)):
strokeWeight(2)
if h[individuals] == True:
fill(255) #healthy
else:
fill(255,0,0) #infected
circle(x[individuals], y[individuals], 40)
#calulate the distance to each neighbors
for neighbors in range(len(x)):
if neighbors == individuals:
continue
d = distance(x[individuals], x[neighbors], y[individuals], y[neighbors])
if d < 40 and (h[neighbors] == False or h[individuals]==False) and (h[individuals] == True or h[neighbors] == True):
#infection happens
h[individuals] = False
h[neighbors] = False
infected = infected + 1
healthy = healthy -1
if healthy <0:
healthy=0
if infected >25:
infected=25 #Need to put infected, healthy stuff in another if statement
for m in range(len(x)):
x[m]= x[m] + random(-10,10)
y[m]= y[m] + random(-10,10)
if x[m] > 500:
x[m] = 500
if y[m] > 500:
y[m] = 500
if x[m] < 0:
x[m] = 0
if y[m] < 0:
y[m] = 0
barGraph()
delay(100)
def barGraph():
global infected, healthy, runs
strokeWeight(1)
fill(255,0,0)
rect(60, 10, infected, 10) #(x coordinate, y coordinate, width, height)
fill(3,3,3)
text('Infected', 10,20)
text(infected, 180,20)
strokeWeight(1)
fill(255)
rect(60, 30, healthy, 10)
fill(3,3,3)
text('Healthy', 10,40)
text(healthy, 180, 35)
text('Click screen to run the simulation again', 10, 60)
text('Iteration # :', 10,80 )
text(runs, 120,80)
P/s。这是我到目前为止一直在尝试刷新的代码:
def mouseClicked():
global infected, healthy, x
infected=1
healthy=24
for individuals in range(len(x)-1):
strokeWeight(2)
if h[individuals] == True:
fill(255) #healthy
circle(x[individuals], y[individuals], 40)
for individuals in range(1):
strokeWeight(2)
fill(255,0,0) #infected
circle(x[individuals], y[individuals], 40)
但是,它只刷新条形图,不刷新圆圈。它应该刷新,以便在开始时,会有 1 个红色感染圆圈,其他 24 个圆圈是白色和健康的。
定义一个函数 initSimulation
该函数必须初始化所有属性并创建列表 x
、y
和 h
:
def initSimulation():
global infected, healthy, x, y, h
infected=1
healthy=24
x = [ ]
y = [ ]
h = [ ]
#Setting up the ... random coordinates
for i in range(25):
x.append(random(0,500))
y.append(random(0,500))
h.append(True) #All healthy, h is health
h[0] = False
调用setup
中的函数,点击鼠标时:
def setup():
size(500,500)
textSize(12);
initSimulation()
def mouseClicked():
initSimulation()
我创建了一个冠状病毒模拟,每次单击屏幕时我都想将所有内容刷新到开头。如果您能帮助我,我将不胜感激。谢谢!
我的代码:
x= [ ]
y= [ ]
h= [False, True] #False=> infected
infected=1
healthy=24
runs=0
def setup():
size(500,500)
#Setting up the ... random coordinates
for i in range(25):
x.append(random(0,500))
y.append(random(0,500))
h.append(True) #All healthy, h is health
textSize(12);
def distance(x1, x2, y1, y2):
a=(x1-x2)
b=(y1-y2)
c= sqrt(a**2 + b**2)
return c
def draw():
global x, y, infected, healthy
background(255)
#Drawing the individuals
for individuals in range(len(x)):
strokeWeight(2)
if h[individuals] == True:
fill(255) #healthy
else:
fill(255,0,0) #infected
circle(x[individuals], y[individuals], 40)
#calulate the distance to each neighbors
for neighbors in range(len(x)):
if neighbors == individuals:
continue
d = distance(x[individuals], x[neighbors], y[individuals], y[neighbors])
if d < 40 and (h[neighbors] == False or h[individuals]==False) and (h[individuals] == True or h[neighbors] == True):
#infection happens
h[individuals] = False
h[neighbors] = False
infected = infected + 1
healthy = healthy -1
if healthy <0:
healthy=0
if infected >25:
infected=25 #Need to put infected, healthy stuff in another if statement
for m in range(len(x)):
x[m]= x[m] + random(-10,10)
y[m]= y[m] + random(-10,10)
if x[m] > 500:
x[m] = 500
if y[m] > 500:
y[m] = 500
if x[m] < 0:
x[m] = 0
if y[m] < 0:
y[m] = 0
barGraph()
delay(100)
def barGraph():
global infected, healthy, runs
strokeWeight(1)
fill(255,0,0)
rect(60, 10, infected, 10) #(x coordinate, y coordinate, width, height)
fill(3,3,3)
text('Infected', 10,20)
text(infected, 180,20)
strokeWeight(1)
fill(255)
rect(60, 30, healthy, 10)
fill(3,3,3)
text('Healthy', 10,40)
text(healthy, 180, 35)
text('Click screen to run the simulation again', 10, 60)
text('Iteration # :', 10,80 )
text(runs, 120,80)
P/s。这是我到目前为止一直在尝试刷新的代码:
def mouseClicked():
global infected, healthy, x
infected=1
healthy=24
for individuals in range(len(x)-1):
strokeWeight(2)
if h[individuals] == True:
fill(255) #healthy
circle(x[individuals], y[individuals], 40)
for individuals in range(1):
strokeWeight(2)
fill(255,0,0) #infected
circle(x[individuals], y[individuals], 40)
但是,它只刷新条形图,不刷新圆圈。它应该刷新,以便在开始时,会有 1 个红色感染圆圈,其他 24 个圆圈是白色和健康的。
定义一个函数 initSimulation
该函数必须初始化所有属性并创建列表 x
、y
和 h
:
def initSimulation():
global infected, healthy, x, y, h
infected=1
healthy=24
x = [ ]
y = [ ]
h = [ ]
#Setting up the ... random coordinates
for i in range(25):
x.append(random(0,500))
y.append(random(0,500))
h.append(True) #All healthy, h is health
h[0] = False
调用setup
中的函数,点击鼠标时:
def setup():
size(500,500)
textSize(12);
initSimulation()
def mouseClicked():
initSimulation()