Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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:
self.standard = set_bit(self.standard, P12)
elif key == WindowEvent.RELEASE_BUTTON_START:
self.standard = set_bit(self.standard, P13)
# XOR to find the changed bits, AND it to see if it was high before.
# Test for both directional and standard buttons.
return ((_directional ^ self.directional) & _directional) or \
((_standard ^ self.standard) & _standard)
# 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
def _glkeyboard(self, c, x, y, up):
if up:
if c == "a":
self.events.append(WindowEvent(WindowEvent.RELEASE_BUTTON_A))
elif c == "s":
self.events.append(WindowEvent(WindowEvent.RELEASE_BUTTON_B))
elif c == "z":
self.events.append(WindowEvent(WindowEvent.STATE_SAVE))
elif c == "x":
self.events.append(WindowEvent(WindowEvent.STATE_LOAD))
elif c == " ":
self.events.append(WindowEvent(WindowEvent.RELEASE_SPEED_UP))
elif c == chr(8):
self.events.append(WindowEvent(WindowEvent.RELEASE_BUTTON_SELECT))
elif c == chr(13):
self.events.append(WindowEvent(WindowEvent.RELEASE_BUTTON_START))
elif c == "o":
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.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
if simultaneous_actions:
raise NotImplementedError("Not implemented yet, raise an issue on GitHub if needed")
else:
self.action_space = Discrete(len(self.actions))
# Building the observation_space
# 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)
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()
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.saved_state.seek(0)
self.pyboy.save_state(self.saved_state)
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,
sdl2.SDLK_x : WindowEvent.STATE_LOAD,
sdl2.SDLK_SPACE : WindowEvent.RELEASE_SPEED_UP,
sdl2.SDLK_p : WindowEvent.PAUSE_TOGGLE,
sdl2.SDLK_i : WindowEvent.SCREEN_RECORDING_TOGGLE,
sdl2.SDLK_o : WindowEvent.SCREENSHOT_RECORD,
sdl2.SDLK_ESCAPE : WindowEvent.QUIT,
sdl2.SDLK_COMMA : WindowEvent.RELEASE_REWIND_BACK,
sdl2.SDLK_PERIOD : WindowEvent.RELEASE_REWIND_FORWARD,
}
# yapf: enable
def sdl2_event_pump(events):
# Feed events into the loop
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()
self.game_has_started = True