本文整理汇总了Python中maze.Maze类的典型用法代码示例。如果您正苦于以下问题:Python Maze类的具体用法?Python Maze怎么用?Python Maze使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Maze类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Main
class Main(object):
def __init__(self):
size = (500, 500)
self.surface = pygame.display.set_mode(size)
mazeOrigin = (250, 250)
self.maze = Maze(mazeOrigin)
done = False
while(not done):
self.surface.fill((255,255,255))
self.maze.update()
self.maze.draw(self.surface)
pygame.display.flip()
event = pygame.event.get()
for e in event:
if(e.type == QUIT):
done = True
elif(e.type == KEYDOWN):
if(e.key == K_q):
done = True
if(e.key == K_r):
self.maze = Maze(mazeOrigin)
示例2: test_neighbors
def test_neighbors():
m = Maze(sample_layout)
ns = m.get_neighbors(1,1)
assert len(ns) == 2 # unintuitive method for set size
assert (1,2) in ns
assert (0,1) in ns
ns = m.get_neighbors(4,0)
assert ns == set([(3,0)])
示例3: __init__
class Gui:
def __init__(self, rows, cols):
pygame.init()
self.rows = rows
self.cols = cols
self.maze = Maze(self.rows, self.cols)
self.maze.generate()
self.screen = pygame.display.set_mode((self.cols * SIZE_CELL, self.rows*SIZE_CELL))
pygame.display.set_caption("OnlineByrinth")
self.show_solution = False
self.quit = False
def update(self):
return
def draw(self):
self.screen.fill((WHITE))
for pos, cell in self.maze.grid.items():
row, col = pos
row, col = row * SIZE_CELL, col * SIZE_CELL
if cell.n_wall:
pygame.draw.line(self.screen, RED, (col, row), (col + SIZE_CELL, row))
if cell.s_wall:
pygame.draw.line(self.screen, RED, (col, row + SIZE_CELL), (col + SIZE_CELL, row + SIZE_CELL))
if cell.w_wall:
pygame.draw.line(self.screen, RED, (col, row), (col, row + SIZE_CELL))
if cell.e_wall:
pygame.draw.line(self.screen, RED, (col + SIZE_CELL, row), (col + SIZE_CELL, row + SIZE_CELL))
# Bordure sud et est
pygame.draw.line(self.screen, RED, (0, self.rows * SIZE_CELL - 1), (self.cols * SIZE_CELL, self.rows * SIZE_CELL - 1))
pygame.draw.line(self.screen, RED, (self.cols * SIZE_CELL - 1, self.rows), (self.cols * SIZE_CELL - 1, self.rows * SIZE_CELL - 1))
if self.show_solution:
for cell in self.maze.path:
row, col = cell.row * SIZE_CELL, cell.col * SIZE_CELL
pygame.draw.ellipse(self.screen, GREEN, pygame.Rect(col + SIZE_CELL / 4, row + SIZE_CELL / 4, SIZE_CELL / 2, SIZE_CELL / 2))
pygame.display.flip()
def main_loop(self):
while not self.quit:
for event in pygame.event.get():
if event.type == QUIT:
self.quit = True
if event.type == KEYDOWN and event.key == pygame.K_SPACE:
self.maze.generate()
if event.type == KEYDOWN and event.key == pygame.K_s:
self.show_solution = not self.show_solution
self.update()
self.draw()
示例4: main
def main():
files = os.listdir(MAZES)
# for f in files:
m = Maze(MAZES + "big.maze")
solved = m.solveUsing(method=BFS, timeseries=True)
if not solved:
return "No solution"
else:
printMaze(solved)
示例5: main
def main():
argv = sys.argv
m = Maze(MAZES + argv[1] + '.maze')
solved =m.solveUsing(A_Star, timeseries=True, heuristic=manhattanDist, comparisonFunc=comparisonFunc, costAssign=costAssignment)
print m.expandedNodes()
with open(argv[1] + '_a_star_+1forward_newheur.out', 'w') as f:
for row in solved:
for elem in row:
f.write(elem)
f.write('\n')
示例6: main
def main():
argv = sys.argv
m = Maze(MAZES + argv[1] + '.maze')
solved = m.solveUsing(greedyBFS, True, euclideanDist, comparisonFunc)
print m.expandedNodes()
with open(argv[1] + '_greedy_bfs.out', 'w') as f:
for row in solved:
for elem in row:
f.write(elem)
f.write('\n')
示例7: main
def main():
argv = sys.argv
m = Maze(MAZES + argv[1] + ".maze")
solved = m.solveUsing(DFS, True)
print m.expandedNodes()
with open(argv[1] + "_dfs.out", "w") as f:
for row in solved:
for elem in row:
f.write(elem)
f.write("\n")
示例8: Pacman
class Pacman(object):
Up, Right, Down, Left = range(4)
tronches = {
Up : 'V',
Down : '^',
Left : '>',
Right : '<',
}
def __init__(self):
self.maze = Maze(10, 10)
self.x = 0
self.y = 0
self.direction = Pacman.Left
self.score = 0
def bouge(self):
oldx = self.x
oldy = self.y
if self.direction == Pacman.Down:
#if self.y < self.maze.longueur - 1:
self.y = (self.y + 1)% self.maze.longueur
elif self.direction == Pacman.Up:
#if self.y > 0:
self.y = (self.y - 1)% self.maze.longueur
elif self.direction == Pacman.Left:
#if self.x > 0:
self.x = (self.x - 1)% self.maze.largeur
elif self.direction == Pacman.Right:
#if self.x < self.maze.largeur - 1:
self.x = (self.x + 1)% self.maze.largeur
if self.maze.get(self.x,self.y) == '+':
self.x = oldx
self.y = oldy
return
self.mange()
def mange(self):
if self.maze.get(self.x, self.y) == '*':
self.score += 1
self.maze.set(self.x, self.y, '.')
@property
def tronche(self):
return self.tronches[self.direction]
示例9: game_loop
def game_loop(self):
"""Main loop. """
pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
score = 0
high_score = 0
maze = Maze(self.maze_tiles, HORIZONTAL_TILES, VERTICAL_TILES)
maze.load_level(maze.MAZE)
pac = PacMan(104, 204, self.pacman_tiles)
delta = (0, 0)
while True:
event = pygame.event.poll()
if event.type == pygame.QUIT:
sys.exit(0)
if event.type == pygame.KEYUP:
if event.key == pygame.K_ESCAPE:
sys.exit(0)
pressed = pygame.key.get_pressed()
if pressed[pygame.K_LEFT]:
delta = (-1, 0)
elif pressed[pygame.K_RIGHT]:
delta = (1, 0)
elif pressed[pygame.K_DOWN]:
delta = (0, 1)
elif pressed[pygame.K_UP]:
delta = (0, -1)
x, y = self.screen_to_tile(pac.x, pac.y)
if maze[(x, y)] > 0:
score += 10
maze[(x, y)] = 0
if score > high_score:
high_score = score
if self.legal_move(maze, pac, delta):
pac.delta = delta
elif not self.legal_move(maze, pac, pac.delta):
pac.delta = (0, 0)
pac.move()
maze.draw()
pac.draw()
self.write_message(3, 0, "1UP")
self.write_message(4, 1, str(score))
self.write_message(9, 0, "HIGH SCORE")
self.write_message(10, 1, str(score))
pygame.display.flip()
示例10: main
def main():
win = Window(fullscreen=True, visible=False)
camera = Camera(win.width, win.height, (0, 0), 100)
renderer = Renderer()
maze = Maze()
maze.create(50, 30, 300)
keyboard = Keyboard()
keyboard.key_handlers[key.ESCAPE] = win.close
keyboard.key_handlers.update(camera.key_handlers)
clock.schedule(maze.update)
win.on_draw = lambda: renderer.on_draw(maze, camera, win.width, win.height)
win.on_key_press = keyboard.on_key_press
keyboard.print_handlers()
win.set_visible()
app.run()
示例11: TestMazeParsing
class TestMazeParsing(unittest.TestCase):
def setUp(self):
self.maze_repr_bad = """
+-+-+-+
|aaaaa|
+-+-+-+
"""
self.maze_repr_bad_semantic = """
+-+-+-+
| | |
+-+-+-+
"""
self.maze_repr = """
+-+-+-+
| |*|
+ + + +
|*| |
+-+-+-+
"""
self.horiz_walls = [[True, True, True],
[False, False, False],
[True, True, True]]
self.vert_walls = [[True, False, True, True],
[True, True, False, True]]
def test_parse_bad(self):
self.assertRaises(Maze.ParseError, Maze, self.maze_repr_bad)
def test_parse_bad_semantic(self):
self.assertRaises(Maze.SemanticError, Maze, self.maze_repr_bad_semantic)
def test_parse(self):
self.maze = Maze(self.maze_repr)
self.assertEqual(self.horiz_walls, self.maze.horiz_walls)
self.assertEqual(self.vert_walls, self.maze.vert_walls)
self.assertEqual(3, self.maze.width())
self.assertEqual(2, self.maze.height())
def test_walls(self):
self.maze = Maze(self.maze_repr)
self.assertEqual([True, True, False, False], self.maze.walls(0, 1))
self.assertEqual([False, True, True, True], self.maze.walls(1, 0))
示例12: restart
def restart(self):
self.maze_obj = Maze(*self.dim)# pass args to change maze size: Maze(10, 10)
if self.diff == 0:
self.maze_obj.generate(self.maze_obj.maze[(0,0)])
else:
self.maze_obj.generate()
self.draw_maze()
self.reset_player()
示例13: build
def build(self):
material = self.material
min_x, max_x = self.mc.min_x, self.mc.max_x
min_z, max_z = self.mc.min_z, self.mc.max_z
# Don't forget the zero block! 1 - -1 == 2, but it is 3 blocks!
x_width = max_x - min_x + 1
z_width = max_z - min_z + 1
c_dim, c_height = 6, 3 # in world block units
# in labyrinth cell units (remember walls overlap, need exact fit + 1 for outermost wall)
x_cells, z_cells = (x_width - 1) / (c_dim - 1), (z_width - 1) / (c_dim - 1)
castle_cells = 13
maze = Maze(z_cells, x_cells)
# Disconnect cells where castle resides, ensures compatible maze
blk_x, blk_z = (x_cells - castle_cells) / 2, (z_cells - castle_cells) / 2
for bx in xrange(blk_x, blk_x + castle_cells):
for bz in xrange(blk_z, blk_z + castle_cells):
maze[bz, bx].disconnect()
maze.generate()
# Remove the wall from graph where the labyrinth will meet with castle
maze[blk_z + castle_cells / 2, blk_x + castle_cells].remove_wall(NORTH)
# Render the labyrinth by generating blocks where there are walls
for cell_x in xrange(0, x_cells):
for cell_z in xrange(0, z_cells):
walls = maze[cell_z, cell_x].walls
north, west = min_x + cell_x * (c_dim - 1), min_z + cell_z * (c_dim - 1)
if walls[NORTH] and walls[SOUTH] and walls[EAST] and walls[WEST]:
self.mc.make_cube(north, 0, west, c_dim, c_height, c_dim, material)
else:
if walls[NORTH]:
self.mc.make_cube(north, 0, west, 1, c_height, c_dim, material)
if walls[SOUTH]:
self.mc.make_cube(north + (c_dim - 1), 0, west, 1, c_height, c_dim, material)
if walls[EAST]:
self.mc.make_cube(north, 0, west + (c_dim - 1), c_dim, c_height, 1, material)
if walls[WEST]:
self.mc.make_cube(north, 0, west, c_dim, c_height, 1, material)
示例14: init_maze
def init_maze(self):
# custom map is a list containing each row
# and the width is the length of the first row
custom_map = []
f = open('map.txt', 'r')
for line in f.readlines():
custom_map.append(line)
f.close()
width = len(custom_map[0])
height = len(custom_map)
self.maze = Maze(width, height, custom_map)
hbox = QHBoxLayout()
hbox.setContentsMargins(0, 0, 10, 0)
hbox.addWidget(self.maze)
vbox = QVBoxLayout()
vbox.setContentsMargins(0, 10, 0, 0)
hbox.addLayout(vbox)
self.agents = QLineEdit("s")
vbox.addWidget(QLabel("agent to watch:"))
vbox.addWidget(self.agents)
self.btn_watch = QPushButton("Watch Agent")
self.btn_watch.clicked.connect(self.watchAgent)
vbox.addWidget(self.btn_watch)
self.btn_remove = QPushButton("Remove path")
self.btn_remove.clicked.connect(self.remove)
vbox.addWidget(self.btn_remove)
self.btn_solve = QPushButton("Start")
self.btn_solve.clicked.connect(self.startSolving)
vbox.addWidget(self.btn_solve)
self.btn_step = QPushButton("Next Step")
self.btn_step.clicked.connect(self.doStep)
vbox.addWidget(self.btn_step)
self.btn_stop = QPushButton("Stop")
self.btn_stop.setEnabled(False)
self.btn_stop.clicked.connect(self.stopSolving)
vbox.addWidget(self.btn_stop)
self.btn_restart = QPushButton("Restart")
self.btn_restart.setEnabled(True)
self.btn_restart.clicked.connect(self.restart)
vbox.addWidget(self.btn_restart)
vbox.addStretch()
proxy_widget = QWidget()
proxy_widget.setLayout(hbox)
self.setCentralWidget(proxy_widget)
self.maze.init()
示例15: test_construction
def test_construction():
if len(sys.argv) == 2:
parsed_info = Maze.parse_layout(sys.argv[1])
test_layout = parsed_info[0]
test_info = parsed_info[1]
else:
test_layout = sample_layout
test_info = None
m = Maze(test_layout)
assert str(m).strip() == test_layout.strip()