Benutzer-Werkzeuge

Webseiten-Werkzeuge


Seitenleiste

ws2021:code

Dies ist eine alte Version des Dokuments!


#Bouncing Vall Simulator import turtle import random 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

#balls = [] agents = []

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

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

#colors = [„red“,„green“,„black“]

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)
  #ball.da = 1

agents[1].anstecken() #first ball will be colored red ball_no = [1] #numbers as the positions in the list of balls, which are colored red

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
              #hier fehlt eine Bedingung, um das ineinanderstecken zu vermeiden?
              ##neu random dx und dy definieren ?
              if agents[i].ball.dx == agents[j].ball.dx:
                  if agents[i].ball.dy == agents[j].ball.dy:
                      agents[i].ball.dx == random.randint(-1,1)
                      agents[i].ball.dy == random.randint(-1,1)
                      agents[j].ball.dx == random.randint(-1,1)
                      agents[j].ball.dy == random.randint(-1,1)
              if i in ball_no:
                  agents[j].ball.color("red")
                  if j not in ball_no:
                      ball_no.append(j)
              if j in ball_no:
                  agents[i].ball.color("red")
                  if i not in ball_no:
                      ball_no.append(i)

wn.mainloop()

# To fix: they get stuck together, maybe because they have similar behaviurs and go to the direction of each other over and over

ws2021/code.1608216351.txt.gz · Zuletzt geändert: 2020/12/17 15:45 von livia.mai