Benutzer-Werkzeuge

Webseiten-Werkzeuge


Seitenleiste

ws2021:code6
import turtle
import random
from klasse_agenten import Agent
 
 
p = 350 #pixels
agentenradius = 15
 
#draw the square for the  bouncing balls
turtle.pu()
turtle.speed(0)
turtle.goto(p+agentenradius/2,p+agentenradius/2)
turtle.pd()
turtle.setheading(0)
for i in range(4):
    turtle.rt(90)
    turtle.fd(2*p+agentenradius)
 
 
wn = turtle.Screen()
wn.setup(p*2+agentenradius,p*2+agentenradius)
wn.bgcolor("white") #Screen background color = white
wn.title("Agents Simulator")
wn.tracer(0) #stops the screen from updating
 
 
agents = []
for _ in range(20): #range(anzahl der agents)
    agents.append(Agent(_)) #create agents
 
id_counter = 0
moving_agents = 3 #jedes 3. Agent bekommt eine Geschwindigkeit
for agent in agents:
    agent.ball.shape("circle")
    agent.ball.shapesize(agentenradius/20,agentenradius/20) #Radius von circles ## turtle.shapesize(x,y) vergrößert/verkleinert den Shape x-mal in x Richtung und y-mal in y Richtung
    agent.ball.color("medium sea green")
    agent.ball.penup() #not draw a line
    agent.ball.speed(0) #Geschwindigkeitsänderung
    agent.ball.id = id_counter
 
    #random xcor and ycor for each ball
    x = random.randint(-p,p)
    y = random.randint(-p,p)
    agent.ball.goto(x,y)
 
    if id_counter % moving_agents == 0:
        #random ycor speed between -3 and 3, no 0
        agent.ball.dy = random.randint(-1,1)
        while agent.ball.dy == 0:
            agent.ball.dy = random.randint(-1,1)
 
        #random xcor speed between -3 and 3, no 0
        agent.ball.dx = random.randint(-1,1)
        while agent.ball.dx == 0:
            agent.ball.dx = random.randint(-1,1)
    else:
        agent.ball.dx = 0
        agent.ball.dy = 0
 
    id_counter += 1
 
 
agents[0].setContagious() #first ball will be colored red
agents[0].steps = 801 # is past "infected"
 
#Anfangswerte von Agenten
susceptible_counter = 19
infected_counter = 0
contagious_counter = 1
recovered_counter = 0
 
steps_counter = 0
 
susceptible_list = [susceptible_counter]
infected_list = [infected_counter]
contagious_list = [contagious_counter]
recovered_list = [recovered_counter]
 
 
while True:
    wn.update() #the movements are smoother
 
    for agent in agents:
 
        agent.ball.sety(agent.ball.ycor() + agent.ball.dy) #sets the balls ycor to (the current ycor plus the change in y)
        agent.ball.setx(agent.ball.xcor() + agent.ball.dx)
 
        #check for a bounce (floor or bottom collision)
        if agent.ball.ycor() < -p:
            agent.ball.sety(-p)
            agent.ball.dy *= -1
        if agent.ball.ycor() > p:
            agent.ball.sety(p)
            agent.ball.dy *= -1
 
        #check for a wall collision
        if agent.ball.xcor() < -p:
            agent.ball.setx(-p)
            agent.ball.dx *= -1
        if agent.ball.xcor() > p:
            agent.ball.setx(p)
            agent.ball.dx *= -1
 
    #check for collisions between the balls
    for i in range(0,len(agents)):
        for j in range(i+1, len(agents)):
            #check for a collision
            if agents[i].ball.distance(agents[j].ball) < agentenradius:
 
 
                #switch the behaviours of these two balls
                if agents[i].id % moving_agents == 0 and agents[j].id % moving_agents == 0:
                     agents[i].ball.dx , agents[j].ball.dx = agents[j].ball.dx, agents[i].ball.dx
                     agents[i].ball.dy , agents[j].ball.dy = agents[j].ball.dy, agents[i].ball.dy
                if agents[i].id % moving_agents == 0 and agents[j].id % moving_agents != 0:
                    agents[i].ball.dx *= -1
                    #agents[i].ball.dy *= -1
                if agents[j].id % moving_agents == 0 and agents[i].id % moving_agents != 0:
                    agents[j].ball.dx *= -1
                    #agents[j].ball.dy *= -1
 
 
                # 0.9 = Wahrscheinlichkeit einer Infektion, Inkubation und Ansteckung
                if (agents[i].isContagious() and agents[j].isSusceptible() and random.random() < 0.2):
                    agents[j].setInfected()
                    susceptible_counter -= 1
                    infected_counter += 1
 
 
 
                if(agents[i].isSusceptible() and agents[j].isContagious() and random.random() < 0.2):
                    agents[i].setInfected()
                    susceptible_counter -= 1
                    infected_counter += 1
    steps_counter += 1
 
    # Krankheitssteps hochzaehlen
    for k in range(0,len(agents)):
        if(agents[k].steps > 0):
            agents[k].steps += 1
 
            if agents[k].steps == 800:
                agents[k].setContagious()
                infected_counter -= 1
                contagious_counter += 1
            elif agents[k].steps == 2800:
                agents[k].setRecovered()
                contagious_counter -= 1
                recovered_counter += 1
 
 
    if steps_counter % 200 == 0: #Listen mit Anzahl der Agenten in den bestimmten Zustaenden werden erstellt
        susceptible_list.append(susceptible_counter)
        infected_list.append(infected_counter)
        contagious_list.append(contagious_counter)
        recovered_list.append(recovered_counter)
 
 
    if contagious_counter == 0 and infected_counter == 0: #Programm stoppt, wenn es keine infizierten und keine ansteckenden Agenten mehr gibt
        break
 
wn.mainloop()
ws2021/code6.txt · Zuletzt geändert: 2021/02/03 18:33 von sarah-j-28