import turtle
import random
# Create the turtle screen
screen = turtle.Screen()
screen.bgcolor("white")
# Create the robot turtle
robot = turtle.Turtle()
robot.shape("turtle")
robot.color("green")
robot.penup()
# Create the obstacle turtles
obstacles = []
num_obstacles = 10
for _ in range(num_obstacles):
obstacle = turtle.Turtle()
obstacle.shape("circle")
obstacle.color("red")
obstacle.penup()
obstacle.goto(random.randint(-200, 200), random.randint(-200, 200))
obstacles.append(obstacle)
# Move the robot
def move_forward():
robot.forward(50)
check_collision()
def move_backward():
robot.backward(50)
check_collision()
def turn_left():
robot.left(90)
def turn_right():
robot.right(90)
# Check collision with obstacles
def check_collision():
for obstacle in obstacles:
if robot.distance(obstacle) < 20:
# Avoid the obstacle
robot.right(180)
robot.forward(50)
break
# Set up key bindings
screen.onkey(move_forward, "Up")
screen.onkey(move_backward, "Down")
screen.onkey(turn_left, "Left")
screen.onkey(turn_right, "Right")
screen.listen()
# Main event loop
turtle.done()