Last active
July 23, 2024 07:16
-
-
Save horstjens/2bdf942d5cb6e56d0abd231a6fdb55e8 to your computer and use it in GitHub Desktop.
100_turtles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import turtle | |
import random | |
W, H = 2000, 1000 | |
turtle.setup(W,H) | |
screen = turtle.Screen() | |
screen.setworldcoordinates(0,0,W,H) | |
screen.tracer(0) | |
# create turtles | |
for i in range(50): | |
t = turtle.Turtle() | |
t.shape("turtle") | |
t.speed(0) | |
t.penup() | |
t.pencolor((random.random(), random.random(), random.random())) | |
t.fillcolor(t.pencolor()) | |
t.goto(random.randint(0,W), random.randint(0,H)) | |
t.setheading(random.random()*360) | |
t.number = i | |
t.pendown() | |
screen.update() # show the turtles | |
while True: | |
for t in turtle.turtles(): | |
# move the turtles | |
t.forward(random.randint(1,5)) | |
t.left(random.choice((-1,1)) * random.choice((0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,2,2,2,2,3,3,3,4,4,5))) | |
if t.xcor() < 0: | |
t.setx(0) | |
t.setheading(random.randint(-90,90)) | |
t.clear() | |
elif t.xcor() > W: | |
t.setx(W) | |
t.setheading(random.randint(90,270)) | |
t.clear() | |
if t.ycor() < 0: | |
t.sety(0) | |
t.setheading(random.randint(0,180)) | |
t.clear() | |
elif t.ycor() > H: | |
t.sety(H) | |
t.setheading(random.randint(180,360)) | |
t.clear() | |
screen.update() # paint all turtles | |
screen.exitonclick() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import turtle | |
import random | |
#import time | |
W, H = 2000, 1000 | |
FPS = 60 | |
screen = turtle.Screen() | |
screen.setup(W,H) | |
screen.setworldcoordinates(0,0,W,H) | |
screen.bgcolor("#909090") | |
screen.tracer(0) | |
class Kröte(turtle.Turtle): | |
#number = 0 | |
def __init__(self): | |
super().__init__() # start like a turtle | |
self.killme = False | |
self.penup() | |
self.speed(0) | |
self.goto(random.randint(0,W), | |
random.randint(0,H)) | |
self.pendown() | |
self.shape("turtle") | |
self.setheading(random.random()*360) | |
#self.number = Kröte.number | |
#Kröte.number += 1 | |
def move(self): | |
self.forward(random.randint(1,5)) | |
self.left(random.choice((-1,1)) * random.choice((0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,2,2,2,2,3,3,3,4,4,5))) | |
def stay_inside_screen(self): | |
if self.xcor() < 0: | |
self.setx(0) | |
self.setheading(random.randint(-90,90)) | |
self.clear() | |
elif self.xcor() > W: | |
self.setx(W) | |
self.setheading(random.randint(90,270)) | |
self.clear() | |
if self.ycor() < 0: | |
self.sety(0) | |
self.setheading(random.randint(0,180)) | |
self.clear() | |
elif self.ycor() > H: | |
self.sety(H) | |
self.setheading(random.randint(180,360)) | |
self.clear() | |
class KröteFeuer(Kröte): | |
def __init__(self): | |
super().__init__() | |
self.pencolor("red") | |
self.fillcolor("red") | |
class KröteWasser(Kröte): | |
def __init__(self): | |
super().__init__() | |
self.pencolor("blue") | |
self.fillcolor("blue") | |
class KröteGras(Kröte): | |
def __init__(self): | |
super().__init__() | |
self.pencolor("green") | |
self.fillcolor("green") | |
def create_turtles(amount=150): | |
for i in range(amount): | |
#t = turtle.Turtle() | |
rest = i%3 | |
match rest: | |
case 0: | |
KröteFeuer() | |
case 1: | |
KröteWasser() | |
case 2: | |
KröteGras() | |
screen.update() # show the turtles | |
def collision(a, b): | |
name_a = a.__class__.__name__ | |
name_b = b.__class__.__name__ | |
if name_a == name_b: | |
a.back(10) | |
b.back(10) | |
a.left(180) | |
b.left(180) | |
return | |
match name_a: | |
case "KröteFeuer": | |
if name_b == "KröteGras": | |
b.hideturtle() | |
b.clear() | |
b.killme = True | |
elif name_b == "KröteWasser": | |
a.hideturtle() | |
a.clear() | |
a.killme = True | |
case "KröteGras": | |
if name_b == "KröteWasser": | |
b.hideturtle() | |
b.clear() | |
b.killme = True | |
elif name_b == "KröteFeuer": | |
a.hideturtle() | |
a.clear() | |
a.killme = True | |
case "KröteWasser": | |
if name_b == "KröteFeuer": | |
b.hideturtle() | |
b.clear() | |
b.killme = True | |
elif name_b == "KröteGras": | |
a.hideturtle() | |
a.clear() | |
a.killme = True | |
def main(): | |
#last_update = time.time() | |
while True: | |
all_turtles = turtle.turtles() | |
# calculate movement for all turtles | |
for t in all_turtles: | |
if t.killme: | |
continue | |
t.move() | |
# collision? | |
for u in all_turtles: | |
if u.killme: | |
continue | |
if t == u: | |
continue | |
if t.distance(u) < 20: | |
collision(t, u) | |
t.stay_inside_screen() | |
# decide if it is time to draw | |
#while time.time() - last_update < 1/FPS: | |
# time.sleep(1/1000) | |
#last_update = time.time() | |
# draw everything | |
screen.update() # paint all turtles | |
#print(len(turtle.turtles())) | |
screen.title(f"turtles alive: {len(all_turtles)}") | |
# remove dead turtles | |
for t in turtle.turtles(): | |
if t.killme: | |
screen._turtles.remove(t) | |
if __name__ == "__main__": | |
create_turtles() | |
main() | |
#screen.exitonclick() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import turtle | |
import random | |
W, H = 2000, 1000 # width and height of screen | |
FPS = 200 | |
ANIMALS = 100 | |
COLLISION_DISTANCE = 20 | |
interval = int(1/FPS*1000) # milliseconds between two updates | |
screen = turtle.Screen() | |
screen.setup(W,H) | |
screen.setworldcoordinates(0,0,W,H) # set origin (0,0) to lower left corner | |
screen.bgcolor("#909090") # grey | |
screen.tracer(0) # paint turtles only with screen.update | |
class Animal(turtle.Turtle): | |
#number = 0 | |
def __init__(self): | |
super().__init__() # start like a turtle | |
self.dead = False | |
self.penup() | |
self.speed(0) | |
self.goto(random.randint(0,W), | |
random.randint(0,H)) | |
self.pendown() | |
self.shape("turtle") | |
self.setheading(random.random()*360) | |
#self.number = Animal.number | |
#Animal.number += 1 | |
def move(self): | |
self.forward(random.randint(1,5)) | |
self.left(random.choice((-1,1)) * random.choice((0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,2,2,2,2,3,3,3,4,4,5))) | |
def stay_inside_screen(self): | |
if self.xcor() < 0: | |
self.setx(0) | |
self.setheading(random.randint(-90,90)) | |
self.clear() | |
elif self.xcor() > W: | |
self.setx(W) | |
self.setheading(random.randint(90,270)) | |
self.clear() | |
if self.ycor() < 0: | |
self.sety(0) | |
self.setheading(random.randint(0,180)) | |
self.clear() | |
elif self.ycor() > H: | |
self.sety(H) | |
self.setheading(random.randint(180,360)) | |
self.clear() | |
class AnimalFire(Animal): | |
def __init__(self): | |
super().__init__() | |
self.pencolor("red") | |
self.fillcolor("red") | |
class AnimalWater(Animal): | |
def __init__(self): | |
super().__init__() | |
self.pencolor("blue") | |
self.fillcolor("blue") | |
class AnimalGras(Animal): | |
def __init__(self): | |
super().__init__() | |
self.pencolor("green") | |
self.fillcolor("green") | |
def create_turtles(amount=ANIMALS): | |
for i in range(amount): | |
rest = i%3 # modulo: the remainder of a division by 3 | |
match rest: | |
case 0: | |
AnimalFire() | |
case 1: | |
AnimalWater() | |
case 2: | |
AnimalGras() | |
screen.update() # show the turtles | |
def collision(a, b): | |
name_a = a.__class__.__name__ | |
name_b = b.__class__.__name__ | |
if name_a == name_b: | |
a.back(10) | |
b.back(10) | |
a.left(180) | |
b.left(180) | |
return | |
match name_a: | |
case "AnimalFire": | |
if name_b == "AnimalGras": | |
b.hideturtle() | |
b.clear() | |
b.dead = True | |
elif name_b == "AnimalWater": | |
a.hideturtle() | |
a.clear() | |
a.dead = True | |
case "AnimalGras": | |
if name_b == "AnimalWater": | |
b.hideturtle() | |
b.clear() | |
b.dead = True | |
elif name_b == "AnimalFire": | |
a.hideturtle() | |
a.clear() | |
a.dead = True | |
case "AnimalWater": | |
if name_b == "AnimalFire": | |
b.hideturtle() | |
b.clear() | |
b.dead = True | |
elif name_b == "AnimalGras": | |
a.hideturtle() | |
a.clear() | |
a.dead = True | |
def main(): | |
""" | |
This funcion has no while loop! | |
Instead, this function call itself using ontimer() | |
""" | |
all_turtles = turtle.turtles() | |
# ---------- update all turtles ---------- | |
for t in all_turtles: | |
if t.dead: | |
continue | |
t.move() | |
# ---------- collision detection ---------- | |
for u in all_turtles: | |
if u.dead: | |
continue | |
if t == u: | |
continue | |
if t.distance(u) < COLLISION_DISTANCE: | |
collision(t, u) | |
t.stay_inside_screen() | |
# ---------- paint all turtles on the screen ---------- | |
screen.update() | |
# ---------- update window title ---------- | |
screen.title(f"turtles alive: {len(all_turtles)}") | |
# ---------- remove dead turtles ---------- | |
for t in all_turtles: | |
if t.dead: | |
screen._turtles.remove(t) | |
# ---------- call the main function every ~10 milliseconds | |
screen.ontimer(main,interval) | |
if __name__ == "__main__": | |
create_turtles() | |
main() # this function will call itself every ~10 milliseconds | |
screen.exitonclick() | |
print("bye bye") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import turtle | |
import random | |
W, H = 2000, 1000 | |
#turtle.setup(W,H) | |
screen = turtle.Screen() | |
screen.setup(W,H) | |
screen.setworldcoordinates(0,0,W,H) | |
screen.tracer(0) | |
# create turtles | |
class Kröte(turtle.Turtle): | |
#pass | |
#def __init__(self): | |
# super().__init__() # start like a turtle | |
def move(self): | |
self.forward(random.randint(1,5)) | |
self.left(random.choice((-1,1)) * random.choice((0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,2,2,2,2,3,3,3,4,4,5))) | |
def collide_with_others(self): | |
for u in turtle.turtles(): | |
#if u.number == self.number: | |
if u == self: | |
continue | |
distance = self.distance(u) | |
if distance < 10: | |
self.back(10) | |
self.left(180) | |
u.back(10) | |
u.left(180) | |
def stay_inside_screen(self): | |
if self.xcor() < 0: | |
self.setx(0) | |
self.setheading(random.randint(-90,90)) | |
self.clear() | |
elif self.xcor() > W: | |
self.setx(W) | |
self.setheading(random.randint(90,270)) | |
self.clear() | |
if self.ycor() < 0: | |
self.sety(0) | |
self.setheading(random.randint(0,180)) | |
self.clear() | |
elif self.ycor() > H: | |
self.sety(H) | |
self.setheading(random.randint(180,360)) | |
self.clear() | |
for i in range(50): | |
#t = turtle.Turtle() | |
t=Kröte() | |
t.shape("turtle") | |
t.speed(0) | |
t.penup() | |
color = (random.random(), random.random(), random.random()) | |
t.pencolor(color) | |
t.fillcolor(color) | |
t.goto(random.randint(0,W), random.randint(0,H)) | |
t.setheading(random.random()*360) | |
t.number = i | |
t.pendown() | |
screen.update() # show the turtles | |
while True: | |
for t in turtle.turtles(): | |
t.move() | |
t.collide_with_others() | |
t.stay_inside_screen() | |
screen.update() # paint all turtles | |
screen.exitonclick() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment