Ejemplo n.º 1
0
    def compute_key(self, key):
        tmp_direction = Direction.NULL
        if key == pygame.K_ESCAPE:
            return False
        elif key == pygame.K_LEFT:
            tmp_direction = Direction.LEFT
        elif key == pygame.K_RIGHT:
            tmp_direction = Direction.RIGHT
        elif key == pygame.K_UP:
            tmp_direction = Direction.UP
        elif key == pygame.K_DOWN:
            tmp_direction = Direction.DOWN

        # Make sure that the new direction is valid
        # (the snake cannot go back on UP if it was going DOWN)
        if Direction.is_movement_valid(self.player.previous_direction,
                                       tmp_direction):
            self.player.direction = tmp_direction

        return True