如何通过xcor和ycor判断Python Turtle是否处于指定圆内?
编程作业问题:判断海龟是否在圆内
作业要求:
- 绘制一个圆
- 将海龟返回起始位置
- 让用户移动海龟十次
- 判断海龟是否处于圆内并显示胜负提示
我需要通过xcor()和ycor()来完成判断,但不清楚具体计算方法,以下是我当前编写的代码:
# import turtle import turtle # declare variables answer = str() move_counter = int() user_turn = str() user_turn_num = int() user_move = str() user_move_num = int() # establish turtle color and background color turtle.pencolor("magenta") turtle.bgcolor("black") # ask the user what shape they'd like the turtle to be print("Turtle has many shapes to choose from. Your options are classic, arrow, turtle, circle, square and triangle.") answer = input("What shape would you like the turtle to be? ") # if structure for changing the shape depending on the user's answer if answer == "classic": turtle.shape("classic") elif answer == "turtle": turtle.shape("turtle") elif answer == "arrow": turtle.shape("arrow") elif answer == "circle": turtle.shape("circle") elif answer == "square": turtle.shape("square") elif answer == "triangle": turtle.shape("triangle") # bring turtle to circle location turtle.penup() turtle.goto(100,-200) # have turtle draw the circle turtle.pendown() turtle.fillcolor("magenta") turtle.begin_fill() turtle.circle(100) turtle.end_fill() # return turtle to starting area turtle.penup() turtle.goto(0,0) while move_counter < 10: # ask user if they'd like to turn user_turn = input("Which direction would you like to turn (left, right, neither)? ") # create if structure for turning if user_turn == "left": user_turn_num = int(input("Turn how many degrees to the left? ")) turtle.left(user_turn_num) elif user_turn == "right": user_turn_num = int(input("Turn how many degrees to the right?")) turtle.right(user_turn_num) elif user_turn == "neither": print("No turn made.") # ask user if they'd like to move forward or backward user_move = input("Would you like to move forward or backward? ") # create if structure for moving forward or backward if user_move == "forward": # ask user how many spaces they'd like to move forward and move turtle that many spaces user_move_num = int(input("How many spaces would you like to move forward?" )) turtle.forward(user_move_num) elif user_move == "backward": # ask user how many spaces they'd like to move backward and move turtle that many spaces user_move_num = int(input("How many spaces would you like to move backward?" )) turtle.backward(user_move_num) # increment counter and let user know how many moves they have left move_counter = move_counter + 1 print("You have", 10 - move_counter, "moves left.") # determine if turtle is within the circle xpos = turtle.xcor ypos = turtle.ycor # display win message # display loss message # make turtle exit on click turtle.exitonclick()
解决方案
判断原理
你绘制的圆圆心坐标是(100, -200),半径为100。判断点是否在圆内,需要计算海龟当前位置到圆心的距离,若距离小于等于半径,说明海龟在圆内(包括边界)。
为了避免开根号的计算开销,直接比较距离的平方和半径的平方即可,结果与比较距离本身一致:
距离平方 = (海龟x坐标 - 圆心x坐标)² + (海龟y坐标 - 圆心y坐标)²
若该值 ≤ 半径²,则海龟在圆内。
代码修改
- 首先修正
xcor和ycor的调用:这两个是方法,必须加括号()才能获取坐标值。 - 添加判断逻辑和胜负提示:
替换原代码中以下部分:
# determine if turtle is within the circle xpos = turtle.xcor ypos = turtle.ycor # display win message # display loss message
为:
# determine if turtle is within the circle xpos = turtle.xcor() ypos = turtle.ycor() # 定义圆的参数 circle_center_x = 100 circle_center_y = -200 circle_radius = 100 # 计算距离平方与半径平方 distance_squared = (xpos - circle_center_x) ** 2 + (ypos - circle_center_y) ** 2 radius_squared = circle_radius ** 2 # 判断并显示结果 if distance_squared <= radius_squared: print("恭喜!你成功将海龟移到圆内,获胜!") else: print("很遗憾,海龟不在圆内,挑战失败!")
额外优化提示
- 可以把圆心坐标和radius定义为全局变量,避免硬编码,让代码更易修改维护。
- Python不需要提前声明变量类型,原代码中变量声明部分可简化为直接赋值。
内容的提问来源于stack exchange,提问作者nightstargalaxy
相关产品推荐
相关产品推荐

