### Lesson 14: Two turtle chasing each other in a box

posted Feb 13, 2020, 8:04 AM by Attila Lengyel
 ```import turtle as t john = t.Turtle() john.shape("turtle") john.fillcolor("blue") john.pencolor("blue") bob = t.Turtle() bob.shape("turtle") bob.fillcolor("red") bob.pencolor("red") bob.penup() screen = t.Screen() john.penup() john.setposition(-200,200) john.pendown() for x in range(4): john.fd(400) john.right(90) john.penup() john.setposition(-190,190) def move_forward(): john.pencolor("blue") john.forward(9) if john.xcor() < -200 and john.ycor()> 200: john.setposition(-199,199) if john.xcor() > 200 and john.ycor()> 200: john.setposition(199,199) if john.xcor() > 200 and john.ycor() < -200: john.setposition(199,-199) if john.xcor() < -200 and john.ycor()<-200: john.setposition(-199,-199) if john.ycor() > 200: john.setposition(john.xcor(), 199) if john.xcor() > 200: john.setposition(199, john.ycor()) if john.ycor() < -200: john.setposition(john.xcor(),-199) if john.xcor() < -200: john.setposition(-199, john.ycor()) def move_backward(): john.pencolor("white") john.backward(9) if john.xcor() < -200 and john.ycor()> 200: john.setposition(-199,199) if john.xcor() > 200 and john.ycor()> 200: john.setposition(199,199) if john.xcor() > 200 and john.ycor() < -200: john.setposition(199,-199) if john.xcor() < -200 and john.ycor()<-200: john.setposition(-199,-199) if john.ycor() > 200: john.setposition(john.xcor(), 199) if john.xcor() > 200: john.setposition(199, john.ycor()) if john.ycor() < -200: john.setposition(john.xcor(),-199) if john.xcor() < -200: john.setposition(-199, john.ycor()) def turn_right(): john.right(10) def turn_left(): john.left(10) def move_forward_b(): bob.pencolor("red") bob.forward(10) if bob.xcor() < -200 and bob.ycor()> 200: bob.setposition(-199,199) if bob.xcor() > 200 and bob.ycor()> 200: bob.setposition(199,199) if bob.xcor() > 200 and bob.ycor() < -200: bob.setposition(199,-199) if bob.xcor() < -200 and bob.ycor()<-200: bob.setposition(-199,-199) if bob.ycor() > 200: bob.setposition(bob.xcor(), 199) if bob.xcor() > 200: bob.setposition(199, bob.ycor()) if bob.ycor() < -200: bob.setposition(bob.xcor(),-199) if bob.xcor() < -200: bob.setposition(-199, bob.ycor()) def move_backward_b(): bob.pencolor("white") bob.backward(10) if bob.xcor() < -200 and bob.ycor()> 200: bob.setposition(-199,199) if bob.xcor() > 200 and bob.ycor()> 200: bob.setposition(199,199) if bob.xcor() > 200 and bob.ycor() < -200: bob.setposition(199,-199) if bob.xcor() < -200 and bob.ycor()<-200: bob.setposition(-199,-199) if bob.ycor() > 200: bob.setposition(bob.xcor(), 199) if bob.xcor() > 200: bob.setposition(199, bob.ycor()) if bob.ycor() < -200: bob.setposition(bob.xcor(),-199) if bob.xcor() < -200: bob.setposition(-199, bob.ycor()) def turn_right_b(): bob.right(10) def turn_left_b(): bob.left(10) def clear_screen(): john.clear() def start_draw(): john.pendown() def stop_draw(): john.penup() screen.onkey(move_forward, "Up") screen.onkey(move_backward, "Down") screen.onkey(turn_right, "Right") screen.onkey(turn_left, "Left") screen.onkey(clear_screen, "c") screen.onkey(stop_draw, "u") screen.onkey(start_draw, "d") screen.onkey(move_forward_b, "e") screen.onkey(move_backward_b, "d") screen.onkey(turn_right_b, "f") screen.onkey(turn_left_b, "s") screen.listen()```