最近有一个叫PyBoy的开源项目火了,原因是它使用了Python 2.7重新将那些在GameBoy上的上古游戏的整个模拟器实现了出来。
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: World 1-1
Coins: 0
lives_left: 2
Score: 0
Time left: 400
Level progress: 251
Fitness: 0
Sprites on screen:
Sprite [3]: Position: (35, 112), Shape: (8, 8), Tiles: (Tile: 0), On screen: True
Sprite [4]: Position: (43, 112), Shape: (8, 8), Tiles: (Tile: 1), On screen: True
Sprite [5]: Position: (35, 120), Shape: (8, 8), Tiles: (Tile: 16), On screen: True
Sprite [6]: Position: (43, 120), Shape: (8, 8), Tiles: (Tile: 17), On screen: True
Tiles on screen:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
____________________________________________________________________________________
0 | 339 339 339 339 339 339 339 339 339 339 339 339 339 339 339 339 339 339 339 339
1 | 320 320 320 320 320 320 320 320 320 320 320 320 320 320 320 320 320 320 320 320
2 | 300 300 300 300 300 300 300 300 300 300 300 300 321 322 321 322 323 300 300 300
3 | 300 300 300 300 300 300 300 300 300 300 300 324 325 326 325 326 327 300 300 300
4 | 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300
5 | 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300
6 | 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300 300
7 | 300 300 300 300 300 300 300 300 310 350 300 300 300 300 300 300 300 300 300 300
8 | 300 300 300 300 300 300 300 310 300 300 350 300 300 300 300 300 300 300 300 300
9 | 300 300 300 300 300 129 310 300 300 300 300 350 300 300 300 300 300 300 300 300
10 | 300 300 300 300 300 310 300 300 300 300 300 300 350 300 300 300 300 300 300 300
11 | 300 300 310 350 310 300 300 300 300 306 307 300 300 350 300 300 300 300 300 300
12 | 300 368 369 300 0 1 300 306 307 305 300 300 300 300 350 300 300 300 300 300
13 | 310 370 371 300 16 17 300 305 300 305 300 300 300 300 300 350 300 300 300 300
14 | 352 352 352 352 352 352 352 352 352 352 352 352 352 352 352 352 352 352 352 352
15 | 353 353 353 353 353 353 353 353 353 353 353 353 353 353 353 353 353 353 353 353
原创文章,作者:研究院精选,如若转载,请注明出处:https://www.pmtemple.com/academy/10772/