Lesson 19: Turtle Race

posted May 1, 2019, 8:03 AM by Attila Lengyel
import turtle as t
t1 = t.Turtle()
t1.shape("turtle")
t1.fillcolor("blue")
t1.pencolor("blue")

t2 = t.Turtle()
screen = t.Screen()

def f(): t1.forward(10)
def b(): t1.backward(10)
def l(): t1.left(10)
def r(): t1.right(10)
screen.onkey(f, "Up")
screen.onkey(b, "Down")
screen.onkey(l, "Left")
screen.onkey(r, "Right")

def catch():
if t1.distance(t2) < 10:
t2.write("CATCH!!!")
t1.clear()
t2.clear()
def f2():
t2.forward(10)
catch()
def b2():
t2.backward(10)
catch()
def l2(): t2.left(10)
def r2(): t2.right(10)

screen.onkey(f2, "s")
screen.onkey(b2, "x")
screen.onkey(l2, "z")
screen.onkey(r2, "c")
screen.listen()

Comments