You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Chrome恐龙游戏自动化脚本Bbox取值异常求助

解决Chrome恐龙游戏自动化脚本的Bbox截图区域问题

问题核心

你的脚本存在两个关键问题:

  • Bbox区域设置过窄:当前截图高度仅2像素,完全无法覆盖仙人掌的有效识别范围,导致颜色数组始终无变化。
  • 逻辑判断错误:循环中直接将函数对象FindCactuses与数值比较,而非调用函数获取返回值;且重复调用函数造成资源浪费。

修正方案

1. 调整Bbox截图范围

将截图区域扩展到恐龙前方能覆盖仙人掌的区间,高度需包含恐龙脚部到地面的范围(仙人掌会出现在这个垂直区间内)。示例调整如下:

box = (
    dinasour[0] + 30,    # 起始x:恐龙右侧30像素,避开恐龙自身
    dinasour[1] - 10,    # 起始y:恐龙上方10像素,扩大垂直覆盖范围
    dinasour[0] + 150,   # 结束x:恐龙右侧150像素,覆盖更远的仙人掌
    dinasour[1] + 20     # 结束y:恐龙下方20像素,覆盖地面附近的仙人掌
)

如果你的屏幕分辨率或游戏窗口大小不同,需根据实际情况微调:

  • 打开游戏后用系统截图工具查看恐龙坐标和仙人掌出现位置,计算合适的x、y范围。
  • 确保Bbox能完整包含仙人掌的黑色像素,同时尽量缩小范围提升识别效率。

2. 修复逻辑判断错误

循环中需先调用FindCactuses()获取返回值,再进行比较,避免重复调用:

while True:  
    color_sum = FindCactuses()
    if color_sum != 697:
        pressSpaceButton()

修改后的完整代码

from PIL import ImageGrab, ImageOps
from webbrowser import open_new_tab as new
from pyautogui import keyDown
from time import sleep
from numpy import *

site_url = "https://trex-runner.com/"
dinasour = (692, 494)


def pressSpaceButton():
    sleep(0.007)
    keyDown('space')
    
      
def openGamePage(): # Open Game URL In New Tab
    new(site_url)


def restartGame(): # Press Space Button To Start/Restart Game
    keyDown('space')
    print("Game Has Been Started / Restarted")


def FindCactuses(): # Find Cactuses In Screen
    box = ( #(top_left_x, top_left_y, bottom_right_x, bottom_right_y)
        dinasour[0] + 30,
        dinasour[1] - 10,
        dinasour[0] + 150, 
        dinasour[1] + 20   
    )
    image = ImageGrab.grab(box)
    grayImage = ImageOps.grayscale(image)
    a = array(grayImage.getcolors())
    print(a)
    return a.sum()

    
sleep(3) # Wait 3 Seconds
openGamePage()

sleep(5)# Wait 5 Seconds
restartGame()

  
while True:  
    color_sum = FindCactuses()
    if color_sum != 697:
        pressSpaceButton()

内容的提问来源于stack exchange,提问作者Ardavan Eskandari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 22:20:30