python - 如何在不使用 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?
感谢您的任何回答!
解决方案
您可以使用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
推荐阅读
- php - Slim 框架 - PHP 没有正确路由 - 有什么想法吗?
- javascript - Javascript 应用程序在本地主机上运行,但在部署时无法正常运行
- android - 我无法在 Kotlin/Android Studio 中获取/列出文件,只有目录
- c++ - 分数背包算法分段错误
- java - 在 Java 中创建 3D 数组
- c# - C# 字符串变为空
- javascript - 如何跟踪 mongodb 内部发生的变化并在 React 应用程序中获取所有数据
- amazon-web-services - 从 S3 中的特定存储桶中删除对象需要哪些权限
- python - 如果只有第一列有数据,熊猫就会删除行
- javascript - React Router 在点击卡片后创建一个“新页面”