首页
/ 星际争霸2 AI 项目教程

星际争霸2 AI 项目教程

2024-08-25 20:09:38作者:滕妙奇

1. 项目的目录结构及介绍

SC2AI/
├── README.md
├── requirements.txt
├── sc2/
│   ├── __init__.py
│   ├── run_game.py
│   ├── maps.py
│   ├── player.py
│   └── constants.py
├── bots/
│   ├── __init__.py
│   ├── sentde_bot.py
│   └── config.py
└── tests/
    ├── __init__.py
    └── test_bot.py
  • README.md: 项目介绍和使用说明。
  • requirements.txt: 项目依赖的Python包列表。
  • sc2/: 包含星际争霸2 API的核心模块。
    • run_game.py: 启动游戏的主文件。
    • maps.py: 地图管理模块。
    • player.py: 玩家管理模块。
    • constants.py: 常量定义模块。
  • bots/: 包含自定义的AI bot文件。
    • sentde_bot.py: 自定义的AI bot实现。
    • config.py: AI bot的配置文件。
  • tests/: 包含测试文件。
    • test_bot.py: 测试自定义AI bot的文件。

2. 项目的启动文件介绍

run_game.py

run_game.py 是项目的启动文件,负责启动游戏并指定各项启动参数。以下是该文件的主要功能:

  • 导入必要的模块:

    from sc2 import run_game, maps, Race, Difficulty
    from sc2.player import Bot, Computer
    
  • 定义AI bot类:

    class SentdeBot(sc2.BotAI):
        async def on_step(self, iteration: int):
            await self.distribute_workers()
    
  • 启动游戏:

    def main():
        run_game(maps.get("AutomatonLE"), [
            Bot(Race.Protoss, SentdeBot()),
            Computer(Race.Terran, Difficulty.Easy)
        ], realtime=True)
    

3. 项目的配置文件介绍

config.py

config.py 是AI bot的配置文件,包含一些基本的配置信息,如游戏路径、种族选择等。以下是该文件的主要内容:

  • 游戏路径配置:

    BASEDIR = {
        "Windows": "F:\\Games\\StarCraft II",
        "Darwin": "/Applications/StarCraft II",
        "Linux": "~/StarCraftII",
        "WineLinux": "~/wine/drive_c/Program Files (x86)/StarCraft II"
    }
    
  • 种族选择:

    RACE = Race.Protoss
    
  • 难度选择:

    DIFFICULTY = Difficulty.Easy
    

通过这些配置,可以方便地修改AI bot的运行环境和对手设置。

登录后查看全文
热门项目推荐