def maze(): the_maze = Maze(30, 4, 2, 0) the_miner = Slaver() the_miner.dig(the_maze.cell(0, 0, 0)) while not the_miner.finished(): the_miner.run() print(the_maze.string())
def __init__(self): pygame.init() self.size = (Constants.WINDOW_WIDTH, Constants.WINDOW_HEIGHT) self.screen = pygame.display.set_mode(self.size) self.keyboard_handler = KeyboardHandler() self.font = pygame.font.SysFont(pygame.font.get_fonts()[0], 64) self.maze = Maze(Constants.GRID_SIZE, Constants.GRID_SIZE, self.size) self.time = pygame.time.get_ticks() self.maze.generate_maze()
def buildMaze(filename): infile = open(filename, "r") # Read the size of the maze. nrows, ncols = readValuePair(infile) maze = Maze(nrows, ncols) # Read the starting and exit positions. row, col = readValuePair(infile) maze.setStart(row, col) row, col = readValuePair(infile) maze.setExit(row, col) # Read the maze itself. for row in range(nrows): line = infile.readline() for col in range(len(line)): if line[col] == "*": maze.setWall(row, col) # Close the maze file and return the newly constructed maze. infile.close() return maze
def _generate(self): random.seed(os.urandom(6)) # collect dimentions and balances knot_work = Maze(self.getWidth(), self.getHeight(), 1, 0) Wall.straights_balance = self.straightsBalance Wall.zoomorph_balance = self.zoomorphBalance # create workers miner1 = Mazer(knot_work) miner1.dig(knot_work.entrance(Tweak(Tweak.master))) knot_work.add_bod(miner1, True) # add symmetries style = Tweak.master if self.symmetries & Symmetry.Rotate90: # must check we are square if self.getWidth() == self.getHeight(): style = Tweak.rot090 else: style = Tweak.rot180 elif self.symmetries & Symmetry.Rotate180: style = Tweak.rot180 elif self.symmetries & Symmetry.Vertical: style = Tweak.vanity elif self.symmetries & Symmetry.Horizontal: style = Tweak.horizon if style != Tweak.master: miner2 = Clone(knot_work, Tweak(style), miner1) miner2.dig(knot_work.entrance(miner2.tweak)) knot_work.add_bod(miner2, True) if style == Tweak.rot090: miner3 = Clone(knot_work, Tweak(Tweak.rot180), miner1) miner4 = Clone(knot_work, Tweak(Tweak.rot270), miner1) miner3.dig(knot_work.entrance(miner3.tweak)) miner4.dig(knot_work.entrance(miner4.tweak)) knot_work.add_bod(miner3, True) knot_work.add_bod(miner4, True) knot_work.mine() self.setKnot(Knot.load(knot_work.code())) self.saved = False self.resetCellEditor() return
class Game: """ Initialize PyGame and create a graphical surface to write. Similar to void setup() in Processing """ def __init__(self): pygame.init() self.size = (Constants.WINDOW_WIDTH, Constants.WINDOW_HEIGHT) self.screen = pygame.display.set_mode(self.size) self.keyboard_handler = KeyboardHandler() self.font = pygame.font.SysFont(pygame.font.get_fonts()[0], 64) self.maze = Maze(Constants.GRID_SIZE, Constants.GRID_SIZE, self.size) self.time = pygame.time.get_ticks() self.maze.generate_maze() """ Method 'game_loop' will be executed every frame to drive the display and handling of events in the background. In Processing this is done behind the screen. Don't change this, unless you know what you are doing. """ def game_loop(self): current_time = pygame.time.get_ticks() delta_time = current_time - self.time self.time = current_time self.handle_events() self.update_game(delta_time) self.draw_components() """ Method 'update_game' is there to update the state of variables and objects from frame to frame. """ def update_game(self, dt): pass """ Method 'draw_components' is similar is meant to contain everything that draws one frame. It is similar to method void draw() in Processing. Put all draw calls here. Leave all updates in method 'update' """ def draw_components(self): self.screen.fill([255, 255, 255]) self.maze.draw_maze(self.screen) pygame.display.flip() def draw_score(self): text = self.font.render( str(self.score[0]) + ":" + str(self.score[1]), True, (255, 255, 255)) self.screen.blit(text, (self.size[0] / 2 - 64, 20)) def reset(self): pass """ Method 'handle_event' loop over all the event types and handles them accordingly. In Processing this is done behind the screen. Don't change this, unless you know what you are doing. """ def handle_events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: self.handle_key_down(event) if event.type == pygame.KEYUP: self.handle_key_up(event) if event.type == pygame.MOUSEMOTION: self.handle_mouse_motion(event) if event.type == pygame.MOUSEBUTTONDOWN: self.handle_mouse_pressed(event) if event.type == pygame.MOUSEBUTTONUP: self.handle_mouse_released(event) """ This method will store a currently pressed buttons in list 'keyboard_handler.pressed'. """ def handle_key_down(self, event): self.keyboard_handler.key_pressed(event.key) """ This method will remove a released button from list 'keyboard_handler.pressed'. """ #TODO: Clean this up! def handle_key_up(self, event): self.keyboard_handler.key_released(event.key) if event.key == pygame.K_m: print("Generating Maze") self.maze.generate_maze() if event.key == pygame.K_e: print("Generating empty space") self.maze.open_maze() if event.key == pygame.K_o: print("Generating obstacles") self.maze.generate_obstacles() if event.key == pygame.K_r: print("Generating rooms") self.maze.generate_room() if event.key == pygame.K_b: print("Breath solve") self.maze.breadth_first_solution() if event.key == pygame.K_d: print("Depth solve") self.maze.depth_first_solution() if event.key == pygame.K_g: print("Greedy solve") self.maze.greedy_search() if event.key == pygame.K_a: print("A* solve") self.maze.a_star_search() if event.key == pygame.K_x: print("Depth solve iteration") self.maze.recursive_dfs_iteration() """ Similar to void mouseMoved() in Processing """ def handle_mouse_motion(self, event): pass """ Similar to void mousePressed() in Processing """ def handle_mouse_pressed(self, event): pass """ Similar to void mouseReleased() in Processing """ def handle_mouse_released(self, event): pass
def create_maze(self, cells_across, cells_up, levels, cell_size, digger, show_dig): x = self.root.winfo_x() y = self.root.winfo_y() maze_window = Toplevel(self.root) maze_window.geometry("+%d+%d" % (x, y + 200)) self.maze_windows.append(maze_window) bods_window = Toplevel(self.root) bods_window.geometry("+%d+%d" % (x + 300, y)) self.maze_windows.append(bods_window) the_maze = Maze(cells_across, cells_up, levels, cell_size) the_maze.tk_init_maze(maze_window) if digger == 1: self.miner = Miner(the_maze) elif digger == 2: self.miner = Lister(the_maze) else: self.miner = Slaver(the_maze) self.miner.dig(the_maze.at(Maze.start)) the_maze.add_bod(self.miner) the_maze.mine(self.miner, show_dig) the_maze.remove_bod(self.miner) # Choose start and goal. self.start_cell = the_maze.cell(0, 0, 0) worm = Inchworm(the_maze) cell_distances = worm.distances(self.start_cell) furthest = len(cell_distances ) - 1 # first is zero, and others go up from there... far_cells = cell_distances[furthest] # list of furthest cells # Make start and goal the_maze.add_thing(self.start_cell, Start(the_maze)) self.goal_cell = the_maze.at(choice(far_cells)) the_maze.add_thing(self.goal_cell, Goal(the_maze)) # Make gates and decoy gates # gate_maker = Gatemaker(the_maze, worm) # gate_maker.make(start_cell, self.goal_cell) # Make robot # self.robot = Robot(the_maze) # self.robot.go(the_maze.at(Maze.start)) # self.robot.goal = self.b # the_maze.add_bod(self.robot) # Make Gamer # self.gamer = Gamer(the_maze) # self.gamer.go(the_maze.at(Maze.start)) # self.gamer.goal = self.goal_cell # the_maze.add_bod(self.gamer) the_maze.tk_init_things() the_maze.tk_init_bods(bods_window) the_maze.tk_paint()