def maze_draw(self): """ callback for the maze draws the maze :return: """ for index, pt in enumerate(self.maze.data): bx,by = maze_helper.get_i_j(self.maze,index) #self.walls.append(pygame.Rect(bx * BLOCKSIZE_X, by * BLOCKSIZE_Y, BLOCKSIZE_X, BLOCKSIZE_Y)) cell = maze_helper.check_cell(self.maze,index) if cell == 1: pygame.draw.rect(self.display_surf, PURPLE, (bx * BLOCKSIZE_X, by * BLOCKSIZE_Y, BLOCKSIZE_X, BLOCKSIZE_Y), 0) self.walls.append(pygame.Rect(bx * BLOCKSIZE_X, by * BLOCKSIZE_Y, BLOCKSIZE_X, BLOCKSIZE_Y)) elif cell == 2: pygame.draw.rect(self.display_surf, BLUE, (bx * BLOCKSIZE_X, by * BLOCKSIZE_Y, BLOCKSIZE_X, BLOCKSIZE_Y), 0) #self.walls.append(pygame.Rect(bx * BLOCKSIZE_X, by * BLOCKSIZE_Y, BLOCKSIZE_X, BLOCKSIZE_Y)) self.start_rec = pygame.Rect(bx * BLOCKSIZE_X, by * BLOCKSIZE_Y, BLOCKSIZE_X, BLOCKSIZE_Y) elif cell == 3: pygame.draw.rect(self.display_surf, RED, (bx * BLOCKSIZE_X, by * BLOCKSIZE_Y, BLOCKSIZE_X, BLOCKSIZE_Y), 0) self.goal_rec = pygame.Rect(bx * BLOCKSIZE_X, by * BLOCKSIZE_Y, BLOCKSIZE_X, BLOCKSIZE_Y)
def maze_callback(self,msg): """ Called when a new maze is published, identifies maze components and calls initialization function :param msg: occupany grid message :return: """ self.maze = msg self.walls = [] starts = [] goals = [] for index, pt in enumerate(self.maze.data): bx,by = maze_helper.get_i_j(self.maze,index) cell = maze_helper.check_cell(self.maze,index) if cell == 1: self.walls.append(pygame.Rect(bx * maze_helper.BLOCKSIZE_X, by * maze_helper.BLOCKSIZE_Y, maze_helper.BLOCKSIZE_X, maze_helper.BLOCKSIZE_Y)) elif cell == 2: starts.append(pygame.Rect(bx * maze_helper.BLOCKSIZE_X, by * maze_helper.BLOCKSIZE_Y, maze_helper.BLOCKSIZE_X, maze_helper.BLOCKSIZE_Y)) self.start_rec = starts[0].unionall(starts) elif cell == 3: goals.append(pygame.Rect(bx * maze_helper.BLOCKSIZE_X, by * maze_helper.BLOCKSIZE_Y, maze_helper.BLOCKSIZE_X, maze_helper.BLOCKSIZE_Y)) self.goal_rec = goals[0].unionall(goals) self.on_init()
def update_score(self, assistance=3): player_x = math.floor(float(self.player.centerx) / maze_helper.BLOCKSIZE_X) # This is the (x,y) block in the grid where the center of the player is player_y = math.floor(float(self.player.centery) / maze_helper.BLOCKSIZE_Y) point_index = maze_helper.index_to_cell(self.maze, player_x, player_y) if maze_helper.check_cell(self.maze, int(point_index)) == 1: self.score -= 1 for rec in self.solved_path: if rec.centerx == player_x and rec.centery == player_y: #print "On track" reward = math.floor(1./len(self.solved_path) * 100) self.score += reward
def check_collision_adaptive(self): walls = [] centers = [] player_x = math.floor(float(self.player_rec.centerx)/BLOCKSIZE_X) # This is the (x,y) block in the grid where the top left corner of the player is player_y = math.floor(float(self.player_rec.centery)/BLOCKSIZE_Y) for x in range(int(player_x) - 1, int(player_x) + 2): for y in range(int(player_y) - 1, int(player_y) + 2): point_index = maze_helper.index_to_cell(self.maze, x, y) if maze_helper.check_cell(self.maze, int(point_index)) == 1: point = Point() wall_block = pygame.Rect((x * BLOCKSIZE_X, y * BLOCKSIZE_Y, BLOCKSIZE_X, BLOCKSIZE_Y)) point.x = wall_block.centerx point.y = wall_block.centery centers.append(point) walls.append(wall_block) pygame.draw.rect(self.display_surf, GREEN , wall_block, 0) pygame.display.update() return centers
# Make it a 1D maze my_maze = OccupancyGrid() my_maze.data = data my_maze.info.width = col my_maze.info.height = row display_surf = pygame.display.set_mode((windowWidth, windowHeight)) pygame.display.set_caption('Travel from Blue Square to Red Square') N = my_maze.info.height # number of rows M = my_maze.info.width # number of columns for index, pt in enumerate(my_maze.data): bx, by = maze_helper.get_i_j(my_maze, index) cell = maze_helper.check_cell(my_maze, index) if cell == 1: pygame.draw.rect( display_surf, PURPLE, (bx * BLOCKSIZE_X, by * BLOCKSIZE_Y, BLOCKSIZE_X, BLOCKSIZE_Y), 0) elif cell == 2: pygame.draw.rect( display_surf, BLUE, (bx * BLOCKSIZE_X, by * BLOCKSIZE_Y, BLOCKSIZE_X, BLOCKSIZE_Y), 0) elif cell == 3: pygame.draw.rect( display_surf, RED, (bx * BLOCKSIZE_X, by * BLOCKSIZE_Y, BLOCKSIZE_X, BLOCKSIZE_Y), 0)