Пример #1
0
def main(speed=0.1, sort_method=sort_alg.bubble_sort):
    screen = pygame.display.set_mode(resolution, 0, 32)
    clock = pygame.time.Clock()
    grid_view = GridView(screen, width, height, grid_size, grid_line_color)

    sort_alg.generate_random_array()
    path_track = sort_method()
    print path_track

    # some utility variable
    index = 0
    pass_time = 0

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                exit()

        screen.fill(background_color)

        grid_view.draw_array_in_grid(path_track[index][0], cell_color,
                                     path_track[index][1], special_cell_color)

        grid_view.draw()
        time_passed_second = clock.tick() / 1000.0
        pass_time += time_passed_second

        if pass_time >= speed:
            pass_time = 0
            index = index + 1
            if index >= len(path_track):
                index = len(path_track) - 1

        pygame.display.update()
Пример #2
0
    def __init__(self, display):
        self.time = 0
        self.agents = dict()
        self.display = display

        self.model = GridModel()

        if display:
            pygame.init()
            pygame.display.set_caption("Grid with agents")
            self.view = GridView(self)
            self.control = GridControl(self.model)

        self.entities = [Entity(x) for x in constants.FRUITS]
        self.entity_global_average_price = {}

        self.price_trends = {}

        self.total_negotiations = []

        #This is a dict to maintain price update requests coming from system in the given time step
        self.entity_global_price_updates = {}
