def check_neighbours(self, grid: Grid, grid_cell: GridCell):
        currentIndex = (self.current_cell.cellx, self.current_cell.celly)

        grid_cell.looked_at = True

        direction = (grid_cell.cellx - self.current_cell.cellx, grid_cell.celly - self.current_cell.celly)

        thing = 0

        for y in range(grid_cell.celly - 1, grid_cell.celly + 1):
            for x in range(grid_cell.cellx - 1, grid_cell.cellx + 1):
                if direction == (-1, 0) or direction == (1, 0):
                    if x < 0 or x > grid.sizeX - 1 or y < 0 or y > grid.sizeY - 1 or y is currentIndex[1]:
                        continue
                elif direction == (0, -1) or direction == (0, 1):
                    if x < 0 or x > grid.sizeX - 1 or y < 0 or y > grid.sizeY - 1 or x is currentIndex[0]:
                        continue

                if grid.grid_as_array[x][y].traversable is True:
                    return False

        if grid_cell.traversable is True:
            return False

        self.stack.append(self.current_cell)
        self.current_cell = grid_cell
        grid_cell.traversable = True
        grid_cell.color = (128, 0, 128)
        return True

        pass
Example #2
0
def update():
    running = True
    global start
    global end
    start = myGrid.gridCells[0]
    start.color = start.hextorgb(start.color_dict["origin/destination"])
    end = myGrid.gridCells[899]
    end.color = end.hextorgb(end.color_dict["origin/destination"])
    isTrue = True
    while running:

        pygame.time.delay(100)

        global mouse_position
        global previous_mouse_position
        mouse_position = pygame.mouse.get_pos()

        if mouse_position != previous_mouse_position:
            # add on mouse move event
            pass

        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONUP:
                handle_button_click(pygame.mouse.get_pos())
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_1:
                    cell = myGrid.find_grid_cell(pygame.mouse.get_pos())
                    if cell is not None:
                        if start.traversable is True:
                            start.color = GridCell.hextorgb(
                                start.color_dict["walkable"])
                        start = cell
                        start.color = GridCell.hextorgb(
                            start.color_dict["origin/destination"])
                if event.key == pygame.K_2:
                    cell = myGrid.find_grid_cell(pygame.mouse.get_pos())
                    if cell is not None:
                        if end.traversable is True:
                            end.color = GridCell.hextorgb(
                                start.color_dict["walkable"])
                        end = cell
                        end.color = GridCell.hextorgb(
                            start.color_dict["origin/destination"])
                    pass

        update_game(myWindow)
        # mybutton.update(myWindow)
        for button in buttons:
            button.update(myWindow)
        pygame.display.update()
        previous_mouse_position = mouse_position
    def make_new_maze(self, grid: Grid):
        # start with filled grid
        for cell in grid.gridCells:
            cell.traversable = False
            cell.color = GridCell.hextorgb(cell.color_dict["obstacle"])
            cell.looked_at = False
        # pick a cell, mark it as part of maze
        self.current_cell = random.choice(grid.gridCells)
        self.current_cell.traversable = True
        self.current_cell.color = GridCell.hextorgb(self.current_cell.color_dict["walkable"])
        # add surrounding cells to stack
        neigh = self.current_cell.searchneighbours
        self.stack.extend(neigh)
        # while stack is not empty
        while len(self.stack) > 0:
            # pick random cell from stack
            current = random.choice(self.stack)
            # if cell doesn't have 2 explored neighbours
            count = 0

            for cell in current.searchneighbours:

                direction = (cell.cellx - current.cellx, cell.celly - current.celly)

                if direction == (-1, 0) or direction == (1, 0):
                    if current.celly is cell.celly or current.celly == 0 or current.celly == grid.sizeY - 1:
                        # print("continued")
                        continue
                elif direction == (0, -1) or direction == (0, 1):
                    if current.cellx is cell.cellx or current.cellx == 0 or current.cellx == grid.sizeX - 1:
                        # print("continued")
                        continue

                if cell.traversable is True:
                    count += 1

            if count < 2:
                # make cell walkable
                current.traversable = True
                current.color = GridCell.hextorgb(cell.color_dict["walkable"])
                # add neighbours to stack
                for cell in current.searchneighbours:
                    if cell.looked_at is False and cell not in self.stack:
                        self.stack.append(cell)
                # self.stack.extend(current_neighbours)

            # remove cell from stack
            self.stack.remove(current)
            current.looked_at = True
            print("removed from stack at length: ", len(self.stack))
            pass
        print("Completed Maze")
        pass
    def find_new_path(self, origin: GridCell, destination: GridCell):
        print("Starting search")
        self.open_list.clear()
        self.closed_list.clear()
        self.open_list.insert(0, origin)

        origin.G = 0
        origin.F = origin.G + self.euclidean_distance(origin, destination)
        while len(self.open_list) > 0:
            currentCell = min(self.open_list, key=lambda o: o.G + o.H)

            if currentCell is destination:
                thread = Thread(self.return_path(currentCell))
                thread.start()
                return

            self.open_list.remove(currentCell)
            self.closed_list.append(currentCell)

            currentCell.G = 0
            currentCell.F = self.euclidean_distance(currentCell, destination)

            currentCell.color = (0, 255, 0)

            for gc in currentCell.neighbours:
                if gc.traversable is False:
                    continue
                cost = currentCell.G + self.euclidean_distance(gc, currentCell)
                if gc in self.open_list and cost < gc.G:
                    self.open_list.remove(gc)
                    print("in open and less")
                if gc in self.closed_list and cost < gc.G:
                    self.closed_list.remove(gc)
                    self.open_list.append(gc)
                    gc.G = cost
                    gc.parent = currentCell
                    print("in closed and less")
                if gc not in self.open_list and gc not in self.closed_list:
                    self.open_list.append(gc)
                    gc.G = cost
                    gc.H = self.euclidean_distance(gc, destination)
                    gc.F = gc.G + gc.H
                    gc.parent = currentCell
                    gc.color = (0, 0, 255)
                time.sleep(0.01)
            time.sleep(0.01)
    def find_path(self, grid: Grid, origin: GridCell, destination: GridCell):
        print("Starting search")
        self.open_list.clear()
        self.closed_list.clear()
        self.open_list.append(origin)

        origin.G = 0
        origin.F = self.distance(origin, destination)

        while len(self.open_list) > 0:
            self.open_list.sort(key=lambda o: o.F)
            current_node = self.open_list[0]
            current_node.color = GridCell.hextorgb(
                current_node.color_dict["searching"])
            self.open_list.remove(current_node)
            self.closed_list.append(current_node)

            if current_node is destination:
                thread = Thread(self.return_path(grid, current_node))
                thread.start()
                print("Found path")
                return None

            for gc in current_node.neighbours:
                if gc is destination:
                    gc.parent = current_node
                    thread = Thread(self.return_path(grid, gc))
                    thread.start()
                    print("found path in child")
                    return None
                if gc not in self.closed_list and gc.traversable is True:
                    if gc not in self.open_list:
                        self.open_list.append(gc)
                        gc.parent = current_node
                        gc.color = GridCell.hextorgb(
                            current_node.color_dict["searchedneighbour"])

                localGoal = current_node.G + self.distance(current_node, gc)

                if localGoal < gc.G:
                    gc.parent = current_node
                    gc.G = localGoal
                    gc.F = gc.G + self.distance(gc, destination)
                time.sleep(0.001)
            time.sleep(0.001)
    def return_path(self, grid: Grid, cell: GridCell):
        self.path.clear()
        current_cell = cell
        self.path.append(current_cell)
        while current_cell.parent is not None:
            current_cell = current_cell.parent
            self.path.append(current_cell)

        self.path.reverse()
        for element in self.path:
            element.color = GridCell.hextorgb(cell.color_dict["path"])
            time.sleep(0.05)
        self.reset_nodes(grid)
Example #7
0
    def initialize_grid(self):
        self.gridCells.clear()
        for y in range(0, self.sizeY):
            for x in range(0, self.sizeX):
                newCell = GridCell(x * (self.cellSizeX * 1),
                                   50 + y * (self.cellSizeY * 1),
                                   self.cellSizeX * 1, self.cellSizeY * 1,
                                   True, x, y)
                self.gridCells.append(newCell)
                self.grid_as_array[y][x] = newCell
        #for i in range(0, self.sizeY * self.sizeX):
        #    y = int(i / self.sizeY)
        #    x = int(i % self.sizeX)
        #    newCell = GridCell(x * (self.cellSizeX * 1), y * (self.cellSizeY * 1), self.cellSizeX * 1,
        #                       self.cellSizeY * 1, True, x, y)
        #    self.gridCells.append(newCell)
        #    self.grid_as_array[x][y] = newCell

            pass
        self.set_neighbours_of_cells()