import os
import sys
from typing import cast
file_path = os.path.dirname(os.path.realpath(__file__))
py_path = os.path.abspath(file_path + "/../../../res/extern/PyBoy")
sys.path.insert(0, py_path)
from boytacean.pyboy import PyBoy, WindowEvent
from pyboy.plugins.game_wrapper_tetris import GameWrapperTetris
if len(sys.argv) > 1:
filename = sys.argv[1]
else:
print("Usage: python gamewrapper_tetris.py [ROM file]")
exit(1)
quiet = True
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() == "TETRIS"
tetris = pyboy.game_wrapper()
if tetris is None:
print("Game Wrapper not enabled")
exit(1)
tetris = cast(GameWrapperTetris, tetris)
tetris.start_game(timer_div=0x00)
tetromino_at_0x00 = tetris.next_tetromino()
assert tetromino_at_0x00 == "L", tetris.next_tetromino()
assert tetris.score == 0
assert tetris.level == 0
assert tetris.lines == 0
assert tetris.fitness == 0
tetris.reset_game(timer_div=0x00)
assert tetris.next_tetromino() == tetromino_at_0x00, tetris.next_tetromino()
blank_tile = 47
first_brick = False
for frame in range(
1000
): pyboy.tick()
if frame % 2 == 0: pyboy.send_input(WindowEvent.PRESS_ARROW_RIGHT)
elif frame % 2 == 1: pyboy.send_input(WindowEvent.RELEASE_ARROW_RIGHT)
game_area = tetris.game_area()
if not first_brick and any(filter(lambda x: x != blank_tile, game_area[-1, :])):
first_brick = True
print("First brick touched the bottom!")
print(tetris)
print("Final game board mask:")
print(tetris)
assert tetris.score == 0
assert tetris.level == 0
assert tetris.lines == 0
assert tetris.fitness == 0
assert any(filter(lambda x: x != blank_tile, game_area[-1, :]))
tetris.reset_game(timer_div=0x00)
assert tetris.next_tetromino() == tetromino_at_0x00, tetris.next_tetromino()
tetris.reset_game(timer_div=0x00) import time
time.sleep(10)
assert tetris.next_tetromino() == tetromino_at_0x00, tetris.next_tetromino()
assert all(filter(lambda x: x != blank_tile, game_area[-1, :]))
tetris.reset_game(timer_div=0x55) assert tetris.next_tetromino() != tetromino_at_0x00, tetris.next_tetromino()
selection = set()
for _ in range(10):
tetris.reset_game()
selection.add(tetris.next_tetromino())
assert len(selection) > 1
pyboy.stop()