Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 查找到x或y坐标之一的距离_Python_Drawing_Coordinates_Distance_Turtle Graphics - Fatal编程技术网

Python 查找到x或y坐标之一的距离

Python 查找到x或y坐标之一的距离,python,drawing,coordinates,distance,turtle-graphics,Python,Drawing,Coordinates,Distance,Turtle Graphics,使用turtle.distance,我们可以找到当前位置turtle.position到指定x,y坐标的距离。然而,我们似乎不能将任何一个选项留空。有没有一种简单的方法来计算海龟当前位置到x坐标或y坐标的距离 任务是画一条线,当它碰到一个400x400的边界框时,它会转过身来,我能想到的最好的方法就是找出它是否在框的x或y值的范围内 def check_bounds(pos,a_rect): rect_left = a_rect[0][0] rect_top = a_rect[0

使用turtle.distance,我们可以找到当前位置turtle.position到指定x,y坐标的距离。然而,我们似乎不能将任何一个选项留空。有没有一种简单的方法来计算海龟当前位置到x坐标或y坐标的距离

任务是画一条线,当它碰到一个400x400的边界框时,它会转过身来,我能想到的最好的方法就是找出它是否在框的x或y值的范围内

def check_bounds(pos,a_rect):
    rect_left = a_rect[0][0]
    rect_top = a_rect[0][1]
    rect_right = a_rect[1][0]
    rect_top = a_rect[1][1]
    hit_x = rect_left <= pos[0] <= rect_right 
    hit_y = rect_top <= pos[1] <= rect_bottom
    return hit_x and  hit_y  #the player has hit the box

box = [(50,50),(100,100)] #TL,BR
turtle_pos = (40,40)

check_bounds(turtle_pos,box)
当然你可以简化它。。。。我只是想把它写得漂亮而详细,这样OP就能明白发生了什么