「」用Python写Gameboy模拟器,还能训练AI模型:丹麦小哥大学项目火了( 三 )


import osimport sysfrom pyboy import PyBoy, WindowEvent# Makes us able to import PyBoy from the directory belowfile_path = os.path.dirname(os.path.realpath(__file__))sys.path.insert(0, file_path + ''/..'')# Check if the ROM is given through argvif len(sys.argv) > 1: filename = sys.argv[1]else: print(''Usage: python mario_boiler_plate.py [ROM file]'') exit(1)
quiet = ''--quiet'' in sys.argvpyboy = 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 == 0assert mario.lives_left == 2assert mario.time_left == 400assert mario.world == (1, 1)assert mario.fitness == 0 # A built-in fitness score for AI developmentlast_fitness = 0print(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 == 27700assert mario.fitness == 17700 # Loosing a live, means 10.000 points in this fitness scoringprint(mario) breakelse: print(''Mario didn't die?'') exit(2)
mario.reset_game()assert mario.lives_left == 2
pyboy.stop()
如果你在加载了 Super Mario Land ROM 的情况下运行上述代码 , 则将在下面得到图片和终端输出 。 值得注意的是 , Mario 的形态显示为索引 0 , 1 , 16 , 17 。

「」用Python写Gameboy模拟器,还能训练AI模型:丹麦小哥大学项目火了
本文插图


「」用Python写Gameboy模拟器,还能训练AI模型:丹麦小哥大学项目火了
本文插图

作者简介
该项目的作者 Asger Anders Lund Hansen、Mads Ynddal 和 Troels Ynddal 均来自丹麦 。 毕业于丹麦哥本哈根大学的 Mads Ynddal 表示 , 事实上这一 Gameboy 模拟器可以追溯到 2015 年他在大学期间的项目 。
「」用Python写Gameboy模拟器,还能训练AI模型:丹麦小哥大学项目火了
本文插图

Gameboy 模拟器的的 1.0 版发布了 , 但对于开发者们来说还有很多事可以去做 。 项目研发者表示 , 目前可以推进的方向包括为模拟器加入声音、彩色、Gameboy 模拟连线 , 以及更多游戏的封装 , 当然还有在其之上训练神经网络的示例 。
希望在人们的努力下 , Gameboy 中的游戏也能重获新生 。 更重要的是 , 它现在还有了训练人工智能的任务 。


推荐阅读