def on_event(self, event_type, event_data): if event_type == "keyboard_keydown" and not self.input_queue.full(): if event_data == pygame.K_UP: self.input_queue.put(Vector2(x=0, y=-1)) elif event_data == pygame.K_DOWN: self.input_queue.put(Vector2(x=0, y=1)) elif event_data == pygame.K_LEFT: self.input_queue.put(Vector2(x=-1, y=0)) elif event_data == pygame.K_RIGHT: self.input_queue.put(Vector2(x=1, y=0))
def init(self): self.free_space = set() for x in range(PLAY_AREA[0]): for y in range(PLAY_AREA[1]): self.free_space.add(Vector2(x, y)) self.head = self.create_snake_head() self.cherry = self.create_cherry()
def render(self, screen): tile_width = int( math.ceil(screen.width / self.tile_texture.get_width())) tile_height = int( math.ceil(screen.height / self.tile_texture.get_height())) for x in range(tile_width): for y in range(tile_height): draw_pos = Vector2(x=x * self.tile_texture.get_width(), y=y * self.tile_texture.get_height()) screen.blit(self.tile_texture, draw_pos)
def step(self, action): direction = Vector2(0, 0) if action == 0: direction.x = 1 elif action == 1: direction.x = -1 elif action == 2: direction.y = 1 elif action == 3: direction.y = -1 else: raise ValueError("Action not in action space") reward = self.state.step(direction) observation = self.state.encode() return observation, reward, (reward < 0), {"state": self.state}
def __init__(self): self.direction = Vector2(x=1, y=0)
def init(self): self.position = Vector2(2, 0) self.set_orientation(Orientations.UP)
def update_input(self, controller): new_direction = controller.get_direction() if new_direction == Vector2(-self.direction.x, -self.direction.y): return self.direction = controller.get_direction()
def init(self, level): self.level = level self.position = Vector2(0, 0) self.direction = Vector2(1, 0) self.length = 3
def cherry_position_lottery(self): if not self.free_space: return Vector2(0, 0) space = list(self.free_space) return self.random.choice(space)