当前位置: 首页>>代码示例>>Python>>正文


Python File.setGrid方法代码示例

本文整理汇总了Python中file.File.setGrid方法的典型用法代码示例。如果您正苦于以下问题:Python File.setGrid方法的具体用法?Python File.setGrid怎么用?Python File.setGrid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在file.File的用法示例。


在下文中一共展示了File.setGrid方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Game

# 需要导入模块: from file import File [as 别名]
# 或者: from file.File import setGrid [as 别名]

#.........这里部分代码省略.........
                        if event.key == K_s:
                            self.save()
                        if event.key == K_l:
                            self.surface = None
                            self.load()

                pygame.display.update()
                self.fpsClock.tick(self.fps)

    def solve(self):
            if self.grid.startSet and self.grid.targetSet:
                print 'Solving'
                self.reset()
                # Reset lists
                self.openList = []
                self.closedList = []
                current = False
                # Add our start to the closed list and begin
                self.openList.append(self.grid.start)

                while len(self.openList)>0:
                    current = self.nextStep()
                    current.current = 1
                    current.draw()
                    if current == self.grid.target:
                        print 'found path!'
                        solving = 0
                        cell = self.grid.target
                        current.current = 0
                        current.draw()
                        path = []
                        while cell.parent:
                            path.append(cell)
                            cell = cell.parent
                        path = reversed(path)
                        for cell in path:
                            cell.path=1
                            cell.highlight=0
                            cell.draw()
                            pygame.time.delay(self.config.found_delay)
                            pygame.display.update()
                        break

                    self.closedList.append(current)

                    found = 0
                    for cell in current.neighbours:
                        if cell in self.closedList:
                            print 'closed'
                            continue
                        if cell not in self.openList or current.g + 1 <= cell.parent.g + 2:
                            cell.highlight = 1
                            cell.drawText = 1
                            cell.parent = current
                            cell.g = cell.parent.g + 1
                            cell.h = cell.calcH()
                            cell.f = cell.calcF()
                            self.openList.append(cell)
                            cell.draw()
                            found = 1
                    if found:
                        pygame.time.delay(self.config.search_delay)
                    print len(self.openList)
                    pygame.display.update()
                    current.current = 0
                    current.draw()
                else:
                    print 'Failure'
                self.solving = 0
            else:
                print 'Error: I need a start and finish!'

    def save(self):
        self.file.save()

    def load(self):
        self.fps = self.config.fps
        grid = self.file.load()

        if grid:
            self.config.grid_width, self.config.grid_height = (len(grid[0]), len(grid))

        self.SCREENWIDTH, self.SCREENHEIGHT = (self.config.grid_width * self.config.cell_width, self.config.grid_height * self.config.cell_height)
        self.surface = pygame.display.set_mode((self.SCREENWIDTH, self.SCREENHEIGHT))
        pygame.display.set_caption('Pathfinder')
        self.grid = Grid(self.config.grid_width, self.config.grid_height, self.config.cell_width, self.config.cell_height, self.surface, self)
        self.file.setGrid(self.grid)
        self.surface.fill(DKGREY)

        if grid:
            for i in range(0, len(grid)):
                for j in range(0, len(grid[i])):
                    self.setupCell(self.grid.cell(j+1, i+1), grid[i][j])
        self.reset()

    def nextStep(self):
        self.openList.sort(key=lambda x: x.f, reverse=True)
        cell = self.openList.pop()
        cell.getNeighbours()
        return cell
开发者ID:fyfey,项目名称:pathfinder,代码行数:104,代码来源:pathfinder.py


注:本文中的file.File.setGrid方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。