gym-super-mario-brosのURL
https://pypi.org/project/gym-super-mario-bros/
インストール
pip3 install gym-super-mario-bros
ユーザ操作による実行
gym_super_mario_bros -m human
ユーザ操作時のコマンド
A 左
D 右
S しゃがむ
O ジャンプ
P ファイヤー(ファイヤマリオの時だけ)
ランダム移動
gym_super_mario_bros -m random
ステージ指定(4-2の場合)
gym_super_mario_bros -e SuperMarioBros-4-2-v1 -m human
引数の意味は以下参照
Individual Stages
These environments allow a single attempt (life) to make it through a single stage of the game.
Use the template
SuperMarioBros---v
where:
is a number in {1, 2, 3, 4, 5, 6, 7, 8} indicating the world
is a number in {1, 2, 3, 4} indicating the stage within a world
is a number in {0, 1, 2, 3} specifying the ROM mode to use
0: standard ROM
1: downsampled ROM
2: pixel ROM
3: rectangle ROM
For example, to play 4-2 on the downsampled ROM, you would use the environment id SuperMarioBros-4-2-v1.
コマンドラインから実行
以下を参考にさせていただきました
sudo apt update
sudo apt install xvfb
pip3 install pyvirtualdisplay
pip3 install matplotlib
sudo apt install imagemagick # gif用
sudo apt install ffmpeg
# マリオ関連のimport
import gym_super_mario_bros
from nes_py.wrappers import JoypadSpace
from gym_super_mario_bros.actions import SIMPLE_MOVEMENT
env = gym_super_mario_bros.make('SuperMarioBros-1-1-v0')
env = JoypadSpace(env, SIMPLE_MOVEMENT)
# プロット関連のimport
import matplotlib.pyplot as plt
from matplotlib import animation, rc
# vcopt関連のimport
import numpy as np
import numpy.random as nr
from vcopt import vcopt
# ゲーム環境のリセット
env.reset()
# 画像の準備
fig = plt.figure()
ims = []
# 繰り返し操作して画面を表示
for i in range(100):
#0:
#1:→
#2:→+ジャンプ
#3:→→
#4:→→+ジャンプ
#5:ジャンプ
#6:←
command = nr.randint(1, 7)
state, reward, done, info = env.step(command)
plt.imshow(env.render(mode='rgb_array'))
plt.show()
if done == True:
break
実行してマリオがランダムに動いているようにみえればOK
python3 sample.py
参考
https://qiita.com/hrs1985/items/871ca5d037d73558bfca
Stable Baselinesを使ってスーパーマリオブラザーズ1-1をクリアするまで
9-3. vcoptでスーパーマリオ1-1をクリアする