Как обновить sh моего моделирования до его начала? - PullRequest
1 голос
/ 10 апреля 2020

Я создал симуляцию коронавируса и хотел, чтобы каждый раз, когда я нажимал на экран, все сначала sh обновлялось. Я был бы очень признателен за вашу помощь, если бы вы могли мне помочь. Спасибо!

Мой код:

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)

П / с. Это то, что я пытался до сих пор, чтобы код обновил sh:

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)

Однако, он только обновляет sh гистограммы, а не круги. Следует поменять sh, чтобы в начале был 1 красный зараженный круг, остальные 24 круга - белые и здоровые.

1 Ответ

1 голос
/ 10 апреля 2020

Определить функцию 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()
...