Benutzer-Werkzeuge

Webseiten-Werkzeuge


Seitenleiste

ws2021:code4

Dies ist eine alte Version des Dokuments!


<code> import turtle import random import time import threading from Agent 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 = [] anzahl_agenten = 100 for _ in range(anzahl_agenten): #range(anzahl der agents)

  agents.append(Agent(_)) #create agents

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("green")
  agent.ball.penup() #not draw a line
  agent.ball.speed(0) #Geschwindigkeitsänderung
  #random xcor and ycor for each ball
  x = random.randint(-p,p)
  y = random.randint(-p,p)
  agent.ball.goto(x,y)
  #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)

agents[1].setContagious() #first ball will be colored red agents[1].days = 801 # is past „infected“ #infizierten = [1] #numbers as the positions in the list of balls, which are colored red #recovered = []

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
              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
              # 0.9 = Wahrscheinlichkeit einer Infektion, Inkubation und Ansteckung
              if (agents[i].isContagious() and agents[j].isSusceptible() and random.random() < 0.9):
                  agents[j].setInfected()
              if(agents[i].isSusceptible() and agents[j].isContagious() and random.random() < 0.9):
                  agents[i].setInfected()
  # Krankheitsdays hochzaehlen
  for k in range(0,len(agents)):
      if(agents[k].days > 0):
          agents[k].days += 1
          if agents[k].days == 800:
              agents[k].setContagious()
          elif agents[k].days == 2800:
              agents[k].setRecovered()

wn.mainloop()

ws2021/code4.1611243898.txt.gz · Zuletzt geändert: 2021/01/21 16:44 von n.gujejiani