コード例 #1
0
ファイル: maze.py プロジェクト: malcolmhoward/laser_game
class Maze(Game):
    def __init__(
        self,
        center,
        bound,
        pwm,
        controller: PlayerController,
        player_turret: Turret,
    ):
        super().__init__(center, bound, pwm)
        self.player = Player(bound,
                             bound,
                             pwm,
                             player_turret,
                             controller,
                             initial_x=415,
                             initial_y=415,
                             x_center=375,
                             y_center=375)
        self.player.laser.on()

    def play_on(self):
        self.playing = True
        binding = {}
        prev_time = 0
        while self.playing:
            curr_time = time.time()
            if curr_time - prev_time >= self.time_rate:
                prev_time = curr_time
                x, y = self.player.manual_servo(**binding)
                binding = WALLS.check_collision(x, y)
コード例 #2
0
ファイル: maze.py プロジェクト: JosiahDub/laser_game
class Maze(Game):
    """
    Find your way to the end!
    """
    '''
    GAME INFO
    '''
    # TOUCHDOWN
    endzone = [[0, 16], [0, 16]]

    lose_time = 360

    def __init__(
        self,
        center,
        bound,
        pwm,
        controller: PlayerController,
        player_turret: Turret,
    ):
        super().__init__(center, bound, pwm)
        self.player_turret = player_turret
        self.player = Player(bound,
                             bound,
                             pwm,
                             player_turret,
                             controller,
                             initial_x=415,
                             initial_y=415,
                             x_center=375,
                             y_center=375)
        self.player.laser.on()

    def play_on(self):
        self.playing = True
        binding = {}
        prev_time = 0
        start_time = time.time()
        while self.playing:
            curr_time = time.time()
            if curr_time - prev_time >= self.time_rate:
                prev_time = curr_time
                x, y = self.player.manual_servo(**binding)
                binding = WALLS.check_collision(x, y)
                centered_x = x - self.center + self.bound / 2
                centered_y = y - self.center + self.bound / 2
                # Check if the player scored the winning goal!!!
                if self.endzone[0][0] <= centered_x <= self.endzone[0][1] and \
                        self.endzone[1][0] <= centered_y <= self.endzone[1][1]:
                    self.win()
                    self.playing = False
                if curr_time - start_time >= self.lose_time:
                    self.playing = False

    def win(self):
        # Free up the GPIO pin
        del self.player.laser
        npc = NPC(self.pwm, self.player_turret)
        radius = 20
        rate = 0.1
        circle = Circle(375, 375, radius, 0, rate)
        data = npc.follow_path(circle.data())
        data.__next__()
        data.__next__()
        npc.laser.on()
        for _ in range(0, 600):
            if _ % 100 == 0:
                circle.clockwise = False
            data.__next__()