示例#1
0
def show_playing_screen(screen: pygame.Surface, cell_size: tuple, angel_icon,
                        devil_icon, game_logic: GameLogic):
    """
    Show the playing screen
    Return (new game logic which is changed,whether a move is made)
    """

    move_made = False

    board_size = game_logic.get_board().get_size()

    board_width = board_size[0]

    board_height = board_size[1]
    #width/column index
    for column_index in range(board_width):
        #height/row index
        for row_index in range(board_height):

            if not (column_index < board_width and row_index < board_height):
                continue

            rect = get_rect(row_index, column_index, cell_size)

            #If the position is angel position

            if game_logic.get_board().get_position(
                    row_index, column_index) == ANGLE_SPACE:
                screen.blit(angel_icon, rect)

            #If the position is devil position

            elif game_logic.get_board().get_position(
                    row_index, column_index) == BLOCK_SPACE:
                screen.blit(devil_icon, rect)
            else:
                pygame.draw.rect(screen, BLACK, rect)

            positions = list()

            is_angel_turn = game_logic.is_angel_turn()

            avaliable_moves = set()

            if is_angel_turn:
                avaliable_moves = game_logic.get_avaliable_angel_moves()
            else:
                avaliable_moves = game_logic.get_avaliable_devil_moves()

            for (i, j) in avaliable_moves:

                rect = get_rect(i, j, cell_size)

                pygame.draw.rect(screen, BLUE, rect)

                positions.append(Position(i, j, rect))

            left_clicked, _, _ = pygame.mouse.get_pressed()

            #if left clicked
            if left_clicked == 1:

                mouse = pygame.mouse.get_pos()

                for position in positions:
                    #if the avaliable position is clicked
                    if position.rect.collidepoint(mouse):

                        move_made = True

                        #if it is an angel turn,move the angel
                        if is_angel_turn:
                            game_logic.angel_move(position.row_index,
                                                  position.column_index)
                        else:
                            game_logic.devil_move(position.row_index,
                                                  position.column_index)

    return game_logic, move_made
示例#2
0
def resource_path(relative_path):
    try:
        # PyInstaller creates a temp folder and stores path in _MEIPASS
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)


if __name__ == "__main__":

    setting = Setting(resource_path("setting.config"))

    screen_resolution = setting.get_screen_resolution()

    grid = setting.get_grid()

    pygame.init()

    game_logic = GameLogic(Board(grid[0], grid[1]), setting.get_n())
    game_logic.init()

    screen = pygame.display.set_mode(screen_resolution)

    cell_size = get_cell_size(screen_resolution[0], screen_resolution[1],
                              game_logic.get_board().get_size())

    game_loop(screen, cell_size, game_logic)