Reputation: 181
I have my hotkeys set up and am able to move turtle around, however when I run the code, nothing happens if i exceed the x and y values.. no errors either. What's wrong?
if (Alex.xcor()>50 or Alex.xcor()<-50) and \
(Alex.ycor()>50 or Alex.ycor()<-50):
Alex.goto(0,0)
Full code:
import turtle
import random
#Setting up the screen to work on
wn=turtle.Screen()
wn.screensize(500,500)
#Setting up properties of Alex
Alex=turtle.Turtle()
Alex.shape('turtle')
Alex.color('blue')
#Setting up function to turn right by 45 degrees
def turn_right():
Alex.right(45)
#Setting up function to turn left by 45 degrees
def turn_left():
Alex.left(45)
#Setting up function to go forward by 30 px
def go_forward():
Alex.forward(30)
#Setting up function to go backwards by 30 px
def go_backward():
Alex.backward(30)
#Setting up keyboard controls for the turtle
turtle.listen()
turtle.onkey(go_forward,"w")
turtle.onkey(turn_left,"a")
turtle.onkey(turn_right,"d")
turtle.onkey(go_backward,"s")
#Setting up border boundaries
if (Alex.xcor()>50 or Alex.xcor()<-50) and \
(Alex.ycor()>50 or Alex.ycor()<-50):
Alex.goto(0,0)
Upvotes: 1
Views: 824
Reputation: 1
The reason check_boundary never worked properly was because you should of never used the return inside the if statement. The return here returns nothing. Instead, all that is being executed is your goto statement. The goto statement should replace the return in the if statement.
Upvotes: 0
Reputation: 41905
Below your boundary logic (gets fixed and) becomes its own function, check_boundary()
and is called by go_forward()
and go_backward()
since they are the only functions that can lead the turtle astray:
from turtle import Turtle, Screen
# Set up the screen to work on
screen = Screen()
screen.setup(500, 500)
# Set up properties of Alex
Alex = Turtle('turtle')
Alex.color('blue')
# Function to turn right by 45 degrees
def turn_right():
Alex.right(45)
# Function to turn left by 45 degrees
def turn_left():
Alex.left(45)
# Function to check boundaries
def check_boundary():
if -100 <= Alex.xcor() <= 100 and -100 <= Alex.ycor() <= 100:
return # OK
Alex.goto(0, 0)
# Function to go forward by 10 px
def go_forward():
Alex.forward(10)
check_boundary()
# Function to go backward by 10 px
def go_backward():
Alex.backward(10)
check_boundary()
# Set up keyboard controls for the turtle
screen.onkey(go_forward, "w")
screen.onkey(turn_left, "a")
screen.onkey(turn_right, "d")
screen.onkey(go_backward, "s")
screen.listen()
screen.mainloop()
Having a turtle moves30px at a time in a 100px cage seems awfully limiting so I increased the size of the cage and shortened his stride so it's easier to see as he comes up against a boundary.
Upvotes: 1
Reputation: 184345
Your if
statement only returns the turtle to the origin when it is outside the boundary in both the X and Y direction at the same time.
You probably want something like this:
if not (-50 <= Alex.xcor() <= 50 and -50 <= Alex.ycor() <= 50):
In other words, define when the turtle is "in bounds" (both X and Y coordinates are between -50 and 50) and then negate that with not
.
Upvotes: 0