为什么下面的 python while 循环永远不会中断?它是一个简单的机器人,只会转身直到面对目标
Why does the python while loop below never break? Its a simple bot that does nothing but turn till its facing a target
def turn():
x=0
while x==0:
#the players X co-ordinate, and the players Y co-ordinate is set to 513,437 respectively
player_x, player_y=513,437
#It looks for the HP bar of the monster, and then notes down the location in terms of left, right, height in the variable target (or target2, whichever it detects.)
target = pyautogui.locateOnScreen(os.path.expanduser(r'~\Desktop\wow bot\references\target.png'),
region=(0, 0, 1024, 768), confidence=.7)
target2 = pyautogui.locateOnScreen(os.path.expanduser(r'~\Desktop\wow bot\references\target2.png'),
region=(0, 0, 1024, 768), confidence=.7)
#this turns the target location into an X, and Y format. So the location of the mob gets turned into x and y position and stored in target_x and target_y
if target is not None or target2 is not None:
global targety
global targetx
target_point=pyautogui.center(target or target2)
targetx,targety=target_point
#distance a = square root of target_y minus players_y to the power of 2
distance_a=math.sqrt((targety-player_y)**2)
#distance h = square root of target_X minus player_x to the power of 2 plus target_y minus player_y to the power of 2
distance_h=math.sqrt((targetx-player_x)**2+(targety-player_y)**2)
#inverse tan of distance a divide by distance h
radian=math.acos(distance_a/distance_h)
#turns radian into degrees
theta=(radian*180/math.pi)
#displays output of the degrees
print((theta))
direction=player_x-targetx
print(direction)
if theta >=25:
if direction <=-1:
keyboard.press('d')
time.sleep(0.009)
keyboard.release('d')
if theta <=25:
x=1
elif direction >=1:
keyboard.press('a')
time.sleep(0.009)
keyboard.release('a')
if theta <= 25:
x=1
我不明白为什么上面的代码没有中断?它是一个简单的机器人,除了转向它面对目标之外什么都不做。我希望循环中断,以便之后我可以继续执行另一项任务。
您的循环将仅在 theta=25
处中断,因为您的第一个 if
条件声明 theta>=25
并且嵌套的 if 条件声明 theta<=25
然后仅 x=1
这很荒谬,因此只有 theta=25
满足条件
def turn():
x=0
while x==0:
#the players X co-ordinate, and the players Y co-ordinate is set to 513,437 respectively
player_x, player_y=513,437
#It looks for the HP bar of the monster, and then notes down the location in terms of left, right, height in the variable target (or target2, whichever it detects.)
target = pyautogui.locateOnScreen(os.path.expanduser(r'~\Desktop\wow bot\references\target.png'),
region=(0, 0, 1024, 768), confidence=.7)
target2 = pyautogui.locateOnScreen(os.path.expanduser(r'~\Desktop\wow bot\references\target2.png'),
region=(0, 0, 1024, 768), confidence=.7)
#this turns the target location into an X, and Y format. So the location of the mob gets turned into x and y position and stored in target_x and target_y
if target is not None or target2 is not None:
global targety
global targetx
target_point=pyautogui.center(target or target2)
targetx,targety=target_point
#distance a = square root of target_y minus players_y to the power of 2
distance_a=math.sqrt((targety-player_y)**2)
#distance h = square root of target_X minus player_x to the power of 2 plus target_y minus player_y to the power of 2
distance_h=math.sqrt((targetx-player_x)**2+(targety-player_y)**2)
#inverse tan of distance a divide by distance h
radian=math.acos(distance_a/distance_h)
#turns radian into degrees
theta=(radian*180/math.pi)
#displays output of the degrees
print((theta))
direction=player_x-targetx
print(direction)
if theta >=25:
if direction <=-1:
keyboard.press('d')
time.sleep(0.009)
keyboard.release('d')
if theta <=25:
x=1
elif direction >=1:
keyboard.press('a')
time.sleep(0.009)
keyboard.release('a')
if theta <= 25:
x=1
我不明白为什么上面的代码没有中断?它是一个简单的机器人,除了转向它面对目标之外什么都不做。我希望循环中断,以便之后我可以继续执行另一项任务。
您的循环将仅在 theta=25
处中断,因为您的第一个 if
条件声明 theta>=25
并且嵌套的 if 条件声明 theta<=25
然后仅 x=1
这很荒谬,因此只有 theta=25