使用 pyautogui 创建一个游戏机器人

问题描述

所以我正在使用以下代码作为参考构建一个游戏机器人,

from PIL import ImageGrab,ImageOps 
import pyautogui 
import time 
import numpy as np   
    
class cordinates(): 
  
    # coordinates of replay button to start the game 
    replaybutton =(360,214) 
    # this coordinates represent the top-right coordinates 
    # that will be used to define the front Box 
    dinasaur = (149,239 ) 
      
def restartGame(): 
  
    # using pyautogui library,we are clicking on the  
    # replay button without any user interaction  
    pyautogui.click(cordinates.replaybutton) 
  
    # we will keep our Bot always down that  
    # will prevent him to get hit by bird  
    pyautogui.keyDown('down') 
  
def press_space(): 
   
    # releasing the Down Key  
    pyautogui.keyUp('down')  
  
    # pressing Space to overcome Bush 
    pyautogui.keyDown('space') 
  
    # so that Space Key will be recognized easily 
    time.sleep(0.05)  
  
    # printing the "Jump" statement on the 
    # terminal to see the current output  
    print("jump") 
    time.sleep(0.10) 
  
    # releasing the Space Key  
    pyautogui.keyUp('space') 
  
    # again pressing the Down Key to keep my Bot always down  
    pyautogui.keyDown('down') 
  
def imageGrab():  
    # defining the coordinates of Box in front of dinosaur  
    Box = (cordinates.dinasaur[0]+30,cordinates.dinasaur[1],cordinates.dinasaur[0]+120,cordinates.dinasaur[1]+2) 
  
    # grabbing all the pixels values in form of RGB tupples    
    image = ImageGrab.grab(Box) 
  
    # converting RGB to Grayscale to 
    # make processing easy and result faster  
    grayImage = ImageOps.grayscale(image) 
  
    # using numpy to get sum of all grayscale pixels  
    a = np.array(grayImage.getcolors()) 
  
    # returning the sum 
    print(a.sum())  
    return a.sum() 

 

我试图实现上述代码的那个游戏有不同的背景,每隔几分钟就会改变一次,还有额外的功能,比如健康和子弹。 2d游戏简单,玩家自动跑,空格键跳跃,boss来自动开枪。

我想达到的目标: 我希望机器人在看到 Buller 和 health 时不要跳跃 应该只在有障碍物时跳跃。 任何人都可以帮我制作这个机器人吗。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)