搜索文章

输入关键词开始搜索

AI游戏助手

思考#TODO#AI#游戏开发#Python#算法#未来的路

思路

图像识别找到怪物

深度强化学习训练策略模型

GPT 方案设计

纯按键版本

pip install pyautogui

识图版本

https://claude.ai/chat/640507fe-f354-48ef-bdcf-f8a6ab4f289a


写一个魔兽世界的脚本,用来自动识别怪物和打怪,请帮我进行方案设计、技术选型、并给出代码。
好的,我来为您设计一个魔兽世界自动打怪脚本的方案,并给出相应的代码实现。
首先,让我们来进行方案设计和技术选型:

方案设计:

使用图像识别来检测屏幕上的怪物
实现自动寻路和移动角色的功能
设计战斗循环,包括技能释放和目标切换
添加简单的物品拾取功能


技术选型:

编程语言: Python (易于使用,有丰富的库支持)
图像识别: OpenCV (用于屏幕捕获和图像处理)
GUI自动化: PyAutoGUI (用于模拟键盘和鼠标输入)
游戏内存读取: ReadWriteMemory (用于读取游戏内存中的数据)



现在,让我们来实现这个脚本的基本框架:
魔兽世界自动打怪脚本Click to open code
这个脚本提供了一个基本的框架,实现了怪物检测、移动、战斗和拾取的功能。以下是对脚本各部分的简要说明:

detect_monster(): 使用OpenCV检测屏幕上的怪物。目前的实现是检测红色像素,你可能需要根据具体游戏画面调整这个算法。
move_to_target(): 模拟按下"W"键向前移动。你可能需要根据游戏中的实际情况调整移动逻辑。
combat_rotation(): 实现了一个简单的战斗循环,按顺序使用三个技能。你需要根据你的角色类型和技能来调整这个函数。
loot(): 模拟按下"V"键拾取物品。请确保这与你的游戏设置一致。
run(): 主循环,不断检测怪物,进行移动、战斗和拾取。

请注意,这个脚本仅提供了一个基本框架,你可能需要根据具体的游戏版本、角色类型和个人需求进行调整。此外,使用自动化脚本可能违反游戏的使用条款,请谨慎使用。
import cv2
import numpy as np
import pyautogui
import time
from ReadWriteMemory import ReadWriteMemory

class WoWBot:
    def __init__(self):
        self.rwm = ReadWriteMemory()
        self.process = self.rwm.get_process_by_name("Wow.exe")
        self.process.open()

    def detect_monster(self):
        # 使用OpenCV检测屏幕上的怪物
        screenshot = pyautogui.screenshot()
        screenshot = cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2BGR)

        # 这里需要根据具体的怪物外观来调整检测算法
        # 示例: 检测红色的像素点(通常怪物名字是红色的)
        lower_red = np.array([0, 0, 100])
        upper_red = np.array([100, 100, 255])
        mask = cv2.inRange(screenshot, lower_red, upper_red)

        contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        if contours:
            return True
        return False

    def move_to_target(self):
        # 模拟按下"W"键向前移动
        pyautogui.keyDown('w')
        time.sleep(2)  # 移动2秒
        pyautogui.keyUp('w')

    def combat_rotation(self):
        # 简单的战斗循环
        pyautogui.press('1')  # 使用技能1
        time.sleep(1.5)
        pyautogui.press('2')  # 使用技能2
        time.sleep(1.5)
        pyautogui.press('3')  # 使用技能3
        time.sleep(1.5)

    def loot(self):
        # 模拟按下"V"键拾取物品(假设"V"是拾取快捷键)
        pyautogui.press('v')

    def run(self):
        while True:
            if self.detect_monster():
                self.move_to_target()
                while self.detect_monster():  # 持续战斗直到怪物消失
                    self.combat_rotation()
                self.loot()
            else:
                self.move_to_target()  # 如果没有检测到怪物,继续移动
            time.sleep(0.5)  # 短暂暂停以降低CPU使用率

if __name__ == "__main__":
    bot = WoWBot()
    bot.run()

按键精灵

https://blog.csdn.net/weixin_39606048/article/details/111455776

思路:通过头像颜色判断是否进入攻击范围、是否已被击杀等等。

待改进:

1、能否判断与怪物的距离? 2、如何正确识别怪物与人的相对方向(检查技能是否可用:尝试使用一个需要面对目标的技能,如果失败则调整方向)? 3、如何向怪物跑过去(跑几步的算法)? 4、如何限制范围,避免跑远了卡住?

Hwnd = Plugin.Window.Find(0, "魔兽世界")

