# How to refresh my simulation to its beginning?

I created a coronavirus simulation and I wanted to refresh everything to the beginning each time I click the screen. I would greatly appreciate your help if you could help me. Thank you!

My code:

``````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. This is what I had been trying so far for the code to refresh:

``````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)
``````

However, it only refresh the bar graphs, not the circles. It should refresh so that in the beginning, there will be 1 red infected circle, the other 24 circles are white and healthy.

1 Like

What if you set up all of your variables in the `setup()` function, then recall `setup()` on `mouseClicked()`?

``````def setup():
size(500,500)
global x, y, h, infected, healthy, runs
x = []
y = []
...

def mouseClicked():
setup()
``````
2 Likes

But then, I can’t use global of those variables I put in setup() for my draw() function…

Global variables are global variables. It doesn’t matter whether you define them above or within the `setup()` function.

Have you tested my solution? It seemed to work for me – i.e. the circles reset to new randomized locations. Perhaps I’ve misunderstood your question.

Yes, your solution worked! Thank you!!

1 Like

It’s okay to post your question to multiple sites, but please link between the posts so we don’t spend time repeating advice you’ve already received.

This question has also been answered here:

3 Likes

Yep! Sorry! I will do!