Skip to content

Instantly share code, notes, and snippets.

@horstjens
Created January 6, 2024 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save horstjens/fbc902bd8fc6d2182ba194b6c1844922 to your computer and use it in GitHub Desktop.
Save horstjens/fbc902bd8fc6d2182ba194b6c1844922 to your computer and use it in GitHub Desktop.
babyturtles
import turtle
import random
class Salat(turtle.Turtle):
salatliste = []
def __init__(self):
super().__init__()
self.hideturtle()
Salat.salatliste.append(self)
self.speed()
self.penup()
self.color("#77CF0C","#77CF0C")
self.alter = 0
self.größe = 0.7
self.maximales_alter = 12
self.shape("circle")
self.shapesize(self.größe, self.größe)
self.goto(random.randint(0,breite),
random.randint(0,höhe))
self.showturtle()
class Baby(turtle.Turtle):
babyliste = []
def __init__(self, pos):
super().__init__()
self.speed(0)
self.penup()
self.hideturtle()
self.color("#FBFF80","#FBFF80")
Baby.babyliste.append(self)
self.goto(pos)
self.alter = 0
self.schlüpfen = random.uniform(10,40)
self.shape("circle")
self.showturtle()
def update(self, dt):
self.alter += dt
if self.alter > self.schlüpfen:
if self.shape() == "circle":
self.shape("turtle")
self.color("#90EE90","#90EE90") # hellgrün
self.forward(10 * dt)
if random.random() < 0.01:
ziel = (random.randint(0,breite),
random.randint(0,höhe))
self.setheading(self.towards(ziel))
höhe = 600
breite = 1200
screen = turtle.Screen()
screen.setup(breite, höhe)
screen.setworldcoordinates(0,0,breite,höhe)
screen.title("Baby turtles")
screen.bgcolor("#D2D282")
turtle.speed(0) # ganz schnell
turtle.hideturtle()
screen.tracer(20,0) # superschnell
mama = turtle.Turtle()
mama.shape("turtle")
mama.shapesize(5,5)
mama.color("#008000", "#008000")
zeit = 0
dt = 0.1
ziel = (random.randint(0,breite),
random.randint(0, höhe))
mama.setheading(mama.towards(ziel))
mama.penup()
while True:
# neuer Salat ?
if len(Salat.salatliste) < 30:
Salat()
# babys updaten
for b in Baby.babyliste:
b.update(dt)
# mama bewegen
zeit += dt
mama.forward(5 * dt)
# eier legen?
if random.random() < 0.005:
Baby(mama.pos())
# ziel erreicht ?
if mama.distance(ziel) < 10:
ziel = (random.randint(0,breite),
random.randint(0, höhe))
mama.setheading(mama.towards(ziel))
turtle.exitonclick()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment