Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if key == WindowEvent.PRESS_ARROW_RIGHT:
self.directional = reset_bit(self.directional, P10)
elif key == WindowEvent.PRESS_ARROW_LEFT:
self.directional = reset_bit(self.directional, P11)
elif key == WindowEvent.PRESS_ARROW_UP:
self.directional = reset_bit(self.directional, P12)
elif key == WindowEvent.PRESS_ARROW_DOWN:
self.directional = reset_bit(self.directional, P13)
elif key == WindowEvent.PRESS_BUTTON_A:
self.standard = reset_bit(self.standard, P10)
elif key == WindowEvent.PRESS_BUTTON_B:
self.standard = reset_bit(self.standard, P11)
elif key == WindowEvent.PRESS_BUTTON_SELECT:
self.standard = reset_bit(self.standard, P12)
elif key == WindowEvent.PRESS_BUTTON_START:
self.standard = reset_bit(self.standard, P13)
elif key == WindowEvent.RELEASE_ARROW_RIGHT:
self.directional = set_bit(self.directional, P10)
elif key == WindowEvent.RELEASE_ARROW_LEFT:
self.directional = set_bit(self.directional, P11)
elif key == WindowEvent.RELEASE_ARROW_UP:
self.directional = set_bit(self.directional, P12)
elif key == WindowEvent.RELEASE_ARROW_DOWN:
self.directional = set_bit(self.directional, P13)
elif key == WindowEvent.RELEASE_BUTTON_A:
self.standard = set_bit(self.standard, P10)
elif key == WindowEvent.RELEASE_BUTTON_B:
self.standard = set_bit(self.standard, P11)
elif key == WindowEvent.RELEASE_BUTTON_SELECT:
timer_div (int): Replace timer's DIV register with this value. Use `None` to randomize.
"""
PyBoyGameWrapper.start_game(self, timer_div=timer_div)
# Boot screen
while True:
self.pyboy.tick()
self.tilemap_background.refresh_lcdc()
if self.tilemap_background[0:3, 16] == [231, 224, 235]: # 'HAL' on the first screen
break
# Wait for transition to finish (start screen)
for _ in range(25):
self.pyboy.tick()
self.pyboy.send_input(WindowEvent.PRESS_BUTTON_START)
self.pyboy.tick()
self.pyboy.send_input(WindowEvent.RELEASE_BUTTON_START)
# Wait for transition to finish (exit start screen, enter level intro screen)
for _ in range(60):
self.pyboy.tick()
# Skip level intro
self.pyboy.send_input(WindowEvent.PRESS_BUTTON_START)
self.pyboy.tick()
self.pyboy.send_input(WindowEvent.RELEASE_BUTTON_START)
# Wait for transition to finish (exit level intro screen, enter game)
for _ in range(60):
self.pyboy.tick()
try:
from cython import compiled
cythonmode = compiled
except ImportError:
cythonmode = False
# https://wiki.libsdl.org/SDL_Scancode#Related_Enumerations
# yapf: disable
KEY_DOWN = {
sdl2.SDLK_UP : WindowEvent.PRESS_ARROW_UP,
sdl2.SDLK_DOWN : WindowEvent.PRESS_ARROW_DOWN,
sdl2.SDLK_RIGHT : WindowEvent.PRESS_ARROW_RIGHT,
sdl2.SDLK_LEFT : WindowEvent.PRESS_ARROW_LEFT,
sdl2.SDLK_a : WindowEvent.PRESS_BUTTON_A,
sdl2.SDLK_s : WindowEvent.PRESS_BUTTON_B,
sdl2.SDLK_RETURN : WindowEvent.PRESS_BUTTON_START,
sdl2.SDLK_BACKSPACE : WindowEvent.PRESS_BUTTON_SELECT,
sdl2.SDLK_SPACE : WindowEvent.PRESS_SPEED_UP,
sdl2.SDLK_COMMA : WindowEvent.PRESS_REWIND_BACK,
sdl2.SDLK_PERIOD : WindowEvent.PRESS_REWIND_FORWARD,
}
KEY_UP = {
sdl2.SDLK_UP : WindowEvent.RELEASE_ARROW_UP,
sdl2.SDLK_DOWN : WindowEvent.RELEASE_ARROW_DOWN,
sdl2.SDLK_RIGHT : WindowEvent.RELEASE_ARROW_RIGHT,
sdl2.SDLK_LEFT : WindowEvent.RELEASE_ARROW_LEFT,
sdl2.SDLK_a : WindowEvent.RELEASE_BUTTON_A,
sdl2.SDLK_s : WindowEvent.RELEASE_BUTTON_B,
sdl2.SDLK_RETURN : WindowEvent.RELEASE_BUTTON_START,
sdl2.SDLK_BACKSPACE : WindowEvent.RELEASE_BUTTON_SELECT,
sdl2.SDLK_z : WindowEvent.STATE_SAVE,
def reset_game(self, timer_div=None):
"""
After calling `start_game`, you can call this method at any time to reset the game.
Kwargs:
timer_div (int): Replace timer's DIV register with this value. Use `None` to randomize.
"""
PyBoyGameWrapper.reset_game(self, timer_div=timer_div)
self.pyboy.send_input(WindowEvent.PRESS_BUTTON_START)
self.pyboy.tick()
self.pyboy.send_input(WindowEvent.RELEASE_BUTTON_START)
for _ in range(6):
self.pyboy.tick()
"""
PyBoyGameWrapper.start_game(self, timer_div=timer_div)
if world_level is not None:
self.set_world_level(*world_level)
# Boot screen
while True:
self.pyboy.tick()
if self.tilemap_background[6:11, 13] == [284, 285, 266, 283, 285]: # "START" on the main menu
break
self.pyboy.tick()
self.pyboy.tick()
self.pyboy.tick()
self.pyboy.send_input(WindowEvent.PRESS_BUTTON_START)
self.pyboy.tick()
self.pyboy.send_input(WindowEvent.RELEASE_BUTTON_START)
while True:
if unlock_level_select and self.pyboy.frame_count == 71: # An arbitrary frame count, where the write will work
self.pyboy.set_memory_value(ADDR_WIN_COUNT, 2 if unlock_level_select else 0)
break
self.pyboy.tick()
self.tilemap_background.refresh_lcdc()
# "MARIO" in the title bar and 0 is placed at score
if self.tilemap_background[0:5, 0] == [278, 266, 283, 274, 280] and \
self.tilemap_background[5, 1] == 256:
self.game_has_started = True
break
self.events.append(WindowEvent(WindowEvent.SCREENSHOT_RECORD))
else:
if c == "a":
self.events.append(WindowEvent(WindowEvent.PRESS_BUTTON_A))
elif c == "s":
self.events.append(WindowEvent(WindowEvent.PRESS_BUTTON_B))
elif c == chr(27):
self.events.append(WindowEvent(WindowEvent.QUIT))
elif c == " ":
self.events.append(WindowEvent(WindowEvent.PRESS_SPEED_UP))
elif c == "i":
self.events.append(WindowEvent(WindowEvent.SCREEN_RECORDING_TOGGLE))
elif c == chr(8):
self.events.append(WindowEvent(WindowEvent.PRESS_BUTTON_SELECT))
elif c == chr(13):
self.events.append(WindowEvent(WindowEvent.PRESS_BUTTON_START))
PyBoyGameWrapper.start_game(self)
# Boot screen
while True:
self.pyboy.tick()
self.tilemap_background.refresh_lcdc()
if self.tilemap_background[2:9, 14] == [89, 25, 21, 10, 34, 14, 27]: # '1PLAYER' on the first screen
break
# Start game. Just press Start when the game allows us.
for i in range(3):
if i == 2:
PyBoyGameWrapper._set_timer_div(self, timer_div)
self.saved_state.seek(0)
self.pyboy.save_state(self.saved_state)
self.pyboy.send_input(WindowEvent.PRESS_BUTTON_START)
self.pyboy.tick()
self.pyboy.send_input(WindowEvent.RELEASE_BUTTON_START)
for _ in range(6):
self.pyboy.tick()
self.game_has_started = True
raise TypeError("pyboy must be a Pyboy object")
# Build game_wrapper
self.game_wrapper = pyboy.game_wrapper()
if self.game_wrapper is None:
raise ValueError(
"You need to build a game_wrapper to use this function. Otherwise there is no way to build a reward function automaticaly."
)
self.last_fitness = self.game_wrapper.fitness
# Building the action_space
self._DO_NOTHING = WindowEvent.PASS
self._buttons = [
WindowEvent.PRESS_ARROW_UP, WindowEvent.PRESS_ARROW_DOWN, WindowEvent.PRESS_ARROW_RIGHT,
WindowEvent.PRESS_ARROW_LEFT, WindowEvent.PRESS_BUTTON_A, WindowEvent.PRESS_BUTTON_B,
WindowEvent.PRESS_BUTTON_SELECT, WindowEvent.PRESS_BUTTON_START
]
self._button_is_pressed = {button: False for button in self._buttons}
self._buttons_release = [
WindowEvent.RELEASE_ARROW_UP, WindowEvent.RELEASE_ARROW_DOWN, WindowEvent.RELEASE_ARROW_RIGHT,
WindowEvent.RELEASE_ARROW_LEFT, WindowEvent.RELEASE_BUTTON_A, WindowEvent.RELEASE_BUTTON_B,
WindowEvent.RELEASE_BUTTON_SELECT, WindowEvent.RELEASE_BUTTON_START
]
self._release_button = {button: r_button for button, r_button in zip(self._buttons, self._buttons_release)}
self.actions = [self._DO_NOTHING] + self._buttons
if action_type == "all":
self.actions += self._buttons_release
elif action_type not in ["press", "toggle"]:
raise ValueError(f"action_type {action_type} is invalid")
self.action_type = action_type
break
# Wait for transition to finish (start screen)
for _ in range(25):
self.pyboy.tick()
self.pyboy.send_input(WindowEvent.PRESS_BUTTON_START)
self.pyboy.tick()
self.pyboy.send_input(WindowEvent.RELEASE_BUTTON_START)
# Wait for transition to finish (exit start screen, enter level intro screen)
for _ in range(60):
self.pyboy.tick()
# Skip level intro
self.pyboy.send_input(WindowEvent.PRESS_BUTTON_START)
self.pyboy.tick()
self.pyboy.send_input(WindowEvent.RELEASE_BUTTON_START)
# Wait for transition to finish (exit level intro screen, enter game)
for _ in range(60):
self.pyboy.tick()
self.game_has_started = True
self.saved_state.seek(0)
self.pyboy.save_state(self.saved_state)