Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_screen_buffer_and_image():
cformat = "RGBA"
boot_logo_hash_predigested = b"=\xff\xf9z 6\xf0\xe9\xcb\x05J`PM5\xd4rX+\x1b~z\xef1\xe0\x82\xc4t\x06\x82\x12C"
boot_logo_hash_predigested = \
b"s\xd1R\x88\xe0a\x14\xd0\xd2\xecOk\xe8b\xae.\x0e\x1e\xb6R\xc2\xe9:\xa2\x0f\xae\xa2\x89M\xbf\xd8|"
window = "headless"
pyboy = PyBoy(any_rom, window_type=window, window_scale=1, bootrom_file=boot_rom, disable_input=True)
pyboy.set_emulation_speed(0)
for n in range(275): # Iterate to boot logo
pyboy.tick()
assert pyboy.botsupport_manager().screen().raw_screen_buffer_dims() == (160, 144)
assert pyboy.botsupport_manager().screen().raw_screen_buffer_format() == cformat
boot_logo_hash = hashlib.sha256()
boot_logo_hash.update(pyboy.botsupport_manager().screen().raw_screen_buffer())
assert boot_logo_hash.digest() == boot_logo_hash_predigested
assert isinstance(pyboy.botsupport_manager().screen().raw_screen_buffer(), bytes)
# The output of `screen_image` is supposed to be homogeneous, which means a shared hash between versions.
boot_logo_png_hash_predigested = (
b"\x1b\xab\x90r^\xfb\x0e\xef\xf1\xdb\xf8\xba\xb6:^\x01"
b"\xa4\x0eR&\xda9\xfcg\xf7\x0f|\xba}\x08\xb6$"
def test_observation_type_minimal(self):
pyboy = PyBoy(supermarioland_rom, window_type="dummy", game_wrapper=True)
pyboy.set_emulation_speed(0)
env = pyboy.openai_gym(observation_type="minimal")
observation = env.reset()
expected_observation = np.zeros_like(observation)
expected_observation[-4:-2, 4:6] = 1 # Mario
expected_observation[-2:, :] = 3 # Ground
expected_observation[-4:-2, 1:3] = 3 # Pipe
expected_observation[9, 5] = 3 # ? Block
print(observation)
print(expected_observation)
assert np.all(observation == expected_observation)
def test_misc():
pyboy = PyBoy(any_rom, window_type="dummy", bootrom_file=boot_rom, disable_input=True)
pyboy.tick()
pyboy.stop(save=False)
window="headless",
verify=True,
record_gif=None,
gif_destination=None,
rewind=False,
bootrom_file=utils.boot_rom,
overwrite=RESET_REPLAYS,
gif_hash=None
):
with open(replay, "rb") as f:
recorded_input, b64_romhash, b64_state = json.loads(zlib.decompress(f.read()).decode("ascii"))
verify_file_hash(ROM, b64_romhash)
state_data = io.BytesIO(base64.b64decode(b64_state.encode("utf8"))) if b64_state is not None else None
pyboy = PyBoy(
ROM,
window_type=window,
bootrom_file=bootrom_file,
disable_input=True,
rewind=rewind,
record_input=(RESET_REPLAYS and window in ["SDL2", "headless", "OpenGL"])
)
# pyboy.set_emulation_speed(0)
if state_data is not None:
pyboy.load_state(state_data)
# Filters out the blacklisted events
recorded_input = list(
map(
lambda event_tuple:
(event_tuple[0], list(filter(lambda x: x not in event_filter, event_tuple[1])), event_tuple[2]),
def test_tilemap_position_list():
pyboy = PyBoy(supermarioland_rom, window_type="headless", disable_input=True)
for _ in range(100):
pyboy.tick()
# Start the game
pyboy.send_input(WindowEvent.PRESS_BUTTON_START)
pyboy.tick()
pyboy.send_input(WindowEvent.RELEASE_BUTTON_START)
# Move right for 100 frame
pyboy.send_input(WindowEvent.PRESS_ARROW_RIGHT)
for _ in range(100):
pyboy.tick()
# Get screen positions, and verify the values
positions = pyboy.botsupport_manager().screen().tilemap_position_list()
for y in range(1, 16):
print("ROM folder not found. Please copy the Game-ROM to '%s'" % ROMdir)
exit()
try:
# Check if the ROM is given through argv
if len(sys.argv) > 1: # First arg is SDL2/PyGame
filename = sys.argv[1]
else:
filename = getROM(ROMdir)
# Start PyBoy and run loop
if "--quiet" in sys.argv:
window = 'headless'
else:
window = 'SDL2'
pyboy = PyBoy(filename, window_type=window, window_scale=3, bootrom_file=bootROM)
pyboy.set_emulation_speed(0)
print("Screen pos:", pyboy.get_screen_position())
first_brick = False
tile_map = pyboy.get_window_tile_map()
for frame in range(5282): # Enough frames to get a "Game Over". Otherwise do: `while not pyboy.tick():`
pyboy.tick()
# print ("frame:", frame)
# Start game. Just press Start and A when the game allows us.
# The frames are not 100% accurate.
if frame == 144:
pyboy.send_input(windowevent.PRESS_BUTTON_START)
elif frame == 145:
pyboy.send_input(windowevent.RELEASE_BUTTON_START)
elif frame == 152:
| --- | --- |
| Escape | Quit |
| D | Debug |
| Space | Unlimited FPS |
| Z | Save state |
| X | Load state |
| I | Toggle screen recording |
| , | Rewind backwards |
| . | Rewind forward |
See "pyboy --help" for how to enable rewind and other awesome features!
"""
)
# Start PyBoy and run loop
pyboy = PyBoy(argv.ROM, **vars(argv))
if argv.loadstate is not None:
if argv.loadstate == INTERNAL_LOADSTATE:
# Guess filepath from ROM path
state_path = argv.ROM + ".state"
else:
# Use filepath given
state_path = argv.loadstate
valid_file_path(state_path)
with open(state_path, "rb") as f:
pyboy.load_state(f)
while not pyboy.tick():
pass
#
# License: See LICENSE file
# GitHub: https://github.com/Baekalfen/PyBoy
#
import sys
from pprint import pprint
from pyboy import PyBoy
hide_window = "--quiet" in sys.argv
pyboy = PyBoy(
"ROMs/POKEMON BLUE.gb",
debugging=False,
disable_input=True,
# window_type="headless", # For unattended use, for example machine learning
hide_window=hide_window,
)
pyboy.set_emulation_speed(0)
for n in range(1000): # Move ahead the desired number of frames.
pyboy.tick()
tile_map = pyboy.get_window_tile_map() # Get the TileView object for the window.
# The following prints out the indices of each tile in the window -- excluding sprites and the background!
print(tile_map)
# Tile Map Address: 0x9c00, Signed Tile Data: No
def test_rom(rom):
logger.info(rom)
pyboy = PyBoy("dummy", 1, rom, "ROMs/DMG_ROM.bin")
# pyboy = PyBoy("SDL2", 1, rom, "ROMs/DMG_ROM.bin")
pyboy.disableTitle()
pyboy.setEmulationSpeed(False)
serial_output = ""
t = time.time()
result = None
while not pyboy.tick():
b = pyboy.getSerial()
if b != "":
serial_output += b
# print b,
t = time.time()
if "Passed" in serial_output:
result = ("Passed")
break