def main(): pygame.init() dims = Vec2(240, 160) window = pygame.display.set_mode((dims.x * square_size, dims.y * square_size)) grid = Grid(dims, window) pygame.display.set_caption("Maze") maze_generator = MazeGenerator(grid) astar = AStar(grid, Vec2(0, 0), grid.dims - Vec2(2, 2)) running = True clock = pygame.time.Clock() while running: for e in pygame.event.get(): if e.type == pygame.QUIT: running = False if e.type == pygame.KEYDOWN: if e.key == pygame.K_SPACE: astar.step() if not maze_generator.finished(): maze_generator.step() if maze_generator.finished(): astar.step() if not maze_generator.finished(): draw_cell(window, maze_generator.current_cell(), colors.red) pygame.display.update() # clock.tick(60) pygame.quit()
def main(): pelaaja = PlayerObject("Seppo") generator = MazeGenerator() pyramidi = generator.generate_maze(pelaaja) labyrintti = pyramidi.get_array() for level in range(3): for row in range(50): rivi = "" for col in range(50): rivi += labyrintti[level][row][col] print rivi print pyramidi.get_player()
class MazeGame(): def __init__(self, width, height, wall_char='X', path_char='.', player_char='@', finish_char='$'): self.exit_points = [ Point(0, 0), Point(width - 1, 0), Point(0, height - 1), Point(width - 1, height - 1) ] self.generator = MazeGenerator(width, height, wall_char=wall_char, path_char=path_char, finish_char=finish_char) self.field = self.generator.field self.player = Player(self.field, speed=speed) def make_new_maze(self): self.generator.generate(start_point=random_point( 0, self.field.width - 1, 0, self.field.height - 1), finish_point=choice(self.exit_points)) def start_game(self): self.make_new_maze() self.start_time = time.time() self.player.teleport(self.generator.start) def get_cur_time(self): return time.time() - self.start_time def check_player(self): if self.player.position in self.generator.finish_neis: self.win() return True return False def win(self): self.generator.field.fill('卍') update_field(self.generator.field) print('CONGRATULATIONS!!!') print(open('win.txt').read()) print('You beat the maze in {:.2f}'.format(self.get_cur_time()))
def __init__(self, width, height, wall_char='X', path_char='.', player_char='@', finish_char='$'): self.exit_points = [ Point(0, 0), Point(width - 1, 0), Point(0, height - 1), Point(width - 1, height - 1) ] self.generator = MazeGenerator(width, height, wall_char=wall_char, path_char=path_char, finish_char=finish_char) self.field = self.generator.field self.player = Player(self.field, speed=speed)
def __init__(self, type): generator = MazeGenerator() if type == 'prim': generator.prepare_prim_model() elif type == 'wall': generator.prepare_wall_model() self.model = generator.maze_model walls = [(i, j) for i in range(self.SIZE[0]) for j in range(self.SIZE[1]) if self.model[j][i] == 1] ghost_house = [] ghost_path = [] accessible = [(i, j) for i in range(self.SIZE[0]) for j in range(self.SIZE[1]) if self.model[j][i] == 0] big_dots = [accessible[random.randint(0, len(accessible)-1)] for i in range(4)] possible_ghost_spawn = [accessible[random.randint(0, len(accessible) - 1)] for i in range(4)] ghost_spawn = { game.GhostNames.inky: possible_ghost_spawn[0], game.GhostNames.pinky: possible_ghost_spawn[1], game.GhostNames.blinky: possible_ghost_spawn[2], game.GhostNames.clyde: possible_ghost_spawn[3] } possible_spawn = [x for x in accessible if x not in possible_ghost_spawn] spawn = possible_spawn[random.randint(0, len(possible_spawn) - 1)] tunnel = [] super().__init__(walls, accessible, ghost_spawn, ghost_house, ghost_path, spawn, tunnel, big_dots, self.SIZE)
from grid import Grid from maze_generator import MazeGenerator import pygame screen = pygame.display.set_mode((640, 480)) running = 1 white = [255, 255, 255] black = [0, 0, 0] myGrid = Grid((8, 8)) myGrid.create_grid((8, 8)) myMaze = MazeGenerator(myGrid) myMaze.processGrid() # Our maze is now fully initialized num_of_rows = len(myGrid.list_of_rows) num_of_cols = len(myGrid.list_of_rows[0]) while running: event = pygame.event.poll() if event.type == pygame.QUIT: running = 0 screen.fill(white) for i in range(0, num_of_rows): for j in range(0, num_of_cols): if (myMaze.mazedGrid.list_of_rows[i][j].top_edge is not None): # Draw top edges of this cell pygame.draw.line(screen, black, myGrid.list_of_rows[i][j].top_edge.point_1, myGrid.list_of_rows[i][j].top_edge.point_2) if (myMaze.mazedGrid.list_of_rows[i][j].bottom_edge is not None):
def main(): ''' Load mazes. ''' pyramid = None input_output = MazeIO() load_maze = str(raw_input("Do you want to load an existing maze? (Y/N) ")) if load_maze.lower() == "y": file_name = str(raw_input("Enter file name: ")) pyramid = input_output.load_maze(file_name) else: generator = MazeGenerator() print "MazeGen initialized..." ''' The purpose of calling the signal function is to break the execution of generate_maze, if no maze seems to be produced. This was to fix the error in the generate_maze algorithm, as it cannot always be guaranteed that its execution stops. ''' maze_done = False while not maze_done: #signal.signal(signal.SIGALRM, handler) #signal.alarm(5) try: pyramid = generator.generate_maze() if pyramid: maze_done = True except NameError: maze_done = False #signal.alarm(0) if pyramid != None: print "Pyramid initialized..." name = str(raw_input("Give player's name: ")) if len(name) > 0: player = PlayerObject(name, pyramid) else: player = PlayerObject("Mikael", pyramid) pygame.init() graffat = GraphicsEngine() print "Graphics initialized..." interfeissi = CommandEngine() if_done = False clock = pygame.time.Clock() while if_done == False: if_done = interfeissi.handle_events(player) #update graphics here graffat.draw_everything(player) # Limit to 20 frames per second clock.tick(20) if player.is_at_goal(): print "Goal reached! Hurrah!" if_done = True print player, 'used', player.get_steps(), 'steps for solving this labyrinth.' show_solution = str(raw_input("Do you want to see the computer's solution? (Y/N) ")) if show_solution.lower() == 'y': solver = MazeSolver() solution = solver.solve_maze(pyramid) height = len(solution) show_which_level = 0 while show_which_level >= 0: #update graphics here graffat.draw_solution(solution, show_which_level) show_which_level = interfeissi.solution_mode(show_which_level, height) # Limit to 20 frames per second clock.tick(20) pygame.quit() ''' Save mazes. ''' save_yn = str(raw_input("Do you want to save this maze? (Y/N) ")) if save_yn.lower() == "y": file_name = str(raw_input("Enter file name: ")) input_output.save_maze(player.get_pyramid(), file_name)