Benutzer-Werkzeuge

Webseiten-Werkzeuge


Seitenleiste

ws2021:code5

Dies ist eine alte Version des Dokuments!


<code python> 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 = [] for _ in range(50): #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].days = 801 # is past „infected“

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.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/code5.1611661645.txt.gz · Zuletzt geändert: 2021/01/26 12:47 von sarah-j-28