Lesson 15: Turtle in the box

posted Feb 4, 2020, 8:00 AM by Attila Lengyel
import turtle as t
john = t.Turtle()

john.shape("turtle")
john.fillcolor("blue")
john.pencolor("blue")
screen = t.Screen()

john.penup()
john.setposition(-200,200)
john.pendown()
john.forward(400)
john.right(90)
john.forward(400)
john.right(90)
john.forward(400)
john.right(90)
john.forward(400)
john.setposition(0,0)

def geofence():
if john.ycor() > 190: return False
if john.ycor() < -190: return False
if john.xcor() > 190: return False
if john.xcor() < -190: return False
return True

def move_forward():
speed = 10
if geofence() == False :
john.setposition(0,0)
john.pencolor("blue")
john.forward(speed)
def move_backward():
speed = 10
if geofence() == False :
john.setposition(0,0)
john.pencolor("white")
john.backward(speed)
def turn_right():
john.right(10)
def turn_left():
john.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.listen()
Comments