微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决使用 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 举报,一经查实,本站将立刻删除。