为什么我的 Python 机器人无法运行? (PyAutoGUI)

发布于 2025-01-10 18:55:40 字数 1486 浏览 0 评论 0原文

我用 Python 编写了一个机器人,它应该自动播放“Friday Night Funkin”(在应该按下箭头时按下箭头),但由于某种原因它没有执行任何操作。当箭头被按下时,我截取了箭头的屏幕截图,这样如果 python 看到箭头被按下(它看到图像/屏幕截图),它就会按下相应的键。我尝试关闭灰度,但没有成功。有什么办法可以解决这个问题或者以不同的方式实现它吗?我对Python真的很陌生,这是我的第一个代码,如果这是一个愚蠢的问题和问题,我很抱歉。

我的 FNF 版本:https://poki.pl/g/friday-night-funkin

我的图片: https://i.sstatic.net/JxQdl.jpg

我的代码:

from pyautogui import *
import pyautogui
import time
import keyboard
import numpy as np
import random
import win32api, win32con

time.sleep(5)

while keyboard.is_pressed('q') == False:

if pyautogui.locateOnScreen('leftarrow.png', region=(1010, 50, 650, 200), grayscale=False, confidence=0.7) != None:
    pyautogui.keyDown('left')
    time.sleep(0.1)
    pyautogui.keyUp('left')

if pyautogui.locateOnScreen('rightarrow.png', region=(1010, 50, 650, 200), grayscale=False, confidence=0.7) != None:
    pyautogui.keyDown('right')
    time.sleep(0.1)
    pyautogui.keyUp('right')
    
if pyautogui.locateOnScreen('uparrow.png', region=(1010, 50, 650, 200), grayscale=False, confidence=0.7) != None:
    pyautogui.keyDown('up')
    time.sleep(0.1)
    pyautogui.keyUp('up')

if pyautogui.locateOnScreen('downarrow.png', region=(1010, 50, 650, 200), grayscale=False, confidence=0.7) != None:
    pyautogui.keyDown('down')
    time.sleep(0.1)
    pyautogui.keyUp('down')

I coded a bot in Python that should automatically play Friday Night Funkin' (press the arrows when they are meant to be pressed) but for some reason it doesn't do anything. I took screenshots of the arrows when they are meant to be pressed and I made it so if python sees that the arrow is meant to be pressed (it sees the images/screenshots) it presses the corresponding key. I tried turning greyscale off but it didn't work. Is there any way to fix this or make it in a different way? I'm really new to Python and this is my first code so sorry if it's a stupid problem and question.

My FNF Version: https://poki.pl/g/friday-night-funkin

My images: https://i.sstatic.net/JxQdl.jpg

My code:

from pyautogui import *
import pyautogui
import time
import keyboard
import numpy as np
import random
import win32api, win32con

time.sleep(5)

while keyboard.is_pressed('q') == False:

if pyautogui.locateOnScreen('leftarrow.png', region=(1010, 50, 650, 200), grayscale=False, confidence=0.7) != None:
    pyautogui.keyDown('left')
    time.sleep(0.1)
    pyautogui.keyUp('left')

if pyautogui.locateOnScreen('rightarrow.png', region=(1010, 50, 650, 200), grayscale=False, confidence=0.7) != None:
    pyautogui.keyDown('right')
    time.sleep(0.1)
    pyautogui.keyUp('right')
    
if pyautogui.locateOnScreen('uparrow.png', region=(1010, 50, 650, 200), grayscale=False, confidence=0.7) != None:
    pyautogui.keyDown('up')
    time.sleep(0.1)
    pyautogui.keyUp('up')

if pyautogui.locateOnScreen('downarrow.png', region=(1010, 50, 650, 200), grayscale=False, confidence=0.7) != None:
    pyautogui.keyDown('down')
    time.sleep(0.1)
    pyautogui.keyUp('down')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北城半夏 2025-01-17 18:55:40

也许是复制粘贴滞后,但在“if”之前没有缩进,

使用“not None”而不是“!= None”

使用

if __name__ == "__main__": 

您的脚本,因为它现在只能作为 python 文件运行,通过控制台命令或导入。

maybe it is copy-paste lag, but there is no indention before "if"

use "not None" instead of "!= None"

use

if __name__ == "__main__": 

your script as it now can be run only as python file, through console command or import.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文