Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
This should only be called when the game has started.
Args:
amount (int): The wanted number of lives
"""
if not self.game_has_started:
logger.warning("Please call set_lives_left after starting the game")
if 0 <= amount <= 99:
tens = amount // 10
ones = amount % 10
self.pyboy.set_memory_value(ADDR_LIVES_LEFT, (tens << 4) | ones)
self.pyboy.set_memory_value(ADDR_LIVES_LEFT_DISPLAY, tens)
self.pyboy.set_memory_value(ADDR_LIVES_LEFT_DISPLAY + 1, ones)
else:
logger.error(f"{amount} is out of bounds. Only values between 0 and 99 allowed.")
window = SDLWindow(scale)
elif window_type == "scanline":
from .window_scanline import ScanlineWindow
window = ScanlineWindow(scale)
elif window_type == "OpenGL":
from .window_opengl import OpenGLWindow
window = OpenGLWindow(scale)
elif window_type == "dummy":
from .window_dummy import DummyWindow
window = DummyWindow(scale)
elif window_type == "headless":
from .window_headless import HeadlessWindow
window = HeadlessWindow(scale)
else:
logger.error("Invalid arguments! Usage: pypy main.py [Window] [ROM path]")
logger.error("Valid Windows are: 'SDL2', 'scanline', 'OpenGL', 'headless', and 'dummy'")
exit(1)
window.init(hide_window)
return window
from .window_sdl2 import SDLWindow
window = SDLWindow(scale)
elif window_type == "scanline":
from .window_scanline import ScanlineWindow
window = ScanlineWindow(scale)
elif window_type == "OpenGL":
from .window_opengl import OpenGLWindow
window = OpenGLWindow(scale)
elif window_type == "dummy":
from .window_dummy import DummyWindow
window = DummyWindow(scale)
elif window_type == "headless":
from .window_headless import HeadlessWindow
window = HeadlessWindow(scale)
else:
logger.error("Invalid arguments! Usage: pypy main.py [Window] [ROM path]")
logger.error("Valid Windows are: 'SDL2', 'scanline', 'OpenGL', 'headless', and 'dummy'")
exit(1)
window.init(hide_window)
return window
def enabled(self):
if self.pyboy_argv.get("window_type") == "OpenGL":
if opengl_enabled:
return True
else:
logger.error("Missing depencency \"PyOpenGL\". OpenGL window disabled")
return False
if path is None:
directory = os.path.join(os.path.curdir, "recordings")
if not os.path.exists(directory):
os.makedirs(directory, mode=0o755)
path = os.path.join(directory, time.strftime(f"{self.gamename}-%Y.%m.%d-%H.%M.%S.gif"))
if self.frames:
self.frames[0].save(path, save_all=True, interlace=False,
loop=0, optimize=True,
append_images=self.frames[1:],
duration=int(round(1000 / fps, -1)))
logger.info("Screen recording saved in {}".format(path))
else:
logger.error("Screen recording failed: no frames")
# This feeds events into the tick-loop from the window. There might already be events in the list from the API.
events = self.plugin_manager.handle_events(events)
for event in events:
if event == WindowEvent.QUIT:
self.done = True
elif event == WindowEvent.RELEASE_SPEED_UP:
# Switch between unlimited and 1x real-time emulation speed
self.target_emulationspeed = int(bool(self.target_emulationspeed) ^ True)
logger.info("Speed limit: %s" % self.target_emulationspeed)
elif event == WindowEvent.STATE_SAVE:
with open(self.gamerom_file + ".state", "wb") as f:
self.mb.save_state(IntIOWrapper(f))
elif event == WindowEvent.STATE_LOAD:
state_path = self.gamerom_file + ".state"
if not os.path.isfile(state_path):
logger.error(f"State file not found: {state_path}")
continue
with open(state_path, "rb") as f:
self.mb.load_state(IntIOWrapper(f))
elif event == WindowEvent.PASS:
pass # Used in place of None in Cython, when key isn't mapped to anything
elif event == WindowEvent.PAUSE_TOGGLE:
if self.paused:
self._unpause()
else:
self._pause()
elif event == WindowEvent.PAUSE:
self._pause()
elif event == WindowEvent.UNPAUSE:
self._unpause()
elif event == WindowEvent._INTERNAL_RENDERER_FLUSH:
self.plugin_manager._post_tick_windows()
action_type (str): Define how the agent will interact with button inputs
* `"press"`: The agent will only press inputs for 1 frame an then release it.
* `"toggle"`: The agent will toggle inputs, first time it press and second time it release.
* `"all"`: The agent have acces to all inputs, press and release are separated.
simultaneous_actions (bool): Allow to inject multiple input at once. This dramatically increases the action_space: \\(n \\rightarrow 2^n\\)
Returns
-------
`pyboy.openai_gym.PyBoyGymEnv`:
A Gym environment based on the `Pyboy` object.
"""
if gym_enabled:
return PyBoyGymEnv(self, observation_type, action_type, simultaneous_actions, **kwargs)
else:
logger.error(f"{__name__}: Missing dependency \"gym\". ")
return None
path = os.path.join(directory, time.strftime(f"{self.pyboy.cartridge_title()}-%Y.%m.%d-%H.%M.%S.gif"))
if len(self.frames) > 0:
self.frames[0].save(
path,
save_all=True,
interlace=False,
loop=0,
optimize=True,
append_images=self.frames[1:],
duration=int(round(1000 / fps, -1))
)
logger.info("Screen recording saved in {}".format(path))
else:
logger.error("Screen recording failed: no frames")
self.frames = []