//设置换视角标志1=刚切换视角 0否

//UserVar Flag1 = 0

Rem 开始

//按Tab寻找目标

Call Plugin.Bkgnd.KeyPress(Hwnd, 9)

Delay 1000

//抓到头像判断颜色

IfColor 320,69,"009400",0 Then

//抓取到怪后进入打怪子过程

Call KillObject()

Else

//没找到怪偏转视角

Call TurnView()

End If

Goto 开始

//打怪

Sub KillObject()

Rem 打怪循环

//能两不足

IfColor 112, 78, "2D59B2", 0 Then

//优先使用技能7 急速射击

Call Plugin.Bkgnd.KeyPress(Hwnd, 55)

//优先使用技能8 群兽奔腾

Call Plugin.Bkgnd.KeyPress(Hwnd, 56)

//优先使用技能9 夺命乌鸦

Call Plugin.Bkgnd.KeyPress(Hwnd, 57)

//优先使用技能6 杀戮射击

Call Plugin.Bkgnd.KeyPress(Hwnd, 54)

//优先使用技能R 飞刃

Call Plugin.Bkgnd.KeyPress(Hwnd, 82)

//优先使用技能2 爆炸射击

Call Plugin.Bkgnd.KeyPress(Hwnd, 50)

//技能C 奥术射击

Call Plugin.Bkgnd.KeyPress(Hwnd, 67)

Else

//优先使用技能7 急速射击

Call Plugin.Bkgnd.KeyPress(Hwnd, 55)

//优先使用技能8 群兽奔腾

Call Plugin.Bkgnd.KeyPress(Hwnd, 55)

//优先使用技能6 杀戮射击

Call Plugin.Bkgnd.KeyPress(Hwnd, 54)

//优先使用技能2 爆炸射击

Call Plugin.Bkgnd.KeyPress(Hwnd, 50)

//技能 E 眼镜蛇射击

Call Plugin.Bkgnd.KeyPress(Hwnd, 69)

//技能释放时间

Delay 600

End If

//公共CD延迟

Delay 1000

//判断目标怪是否存活

IfColor 320,69,"009400",0 Then

//存活就继续循环打怪

Goto 打怪循环

Else

//退出子过程

End If

End Sub

//转换视角

Sub TurnView()

Call Plugin.Bkgnd.KeyDown(Hwnd, 65)

Delay 100

Call Plugin.Bkgnd.KeyUp(Hwnd, 65)

End Sub

常用函数


[General]
SyntaxVersion=2
BeginHotkey=121
BeginHotkeyMod=0
PauseHotkey=0
PauseHotkeyMod=0
StopHotkey=123
StopHotkeyMod=0
RunOnce=1
EnableWindow=
MacroID=17711ebe-dee7-4100-877f-98ceb167e95a
Description=我的脚本1
Enable=1
AutoRun=0
[Repeat]
Type=0
Number=1
[Relative]
SetupOCXFile=
[Comment]

[Script]

//Call RunApp("notepad.exe")
Delay 5000
//下面这句是得到当前最前面的窗口句柄
Hwnd = Plugin.Window.Foreground()
MsgBox "得到当前最前面的窗口句柄为:"& Hwnd
//下面这句是得到窗口句柄的类名
Class1 = Plugin.Window.GetClass(Hwnd)
MsgBox "得到窗口句柄的类名为:" & Class1



Delay 5000
//Hwnd = Plugin.Window.Find("GxWindowClass", "魔兽世界")

Hwnd = Plugin.Window.Find("GxWindowClass", "暴雪战网")

MsgBox "得到当前最前面的窗口句柄为:"& Hwnd
Delay 5000


Call Plugin.Bkgnd.MoveTo(Hwnd, 100, 100)


Call Plugin.Bkgnd.KeyPress(Hwnd, 32)

Call Plugin.Bkgnd.KeyDown(Hwnd, 38)
Delay 5000
Call Plugin.Bkgnd.KeyUp(Hwnd, 38)

参考资料

KeymouseGo

Python AI 全自动刷怪升级:

https://www.bilibili.com/video/BV1SN41117La/

贪吃蛇:

https://www.bilibili.com/video/BV1ag4y1F7x4/

街头霸王:

https://www.bilibili.com/video/BV1DT411H7ph/

(精)超级马里奥:

https://www.bilibili.com/video/BV1iL411A7zo/

星际争霸 2:

https://www.bilibili.com/video/BV16a411d71p/

王者荣耀:

https://www.bilibili.com/video/BV13V411W7aW/

读取魔兽世界怀旧服内存的研究: https://wkings.blog/archives/406