Hier werden die Unterschiede zwischen zwei Versionen gezeigt.
Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
ws2021:code [2020/12/17 16:56] n.gujejiani |
ws2021:code [2021/02/03 18:32] (aktuell) sarah-j-28 |
||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
<code Python> | <code Python> | ||
- | #Bouncing Ball Simulator | + | #Bouncing Vall Simulator |
import turtle | import turtle | ||
- | import random | + | import random |
- | from erste_klasse import Agent | + | from klasse_agenten import Agent |
- | #ag1 = Agent() | + | #ag1 = Agent() |
#print(ag1) | #print(ag1) | ||
Zeile 28: | Zeile 28: | ||
- | #balls = [] | ||
agents = [] | agents = [] | ||
- | for _ in range(50): #range(Anzahl der bälle) | + | for _ in range(50): #range(anzahl der bälle) |
agents.append(Agent(_)) #create balls | agents.append(Agent(_)) #create balls | ||
- | |||
- | #colors = ["red","green","black"] | ||
Zeile 40: | Zeile 37: | ||
agent.ball.shape("circle") | agent.ball.shape("circle") | ||
agent.ball.color("green") | agent.ball.color("green") | ||
- | agent.ball.penup() | + | agent.ball.penup() #not draw a line |
agent.ball.speed(1) #Geschwindigkeitsänderung | agent.ball.speed(1) #Geschwindigkeitsänderung | ||
Zeile 58: | Zeile 55: | ||
agent.ball.dx = random.randint(-1,1) | agent.ball.dx = random.randint(-1,1) | ||
- | #ball.da = 1 | ||
agents[1].anstecken() #first ball will be colored red | 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 | + | infizierten = [1] #numbers as the positions in the list of balls, which are colored red |
+ | recovered = [] | ||
while True: | while True: | ||
Zeile 95: | Zeile 92: | ||
#check for a collision | #check for a collision | ||
if agents[i].ball.distance(agents[j].ball) < 20: # because each ball has 20 pixels width | if agents[i].ball.distance(agents[j].ball) < 20: # because each ball has 20 pixels width | ||
+ | |||
+ | |||
#switch the behaviours of these two balls | #switch the behaviours of these two balls | ||
Zeile 101: | Zeile 100: | ||
- | #hier fehlt eine Bedingung, um das ineinanderstecken zu vermeiden? | + | # 0.9 = Wahrscheinlichkeit einer Infektion |
- | ##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 infizierten and j not in recovered and random.random()<0.9: | ||
+ | agents[j].anstecken() | ||
+ | if j not in infizierten: | ||
+ | infizierten.append(j) | ||
- | 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) | ||
+ | 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) | ||
+ | infizierten.remove(k) | ||
wn.mainloop() | wn.mainloop() | ||
- | # To fix: they get stuck together, maybe because they have similar behaviours and go to the direction of each other over and over | + | # To fix: they get stuck together, maybe because they have similar behaviurs and go to the direction of each other over and over |
</code> | </code> |