[英]Finding distance to one of either the x or y coordinate
使用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可以了解發生了什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.