示例#1
0
    def reset(self):
        new_snakes = []
        snake_pos = Vector(random.randrange(self.grid_size.x), random.randrange(self.grid_size.y))
        self.snakes[0].reset(snake_pos)
        new_snakes.append(self.snakes[0])
        new_snakes[0].grow_pending = True
        new_snakes[0].move()
        self.handle_border_cross(new_snakes[0])
        for snake in self.snakes[1:]:
            snake_pos = Vector(random.randrange(self.grid_size.x), random.randrange(self.grid_size.y))
            occupied_axis = [snake.head.position.y for snake in new_snakes]
            while snake_pos.y in occupied_axis:
                snake_pos.y = random.randrange(self.grid_size.y)
            snake.reset(snake_pos)
            new_snakes.append(snake)
            new_snakes[-1].grow_pending = True
            new_snakes[-1].move()
            self.handle_border_cross(new_snakes[-1])
        self.snakes = new_snakes
        apple_pos = Vector(random.randrange(self.grid_size.x), random.randrange(self.grid_size.y))
        while apple_pos in [s.head.position for s in self.snakes]:
            apple_pos = Vector(random.randrange(self.grid_size.x), random.randrange(self.grid_size.y))
        self.apple.position = apple_pos

        self.info_tracker.reset()

        self.action_frame.remove_rendering_component(self.popup)
        self.popup = None
示例#2
0
 def get_action_frame(self):
     self.action_frame = ActionFrame(
         Vector(self.grid_size.x, self.grid_size.y + 1),
         Vector(40, 40),
         [self.apple] + self.snakes + [self.info_tracker]
     )
     return self.action_frame
示例#3
0
    def stride(self):
        if self.result:
            return

        self.position += self.moving_direction
        self.distance += 1

        if self.position.x < 0:
            self.position.x = Strider.grid_size.x - 1
        elif self.position.x == Strider.grid_size.x:
            self.position.x = 0

        if self.position.y < 0:
            self.position.y = Strider.grid_size.y - 1
        elif self.position.y == Strider.grid_size.y:
            self.position.y = 0

        if self.position in Strider.destinations:
            self.result = self.distance * int(self.moving_direction)

        if self.diagonal:
            minimal_distance = None
            for i in range(1, self.distance + 1):
                if \
                        Vector((self.position.x - i * self.moving_direction.x) % Strider.grid_size.x, self.position.y) \
                        in Strider.destinations \
                        or \
                        Vector(self.position.x, (self.position.y - i * self.moving_direction.y) % Strider.grid_size.y) \
                        in Strider.destinations:
                    minimal_distance = self.distance * 2 - i
            if minimal_distance:
                self.result = minimal_distance
示例#4
0
 def get_rendering_components(self):
     if self.fat:
         return RenderPacket(ResourceManager.snake_fatbodies[self.num],
                             self.position + Vector(0, 1))
     else:
         return RenderPacket(ResourceManager.snake_bodies[self.num],
                             self.position + Vector(0, 1))
示例#5
0
    def perform_move(self, game_state):
        self.total_moves += 1
        if self.last_apple_position == game_state.apple_position:
            self.moves_without_eating += 1
        else:
            self.last_apple_position = game_state.apple_position
            self.moves_without_eating = 0

        if self.moves_without_eating > BotManager.snake_idle_moves_limit:
            self.bot_trainer.game_lost(GameResult(game_state.score, self.total_moves, True))
            self.total_moves = 0
            self.moves_without_eating = 0
            return SpecialAction.reset_game

        input_matrix = self.from_game_state_to_matrix(game_state)
        output_matrix = self.bot_trainer.perform_move(input_matrix)

        move = self.from_matrix_to_move(output_matrix)
        if move == 0:
            return [Vector(1, 0)]
        elif move == 1:
            return [Vector(-1, 0)]
        elif move == 2:
            return [Vector(0, 1)]
        elif move == 3:
            return [Vector(0, -1)]
示例#6
0
    def __init__(self, pos=Vector(0, 0), num=0):
        self.head = Head(pos, num=num)
        self.body = []
        self.grow_pending = False
        self.moving_direction = Vector(1, 0)
        self.num = num

        self.alive = True
