# How to refresh my simulation to its beginning?

**URL:** <https://discourse.processing.org/t/how-to-refresh-my-simulation-to-its-beginning/19604>\
**Category:** Processing.py\
**Tags:** homework\
**Created:** [April 10, 2020, 4:01am UTC](https://discourse.processing.org/t/how-to-refresh-my-simulation-to-its-beginning/19604 "2020-04-10T04:01:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Upchanges](https://avatars.discourse-cdn.com/v4/letter/u/839c29/32.png) [@Upchanges](https://discourse.processing.org/u/Upchanges)\
**Post date:** [April 10, 2020, 4:01am UTC](https://discourse.processing.org/t/how-to-refresh-my-simulation-to-its-beginning/19604/1 "2020-04-10T04:01:57Z")

</div>

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:

```auto
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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [April 10, 2020, 5:19am UTC](https://discourse.processing.org/t/how-to-refresh-my-simulation-to-its-beginning/19604/2 "2020-04-10T05:19:54Z")

</div>

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

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

def mouseClicked():
    setup()

```

---

<div class="post-metadata">

**Author:** ![Upchanges](https://avatars.discourse-cdn.com/v4/letter/u/839c29/32.png) [@Upchanges](https://discourse.processing.org/u/Upchanges)\
**Post date:** [April 10, 2020, 5:22am UTC](https://discourse.processing.org/t/how-to-refresh-my-simulation-to-its-beginning/19604/3 "2020-04-10T05:22:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [April 10, 2020, 5:35am UTC](https://discourse.processing.org/t/how-to-refresh-my-simulation-to-its-beginning/19604/4 "2020-04-10T05:35:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Upchanges](https://avatars.discourse-cdn.com/v4/letter/u/839c29/32.png) [@Upchanges](https://discourse.processing.org/u/Upchanges)\
**Post date:** [April 10, 2020, 5:48am UTC](https://discourse.processing.org/t/how-to-refresh-my-simulation-to-its-beginning/19604/5 "2020-04-10T05:48:13Z")

</div>

Yes, your solution worked! Thank you!!

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [April 10, 2020, 5:05pm UTC](https://discourse.processing.org/t/how-to-refresh-my-simulation-to-its-beginning/19604/6 "2020-04-10T17:05:35Z")

</div>

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:

> <https://stackoverflow.com/questions/61133792/how-to-refresh-my-simulation-to-its-beginning>

---

<div class="post-metadata">

**Author:** ![Upchanges](https://avatars.discourse-cdn.com/v4/letter/u/839c29/32.png) [@Upchanges](https://discourse.processing.org/u/Upchanges)\
**Post date:** [April 11, 2020, 1:09am UTC](https://discourse.processing.org/t/how-to-refresh-my-simulation-to-its-beginning/19604/7 "2020-04-11T01:09:12Z")

</div>

Yep! Sorry! I will do!
