Lesson 13: Turtle and lots of ifs

posted Feb 6, 2020, 8:05 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()
for x in range(4):
john.fd(400)
john.right(90)
john.penup()
john.setposition(0,0)

def define_action(john):
if john.xcor() < 200 and john.ypos()> 200:
return 1
if john.xcor() > 200 and john.ypos()> 200:
return 3
if john.xcor() > 200 and john.ypos() < -200:
return 5
if john.xcor() < -200 and john.ypos()<-200:
return 7
if john.ycor() > 200: return 2
if john.xcor() > 200: return 4
if john.ycor() < -200: return 6
if john.xcor() < -200: return 8
return 9

def action(john):
item = define_action()
john.write(item, font=("Arial", 64, "normal"))
if item == 1: john.setposition(-199,199)
if item == 3: john.setposition(199,199)
if item == 5: john.setposition(199,-199)
if item == 7: john.setposition(-199,-199)
if item == 2: john.setposition(john.xcor(), 199)
if item == 4: john.setposition(199, john.ycor())
if item == 6: john.setposition(john.xcor(),-199)
if item == 8: john.setposition(-199, john.ycor())
def move_forward():
john.pencolor("blue")
john.forward(10)
def move_backward():
john.pencolor("white")
john.backward(10)
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