用Python实现的游戏模拟器,像超级马里奥、口袋妖怪等经典游戏都可以在上面完美运行。支持:macOS、Raspberry Pi (Raspbian)、Linux (Ubuntu) 和 Windows 10。
GitHub数据
- 3.7k stars
- 72 watching
- 371 forks
开源地址:https://github.com/Baekalfen/PyBoy
安装
安装SDL2
Debian and Ubuntu
代码语言:javascript复制> sudo apt update
> sudo apt install python3 python3-pip python3-dev libsdl2-dev build-essential
> python3 -m pip install --upgrade pip
> python3 -m pip install pyboy
macOS
代码语言:javascript复制> brew update
> brew install python3 sdl2
> python3 -m pip install --upgrade pip
> python3 -m pip install pyboy
树莓派
代码语言:javascript复制Raspberry Pi OS 的说明与上面的 Ubuntu/Linux 说明几乎相同
> sudo apt update
> sudo apt install libsdl2-dev libatlas-base-dev
> python3 -m pip install --upgrade pip
> python3 -m pip install pyboy
Windows 10
代码语言:javascript复制> (New-Object Net.WebClient).DownloadFile('https://www.python.org/ftp/python/3.7.6/python-3.7.6-amd64.exe', 'python-3.7.6-amd64.exe')
> ./python-3.7.6-amd64.exe /passive InstallAllUsers=1 PrependPath=1
代码语言:javascript复制需要重新启动 PowerShell 。
> (New-Object Net.WebClient).DownloadFile('https://www.libsdl.org/release/SDL2-devel-2.0.10-VC.zip', 'SDL2-devel-2.0.10-VC.zip')
> Expand-Archive -Force 'SDL2-devel-2.0.10-VC.zip' C:SDL2
> setx PYSDL2_DLL_PATH C:SDL2SDL2-2.0.10libx64
> setx PATH "%PATH%;C:SDL2SDL2-2.0.10libx64"
> python -m pip install --upgrade --user pip
> python -m pip install --user pyboy
启动 PyBoy
找到 ROM 转储文件,然后
python3 -m pyboy path/to/rom.gb
从 PyBoy Git repo 的根目录运行。如果您选择在系统上安装 PyBoy,你可以pyboy path/to/rom.gb
从任何目录执行。
PyBoy的控制键
键盘键 | 等价于GameBoy |
---|---|
Up | Up |
Down | Down |
Left | Left |
Right | Right |
A | A |
S | B |
Return | Start |
Backspace | Select |
PyBoy模拟器上的其它键
键盘键 | 模拟器功能 |
---|---|
Escape | 退出 |
D | 调试 |
Space | 无限FPS |
Z | 保存状态 |
X | 加载状态 |
I | 切换屏幕录制 |
, | 回退 |
. | 快退 |
使用案例
代码语言:javascript复制from pyboy import PyBoy
with PyBoy('ROMs/gamerom.gb') as pyboy:
while not pyboy.tick():
pass
代码语言:javascript复制这里有一个从屏幕读取数据的简短演示。该代码也可以在以下位置找到
gamewrapper_mario.py
import os
import sys
from pyboy import PyBoy, WindowEvent
# Makes us able to import PyBoy from the directory below
file_path = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, file_path "/..")
# Check if the ROM is given through argv
if len(sys.argv) > 1:
filename = sys.argv[1]
else:
print("Usage: python mario_boiler_plate.py [ROM file]")
exit(1)
quiet = "--quiet" in sys.argv
pyboy = PyBoy(filename, window_type="headless" if quiet else "SDL2", window_scale=3, debug=not quiet, game_wrapper=True)
pyboy.set_emulation_speed(0)
assert pyboy.cartridge_title() == "SUPER MARIOLAN"
mario = pyboy.game_wrapper()
mario.start_game()
assert mario.score == 0
assert mario.lives_left == 2
assert mario.time_left == 400
assert mario.world == (1, 1)
assert mario.fitness == 0 # A built-in fitness score for AI development
last_fitness = 0
print(mario)
pyboy.send_input(WindowEvent.PRESS_ARROW_RIGHT)
for _ in range(1000):
assert mario.fitness >= last_fitness
last_fitness = mario.fitness
pyboy.tick()
if mario.lives_left == 1:
assert last_fitness == 27700
assert mario.fitness == 17700 # Loosing a live, means 10.000 points in this fitness scoring
print(mario)
break
else:
print("Mario didn't die?")
exit(2)
mario.reset_game()
assert mario.lives_left == 2
pyboy.stop()
如果您在加载了 Super Mario Land ROM 的情况下运行上述代码,你将得到类似于下面的图片和终端打印输出的内容。请注意以索引 0、1、16 和 17 表示的马里奥的形状。
更多功能广大网友可以继续挖掘。