示例#7
0
 def handle_border_cross(self, snake):
     snake_head_pos = snake.head.get_pos()
     if snake_head_pos.x < 0:
         snake.head.change_pos(Vector(self.grid_size.x - 1, snake_head_pos.y))
     elif snake_head_pos.x > self.grid_size.x - 1:
         snake.head.change_pos(Vector(0, snake_head_pos.y))
     elif snake_head_pos.y < 0:
         snake.head.change_pos(Vector(snake_head_pos.x, self.grid_size.y - 1))
     elif snake_head_pos.y > self.grid_size.y - 1:
         snake.head.change_pos(Vector(snake_head_pos.x, 0))
示例#8
0
 def __init__(self, position, length):
     self.score = 0
     self.position = position
     self.length = length
     self.displays = [
         DigitDisplay(self.position + Vector(length - 1, 0)),
         DigitDisplay(self.position + Vector(length - 2, 0)),
         DigitDisplay(self.position + Vector(length - 3, 0))
     ]
     self.displays[0].next_display = self.displays[1]
     self.displays[1].next_display = self.displays[2]
    def __init__(self, position):
        self.score = 0
        self.position = position
        self.displays = [
            DigitDisplay(self.position + Vector(3, 0)),
            DigitDisplay(self.position + Vector(2, 0), cap=6),
            DigitDisplay(self.position + Vector(0, 0))
        ]
        self.displays[0].next_display = self.displays[1]
        self.displays[1].next_display = self.displays[2]

        self.current_elapsed_time = 0
        self.registered_elapsed_time = 0
        self.start_time = time.time()
 def get_click_position(events=None):
     if not events:
         events = pygame.event.get()
     for event in events:
         if event.type == pygame.MOUSEBUTTONUP and event.button == pygame.BUTTON_LEFT:
             mouse_position = pygame.mouse.get_pos()
             return Vector(mouse_position[0], mouse_position[1])
示例#11
0
    def reset(self, head_pos):
        self.head.position = head_pos
        self.body = []
        self.grow_pending = False
        self.moving_direction = Vector(1, 0)

        self.alive = True
