Benutzer-Werkzeuge

Webseiten-Werkzeuge


Seitenleiste

ws2021:bouncingballs

Dies ist eine alte Version des Dokuments!


import turtle import random import time import threading from erste_klasse import Agent

#ag1 = Agent() #print(ag1)

p = 400 #pixels

#draw the square for the bouncing balls turtle.pu() turtle.speed(0) turtle.goto(p+10,p+10) turtle.pd() turtle.setheading(0) for i in range(4):

  turtle.rt(90)
  turtle.fd(2*p+20)

wn = turtle.Screen() wn.setup(p*2+20,p*2+20) wn.bgcolor(„white“) #Screen background color = white wn.title(„Bouncing Ball Simulator“) wn.tracer(0) #stops the screen from updating

agents = []

for _ in range(50): #range(anzahl der bälle)

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

for agent in agents:

  agent.ball.shape("circle")
  agent.ball.color("green")
  agent.ball.penup() #not draw a line
  agent.ball.speed(1) #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].anstecken() #first ball will be colored red 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:
      #ball.rt(ball.da) #changing bouncing direction (sonst bleiben die zu zweit stecken)
      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
      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) < 20: # because each ball has 20 pixels width
              #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.5 = Wahrscheinlichkeit einer Infektion
              if i in infizierten and j not in recovered and random.random()<0.9:
                  agents[j].anstecken()
                  if j not in infizierten:
                      infizierten.append(j)
              if j in infizierten and i not in recovered and random.random()<0.9:
                  agents[i].anstecken()
                  if i not in infizierten:
                      infizierten.append(i)
              for k in range(0,len(agents)):
                  if agents[k].tage >= 1 and agents[k].tage < 1000:
                      agents[k].tage += 1
                  if agents[k].tage == 1000:
                      agents[k].recovered()
                      recovered.append(k)
                      print(infizierten,k)
                      infizierten.remove(k)

wn.mainloop()

ws2021/bouncingballs.1610641603.txt.gz · Zuletzt geändert: 2021/01/14 17:26 von livia.mai