首页 > 解决方案 > 如何在不使用 time.sleep 的情况下跟踪我在游戏中前进了多少秒?

问题描述

def pathing(shape,size):
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)
target3 = pyautogui.locateOnScreen(os.path.expanduser(r'~\Desktop\wow bot\references\target3.png'),
                                   region=(0, 0, 1024, 768), confidence=.7)
distance_moved=[]
seconds_moved=0
if shape=='triangle':
    if target is None and target2 is None and target3 is None:
        pyautogui.keyDown("w")
        distance_moved.append(seconds_moved+1)
        seconds_moved+=1

我有上面的代码来跟踪移动的秒数,并将其附加到列表 distance_moved[]。然而,问题在于它不会为每 1 秒移动的秒数增加 +1。是否有可能让它在每秒后添加 +1,但不使用 time.sleep?

感谢您的任何回答!

标签: python

解决方案


您可以使用time.time 示例:

import time 
start = time.time()
while True:
    now = time.time()
    print(f"{now - start} second(s) have passed")

所以在你的情况下:

if now - start > 1:
    distance_moved.append(seconds_moved+1)

如果您希望它每隔一秒发生一次,您可以像这样重置计时器:

if now - start > 1:
    distance_moved.append(seconds_moved+1)
    start = now

推荐阅读