Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def set_lives_left(self, amount):
"""
Set the amount lives to any number between 0 and 99.
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.")
raise Exception("OpenGL couldn't initialize!")
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA)
glutInitWindowSize(*self._scaledresolution)
glutCreateWindow("PyBoy")
glutKeyboardFunc(self._key)
glutKeyboardUpFunc(self._keyUp)
glutSpecialFunc(self._spec)
glutSpecialUpFunc(self._specUp)
self.events = []
glPixelZoom(self._scale, self._scale)
glutReshapeFunc(self._glreshape)
glutDisplayFunc(self._gldraw)
if hide_window:
logger.warning("Hiding the window is not supported in OpenGL")
def enabled(self):
if Image is None:
logger.warning(f"{__name__}: Missing dependency \"Pillow\". Screenshots disabled")
return False
return True
def get_screen_image(self):
if not Image:
logger.warning("Cannot generate screen image. Missing dependency \"Pillow\".")
return None
return Image.frombytes(
self.color_format,
self.buffer_dims,
self.get_screen_buffer())
def enabled(self):
if Image is None:
logger.warning(f"{__name__}: Missing dependency \"Pillow\". Recording disabled")
return False
return True
def save(self, path=None, fps=60):
if not Image:
logger.warning("ScreenRecorder: No recording to save. Missing dependency \"Pillow\".")
return
logger.info("ScreenRecorder saving...")
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)))
def get_screen_image(self):
if not Image:
logger.warning("Cannot generate screen image. Missing dependency \"Pillow\".")
return None
# Convert to RGBA for consistency with SDL2
return Image.fromarray(np.frombuffer(self.get_screen_buffer(), dtype=np.uint8).reshape(
self.buffer_dims+(4,))[:, :, 1:], self.color_format).convert(mode='RGBA')
def set_emulation_speed(self, target_speed):
"""
Set the target emulation speed. It might loose accuracy of keeping the exact speed, when using a high
`target_speed`.
The speed is defined as a multiple of real-time. I.e `target_speed=2` is double speed.
A `target_speed` of `0` means unlimited. I.e. fastest possible execution.
Args:
target_speed (int): Target emulation speed as multiplier of real-time.
"""
if target_speed > 5:
logger.warning("The emulation speed might not be accurate when speed-target is higher than 5")
self.target_emulationspeed = target_speed
def __init__(self, gamename):
self.gamename = gamename
if Image:
logger.info("ScreenRecorder started")
self.frames = []
else:
logger.warning("ScreenRecorder: Dependency \"Pillow\" could not be imported. "
"Screen recording is disabled.")