示例#12
0
 def call_popup(self, reason):
     self.popup = PopupAssembly.get_standard_finish_popup(
         Vector((self.grid_size.x - 11) // 2, (self.grid_size.y - 5) // 2),
         self.grid_size.x,
         reason
     )
     self.action_frame.add_rendering_component(self.popup)
示例#13
0
    def __init__(self, grid_size, amount_of_players=1):
        self.grid_size = grid_size

        self.is_multi = (amount_of_players > 1)
        self.amount_of_players = amount_of_players
        self.snakes = []
        snake_pos = Vector(random.randrange(grid_size.x), random.randrange(grid_size.y))
        self.snakes.append(Snake(snake_pos))
        self.snakes[0].grow_pending = True
        self.snakes[0].move()
        self.handle_border_cross(self.snakes[0])
        for i in range(1, amount_of_players):
            occupied_axis = [snake.head.position.y for snake in self.snakes]
            snake_pos = Vector(random.randrange(grid_size.x), random.randrange(grid_size.y))
            while snake_pos.y in occupied_axis:
                snake_pos.y = random.randrange(grid_size.y)
            self.snakes.append(Snake(snake_pos, num=i))
            self.snakes[i].grow_pending = True
            self.snakes[i].move()
            self.handle_border_cross(self.snakes[i])
        apple_pos = Vector(random.randrange(grid_size.x), random.randrange(grid_size.y))
        while apple_pos in [s.head.position for s in self.snakes]:
            apple_pos = Vector(random.randrange(grid_size.x), random.randrange(grid_size.y))
        self.apple = Apple(apple_pos)

        score_tracker = ScoreTracker(Vector(0, 0), grid_size.x)
        time_tracker = TimeTracker(Vector(0, 0))
        self.info_tracker = InfoTracker(Vector(0, 0), score_tracker, time_tracker)

        self.action_frame = None
        self.popup = None
示例#14
0
 def get_rendering_components(self):
     if self.sprite:
         button_image = self.sprite
     else:
         if self.is_marked:
             button_image = ResourceManager.snake_bodies[0]
         else:
             button_image = ResourceManager.button_inactive
     return [RenderPacket(button_image, copy.deepcopy(self.position)),
             RenderPacket(self.text_image, Vector(self.position.x + 1.5, self.position.y + 0.4))]
示例#15
0
 def handle_popup(self):
     events = pygame.event.get()
     key_events = [e for e in events if e.type == pygame.KEYDOWN]
     click = MouseActionProvider.get_click_position(events)
     if click:
         if self.popup.buttons[0].contains_position(click //
                                                    Vector(40, 40)):
             return PopupAction.return_to_menu
         elif self.popup.buttons[1].contains_position(click //
                                                      Vector(40, 40)):
             if self.popup.type == PopupType.game_ended:
                 return PopupAction.restart_game
             else:
                 return PopupAction.resume_game
     if self.popup.type == PopupType.game_pause and pygame.K_SPACE in [
             e.key for e in key_events
     ]:
         return PopupAction.resume_game
     return PopupAction.idle
示例#16
0
 def __init__(self, starting_position, end_positions, grid_size):
     Strider.destinations = end_positions
     Strider.grid_size = grid_size
     self.grid_size = grid_size
     self.striders = [
         Strider(starting_position, Vector(1, 0)),
         Strider(starting_position, Vector(-1, 0)),
         Strider(starting_position, Vector(0, 1)),
         Strider(starting_position, Vector(0, -1)),
         Strider(starting_position, Vector(1, 1)),
         Strider(starting_position, Vector(1, -1)),
         Strider(starting_position, Vector(-1, 1)),
         Strider(starting_position, Vector(-1, -1)),
     ]
    def get_standard_finish_popup(position, length, reason):

        if reason == Reason.game_pause:
            message_sprite = ResourceManager.msg_pause
            back_button = Button(position + Vector(3, 3),
                                 Vector(1, 1),
                                 False,
                                 "",
                                 sprite=ResourceManager.button_back)
            resume_button = Button(position + Vector(7, 3),
                                   Vector(1, 1),
                                   False,
                                   "",
                                   sprite=ResourceManager.button_resume)

            return Popup(PopupType.game_pause, position, Vector(length, 5),
                         message_sprite, [back_button, resume_button])

        back_button = Button(position + Vector(3, 3),
                             Vector(1, 1),
                             False,
                             "",
                             sprite=ResourceManager.button_back)
        retry_button = Button(position + Vector(7, 3),
                              Vector(1, 1),
                              False,
                              "",
                              sprite=ResourceManager.button_retry)

        if reason == Reason.game_lost:
            message_sprite = ResourceManager.msg_lose
        elif reason == Reason.game_won:
            message_sprite = ResourceManager.msg_win
        elif reason == Reason.tie:
            message_sprite = ResourceManager.msg_tie
        else:
            message_sprite = ResourceManager.msg_px_wins[reason]

        return Popup(PopupType.game_ended, position, Vector(length, 5),
                     message_sprite, [back_button, retry_button])
示例#18
0
 def get_rendering_components(self):
     return \
         [RenderPacket(ResourceManager.colon, self.position + Vector(1, 0))] + \
         self.displays[0].get_rendering_components() + \
         self.displays[1].get_rendering_components() + \
         self.displays[2].get_rendering_components()
示例#19
0
 def get_rendering_components(self):
     return [
         RenderPacket(self.text_image,
                      Vector(self.position.x + 0.3, self.position.y + 0.4))
     ]
示例#20
0
 def __init__(self, position=Vector(0, 0), num=0):
     self.position = position
     self.fat = False
     self.num = num
示例#21
0
 def place_new_apple(self):
     potential_apple_pos = Vector(random.randrange(self.grid_size.x), random.randrange(self.grid_size.y))
     slots_occupied_by_snakes = [a for s in self.snakes for a in s.get_slots_occupied_by_snake()]
     while potential_apple_pos in slots_occupied_by_snakes:
         potential_apple_pos = Vector(random.randrange(self.grid_size.x), random.randrange(self.grid_size.y))
     self.apple.change_pos(potential_apple_pos)
 def get_action_frame(self):
     return ActionFrame(Vector(11, 18), self.node_size, self.buttons)
示例#23
0
 def get_rendering_components(self):
     return RenderPacket(ResourceManager.snake_heads[self.num],
                         self.position + Vector(0, 1), self.rotation)
示例#24
0
def test():

    Strider.grid_size = Vector(5, 5)
    Strider.destinations = [Vector(2, 2)]

    # correct distance in a line
    strider = Strider(Vector(0, 2), Vector(1, 0))
    strider.stride()
    strider.stride()
    strider.stride()
    print("Distance from (0, 2) to (2, 2) right is 2:", strider.result == 2)

    # same backwards
    strider = Strider(Vector(0, 2), Vector(-1, 0))
    strider.stride()
    strider.stride()
    strider.stride()
    strider.stride()
    print("Distance from (0, 2) to (2, 2) left is 3:", strider.result == 3)

    # correct distance in diagonal
    strider = Strider(Vector(0, 0), Vector(1, 1))
    strider.stride()
    strider.stride()
    strider.stride()
    print("Distance from (0, 0) to (2, 2) diagonal is 4:", strider.result == 4)

    # same backwards
    strider = Strider(Vector(0, 0), Vector(-1, -1))
    strider.stride()
    strider.stride()
    strider.stride()
    strider.stride()
    print("Distance from (0, 0) to (2, 2) diagonal backwards is 6:",
          strider.result == 6)

    strider = Strider(Vector(0, 1), Vector(1, 1))
    strider.stride()
    strider.stride()
    strider.stride()
    strider.stride()
    print("Distance from (0, 1) to (2, 2) diagonal is 3:", strider.result == 3)
    def __init__(self):
        self.node_size = Vector(40, 40)

        self.game_mode_text = Text(Vector(0, 0), "Game mode")
        self.single_player_button = Button(Vector(1, 1), Vector(1, 1), True,
                                           "Single player")
        self.two_player_button = Button(Vector(1, 2), Vector(1, 1), False,
                                        "2 player")
        self.three_player_button = Button(Vector(1, 3), Vector(1, 1), False,
                                          "3 player")
        self.four_player_button = Button(Vector(1, 4), Vector(1, 1), False,
                                         "4 player")
        self.is_bot_button = Button(Vector(1, 5), Vector(1, 1), False, "Bot")

        self.rendering_text = Text(Vector(0, 7), "Rendering")
        self.rendering_enabled_button = Button(Vector(1, 8), Vector(1, 1),
                                               True, "Enabled")
        self.rendering_disabled_button = Button(Vector(1, 9), Vector(1, 1),
                                                False, "Disabled")

        self.board_size_text = Text(Vector(0, 11), "Board size")
        self.small_board_button = Button(Vector(1, 12), Vector(1, 1), True,
                                         "Small")
        self.medium_board_button = Button(Vector(1, 13), Vector(1, 1), False,
                                          "Medium")
        self.large_board_button = Button(Vector(1, 14), Vector(1, 1), False,
                                         "Large")

        self.start_button = Button(Vector(4, 16),
                                   Vector(3, 1),
                                   False,
                                   "Start game",
                                   sprite=ResourceManager.snake_heads[0])

        self.buttons = [
            self.start_button, self.single_player_button,
            self.two_player_button, self.three_player_button,
            self.four_player_button, self.is_bot_button, self.game_mode_text,
            self.rendering_text, self.rendering_disabled_button,
            self.rendering_enabled_button, self.board_size_text,
            self.small_board_button, self.medium_board_button,
            self.large_board_button
        ]
示例#26
0
class SieveType:

    arrow_player = {
        pygame.K_UP: Vector(0, -1),
        pygame.K_DOWN: Vector(0, 1),
        pygame.K_LEFT: Vector(-1, 0),
        pygame.K_RIGHT: Vector(1, 0)
    }

    wasd_player = {
        pygame.K_w: Vector(0, -1),
        pygame.K_s: Vector(0, 1),
        pygame.K_a: Vector(-1, 0),
        pygame.K_d: Vector(1, 0)
    }

    ijkl_player = {
        pygame.K_i: Vector(0, -1),
        pygame.K_k: Vector(0, 1),
        pygame.K_j: Vector(-1, 0),
        pygame.K_l: Vector(1, 0)
    }

    num_player = {
        pygame.K_KP8: Vector(0, -1),
        pygame.K_KP5: Vector(0, 1),
        pygame.K_KP4: Vector(-1, 0),
        pygame.K_KP6: Vector(1, 0)
    }
示例#27
0
 def move(self, moving_direction=Vector(0, 0)):
     previous_pos = Vector(self.position.x, self.position.y)
     self.position.x += moving_direction.x
     self.position.y += moving_direction.y
     return previous_pos
示例#28
0
 def get_rendering_components(self):
     return [
                RenderPacket(self.rect, Vector(0, self.position.y)),
                RenderPacket(self.message_sprite, self.position + Vector(1, 1))
            ] + \
            [button_component for button in self.buttons for button_component in button.get_rendering_components()]
示例#29
0
 def __init__(self, position=Vector(0, 0), rotation=0, num=0):
     self.position = position
     self.rotation = rotation
     self.num = num
示例#30
0
 def __init__(self, position=Vector(0, 0)):
     self.position = position