Is there a way to execute a command after Python turtle goes past a certain coordinate?

Viewed 53

I have a code here that changes turtle to a ball and allows the user to move a ball around.

I want it to change background colors when the ball's x value decreases from (30,0) but is there a way to do this? if possible, i want the background color to change back to white if the ball's x value rises past 30 again.

# import turtle setup
from turtle import Screen, Turtle
TURTLE_SIZE = 200

# functions
def go_left():
    x = t.xcor()
    if x > TURTLE_SIZE - 576:
        x -= 5
        t.setx(x)
    screen.update()

def go_right():
    x = t.xcor()
    if x < 576 - TURTLE_SIZE:
        x += 5
        t.setx(x)
    screen.update()

def go_up():
    y = t.ycor()
    if y < 576 - TURTLE_SIZE:
        y += 5
        t.sety(y)
    screen.update()

def go_down():
    y = t.ycor()
    if y < 576 - TURTLE_SIZE:
        y -= 5
        t.sety(y)
    screen.update()

# screen
screen = Screen()
screen.setup(1152,648)
screen.tracer(0)

t = Turtle()
t.shape("circle")
t.speed("slow")
t.color("blue")
t.penup()
t.setx(338)
t.sety(0)
screen.update()

# Keyboard (listeners)
screen.onkeypress(go_left, "Left")
screen.onkeypress(go_down, "Down")
screen.onkeypress(go_up, "Up")
screen.onkeypress(go_right, "Right")
screen.listen()
screen.mainloop()

if x < 30 - TURTLE_SIZE:
    screen.bgcolor("darkred")
3 Answers

According to official turtle's documentation, turtle.pos() returns the current coordinates of turtle. Maybe you could check the coordinate with an if after each movement?

Try this.

# import turtle setup
from turtle import Screen, Turtle
TURTLE_SIZE = 200

# functions
def change_color():
    x = t.xcor()
    if x < 30 - TURTLE_SIZE:
        screen.bgcolor("darkblue")
    else:
        screen.bgcolor("White")
def go_left():
    x = t.xcor()
    if x > TURTLE_SIZE - 576:
        x -= 5
        t.setx(x)
    screen.update()
    change_color()

def go_right():
    x = t.xcor()
    if x < 576 - TURTLE_SIZE:
        x += 5
        t.setx(x)
    screen.update()
    change_color()

def go_up():
    y = t.ycor()
    if y < 576 - TURTLE_SIZE:
        y += 5
        t.sety(y)
    screen.update()

def go_down():
    y = t.ycor()
    if y < 576 - TURTLE_SIZE:
        y -= 5
        t.sety(y)
    screen.update()

# screen
screen = Screen()
screen.setup(1152,648)
screen.tracer(0)

t = Turtle()
t.shape("circle")
t.speed("slow")
t.color("blue")
t.penup()
t.setx(338)
t.sety(0)
screen.update()

# Keyboard (listeners)
screen.onkeypress(go_left, "Left")
screen.onkeypress(go_down, "Down")
screen.onkeypress(go_up, "Up")
screen.onkeypress(go_right, "Right")
screen.listen()
screen.mainloop()



What is added inside this code.

  1. I added a function name change_color and call it every time any key is pressed using the lambda function.

  2. screen.color gives you error because it is screen.bgcolor()

You should check it directly in go_left, go_right

def go_left():
    x = t.xcor()
    if x > TURTLE_SIZE - 576:
        x -= 5
        t.setx(x)

    if x < 30 - TURTLE_SIZE:
        screen.bgcolor("darkred")
    else:
        screen.bgcolor("white")
        
    screen.update()

def go_right():
    x = t.xcor()
    if x < 576 - TURTLE_SIZE:
        x += 5
        t.setx(x)

    if x < 30 - TURTLE_SIZE:
        screen.bgcolor("darkred")
    else:
        screen.bgcolor("white")

    screen.update()

You can reduce it and in go_left change color to darkred and in go_right to white (using >= instead of <)

def go_left():
    x = t.xcor()
    if x > TURTLE_SIZE - 576:
        x -= 5
        t.setx(x)

    if x < 30 - TURTLE_SIZE:
        screen.bgcolor("darkred")
        
    screen.update()

def go_right():
    x = t.xcor()
    if x < 576 - TURTLE_SIZE:
        x += 5
        t.setx(x)

    if x >= 30 - TURTLE_SIZE:
        screen.bgcolor("white")

    screen.update()

EDIT:

Both versions change background in every move - if you want to reduce it then you can remebere current color in global variable and run bgcolor() only when new color is different than current color.

current_backgroud = "white"  # global variable with default value at start

def go_left():
    global current_backgroud
    
    x = t.xcor()
    if x > TURTLE_SIZE - 576:
        x -= 5
        t.setx(x)

    if (x < 30 - TURTLE_SIZE) and (current_backgroud != "darkred"):
        current_backgroud = "darkred"
        screen.bgcolor("darkred")
        
    screen.update()

def go_right():
    global current_backgroud
    
    x = t.xcor()
    if x < 576 - TURTLE_SIZE:
        x += 5
        t.setx(x)

    if (x >= 30 - TURTLE_SIZE) and (current_backgroud != "white"):
        current_backgroud = "white"
        screen.bgcolor("white")

    screen.update()

EDIT:

You can use it to creaet many areas in different colors.

go_left and go_right need if/else in different order because one use < and other >=

current_backgroud = "white"  # global variable with default value at start

def go_left():
    global current_backgroud
    
    x = t.xcor()
    if x > TURTLE_SIZE - 576:
        x -= 5
        t.setx(x)

    if x < 30 - TURTLE_SIZE:
        new_color = "darkred"
    elif x < 130 - TURTLE_SIZE:
        new_color = "red"
    elif x < 230 - TURTLE_SIZE:
        new_color = "yellow"
    else:
        new_color = "white"
        
    if new_color != current_backgroud:
        current_backgroud = new_color
        screen.bgcolor(new_color)
        
    screen.update()

def go_right():
    global current_backgroud
    
    x = t.xcor()
    if x < 576 - TURTLE_SIZE:
        x += 5
        t.setx(x)

    if x >= 230 - TURTLE_SIZE:
        new_color = "white"
    elif x >= 130 - TURTLE_SIZE:
        new_color = "yellow"
    elif x >= 30 - TURTLE_SIZE:
        new_color = "red"
    else:
        new_color = "darkred"
        
    if new_color != current_backgroud:
        current_backgroud = new_color
        screen.bgcolor(new_color)

    screen.update()
Related