How would I be able to make a randomly moving turtle be constrained inside a circle with a radius of 50, the circles center being at (0, 0)? So if the turtle is currently at location (x, y), it's distance from the center is math.sqrt(x ** 2 + y ** 2). Whenever the turtle's distance from the center is more than 50, have it turn around and continue. I have gotten the code to work with the screen size, but where do I put math.sqrt(x ** 2 + y ** 2) to get it to be constrained in a circle? Here's the code I have so far:
import turtle, random, math
def bounded_random_walk(num_steps, step_size, max_turn):
turtle.reset()
width = turtle.window_width()
height = turtle.window_height()
for step in range(num_steps):
turtle.forward(step_size)
turn = random.randint(-max_turn, max_turn)
turtle.left(turn)
x, y = turtle.position()
if -width/2 <= x <= width/2 and -height/2 <= y <= height/2:
pass
else: # turn around!
turtle.left(180)
turtle.forward(step_size)
This code works for a turtle in the screen, but not in a circle.