Пример #3
0
def main(maze_method, speed=0.010, mode=0):
    """
        the main program of the program
        mode 0: default mode, build the wall
        mode 1: first all cell are wall, then pull down the wall
        """
    screen = pygame.display.set_mode(resolution, 0, 32)
    clock = pygame.time.Clock()
    maze, cell_list = maze_method()

    path_finder = MazePathFinder(maze, (2, 1), (cell_row_num * 2, cell_col_num * 2 + 1),
                                 height // grid_size, width // grid_size)
    path = path_finder.bfs_find_path()

    index = 0
    path_index = 0
    maze_finished = False

    pass_time = 0
    grid_view = GridView(screen, width, height, grid_size, grid_line_color)
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                exit()

        press_key = pygame.key.get_pressed()

        # press F5 to regenerate the maze
        if press_key[K_F5]:
            index = 0
            path_index = 0
            maze, cell_list = maze_method()
            path_finder = MazePathFinder(maze, (2, 1), (cell_row_num * 2, cell_col_num * 2 + 1),
                                         height // grid_size, width // grid_size)
            path = path_finder.bfs_find_path()
            maze_finished = False

        # print index

        if mode == 0:
            screen.fill(background_color)
        else:
            screen.fill(cell_color)

        # draw the cell
        for i in range(index + 1):
            if mode == 0:
                grid_view.fill_a_cell(cell_list[i][1], cell_list[i][0], cell_color)
            else:
                grid_view.fill_a_cell(cell_list[i][1], cell_list[i][0], background_color)

        # draw the grid
        grid_view.draw()

        # draw the path
        if maze_finished and path:
            for i in range(path_index + 1):
                grid_view.fill_a_cell_with_circle(path[len(path) - i - 1][1],
                                                  path[len(path) - i - 1][0], path_color)

        time_passed_seconds = clock.tick() / 1000.0
        pass_time += time_passed_seconds

        if pass_time >= speed:
            pass_time = 0
            if maze_finished:
                if path_index + 1 < len(path):
                    path_index += 1

            if index >= len(cell_list) - 1:
                # print 'maze_finished'
                maze_finished = True

            if index + 1 < len(cell_list):
                index += 1

        pygame.display.update()
Пример #4
0
    button.nav_target = f"level_{x + 1}"


num_rows = NUM_LEVELS // LEVELS_PER_ROW
level_select_screen = GridView([
    [View(5, 1, children=[
        Button(
            text="Quit",
            dest=(0.05, 0.05, 0.1, 0.4),
            aspect_ratio=2,
            border_width=5,
            border_radius=10,
            on_click=lambda self, event: self.run_hook("QUIT_APP")
        ),
        Text(
            text="Level Select",
            font_kwargs={
                "font_size": 40,
                "bold": True
            },
            dest=(0.3, 0.1, 0.4, 0.8)
        )
    ])]
] + [
    level_select_buttons[row * LEVELS_PER_ROW:(row + 1) * LEVELS_PER_ROW]   
    for row in range(NUM_LEVELS // LEVELS_PER_ROW)
], margins=(10,)*4)


hoster = Hoster(
    {
Пример #5
0
 def __init__(self):
     pygame.init()
     pygame.display.set_caption("Grid with agents")
     self.model = GridModel()
     self.view = GridView(self.model)
     self.control = GridControl(self.model)
 GridView(
     [[Text(2, 3, text="Arithma", **menu_h1_style)],
      [
          Button(2,
                 2,
                 text="Levels",
                 on_click=lambda self, event: self.run_hook(
                     "NAVIGATE_TO", "level_select"),
                 **menu_button_style)
      ],
      [
          Button(2,
                 2,
                 text="Settings",
                 on_click=lambda self, event: self.run_hook(
                     "NAVIGATE_TO", "settings"),
                 **menu_button_style)
      ],
      [
          Button(1,
                 2,
                 text="Credits",
                 on_click=lambda self, event: self.run_hook(
                     "NAVIGATE_TO", "credits"),
                 **menu_button_style),
          Button(1,
                 2,
                 text="Quit",
                 on_click=lambda self, event: self.run_hook("QUIT_APP"),
                 **menu_button_style)
      ]],
     dest=(0.3, 0.0, 0.4, 0.9))
Пример #7
0
            if self.rerender_on_next_frame:
                self.update_screen()
                self.rerender_on_next_frame = False

        pygame.display.quit()


if __name__ == "__main__":
    test_button = Button(
        1, 1, RED,
        text="Navigate to Other!",
        on_click=lambda self, event: self.run_hook("NAVIGATE_TO", "other")
    )

    test_component_1 = GridView([
        [View(5, 1, RED), View(2, 1, GREEN)],
        [View(6, 1, BLUE), test_button]
    ])

    test_component_2a = GridView([
        [View(1, 1, GREEN), View(1, 1, RED)],
        [Button(1, 1, RED, text="HIT", on_click=lambda self, event: print("HIT")), View(1, 1, GREEN)]
    ], x_flex=1, y_flex=1)

    test_assets_dir = os.path.join(os.path.dirname(__file__), "test_assets")
    test_image_source = pygame.image.load(os.path.join(test_assets_dir, "smile.png"))

    test_component_2 = GridView([
        [Image(image=test_image_source), test_component_2a],
        [Text(2, 1, BLUE, "Hello, World!")]
    ])
Пример #8
0
def main(maze_method, speed=0.010, mode=0):
    pygame.init()
    check = 0
    screen = pygame.display.set_mode(resolution)
    pygame.display.set_caption("A_star_Maze")
    clock = pygame.time.Clock()
    maze, cell_list = maze_method()  # 랜덤으로 생성된 2차원 리스트 미로와 xxx 좌표가 담긴 리스트를 받는다

    path = A_star_logic.main(maze, start_point, end_point)
    index = 0
    maze_finished = False

    pass_time = 0
    grid_view = GridView(screen, width, height, grid_size, grid_line_color)
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        press_key = pygame.key.get_pressed()

        # press F5 to regenerate the maze
        if press_key[pygame.K_F5]:
            index = 0
            # path_index = 0
            maze, cell_list = maze_method()
            path = A_star_logic.main(maze, start_point, end_point)
            maze_finished = False
            check = 0
        elif press_key[pygame.K_d]:
            main(maze_method=get_maze_method(DFS), mode=1)
            pass
        elif press_key[pygame.K_k]:
            main(maze_method=get_maze_method(Kruskal), mode=1)

        # print index

        if mode == 0:
            screen.fill(background_color)
        else:
            screen.fill(cell_color)

        # draw the cell
        for i in range(index + 1):
            if mode == 0:
                grid_view.fill_a_cell(
                    cell_list[i][1], cell_list[i][0], cell_color)
            else:
                grid_view.fill_a_cell(
                    cell_list[i][1], cell_list[i][0], background_color)

        # draw the grid
        grid_view.draw()

        # draw the path
        if maze_finished and path:
                for i in range(len(path)):
                    grid_view.fill_a_cell_with_circle(
                        path[i][1], path[i][0], path_color)
                    pygame.display.update()
                    sleep(0.1)
                maze_finished = False
                check = 1

        time_passed_seconds = clock.tick() / 1000.0
        pass_time += time_passed_seconds

        if pass_time >= speed:
            pass_time = 0

            if index >= len(cell_list) - 1 and check == 0:
                    maze_finished = True
                    print("랜덤으로 생성된 미로 탐색 비용은 : "+str(len(path)))

            if index + 1 < len(cell_list):
                index += 1
                pygame.display.update()