def load_map(path): color_to_material = _load_color_to_value_mapping( os.path.join(path, 'materials.json'), Material) color_to_object = _load_color_to_value_mapping( os.path.join(path, 'objects.json'), Object) materials, width = _load_map_layer(os.path.join(path, 'materials.png'), color_to_material, default=Material.FLOOR) object_layer, object_width = _load_map_layer(os.path.join( path, 'objects.png'), color_to_object, default=None) square_to_center_offset = Direction(0.5, 0.5) objects = [] def to_position(index): return Position(index % object_width, int(index / object_width)) for index, object in enumerate(object_layer): if object is not None: position = to_position(index) + square_to_center_offset objects.append((object, position)) return Map(materials, objects, width)
def move_drone(current_map: Map, path, speed=0.5): screen = initialize_gui((current_map.m * 20, current_map.n * 20)) drona = pygame.image.load("drona.png") brick = pygame.Surface((20, 20)) brick.fill(GREEN) seen_square = pygame.Surface((20, 20)) seen_square.fill(RED) sensor = pygame.image.load("sensor.png") for square_index in range(len(path)): for event in pygame.event.get(): if event.type == pygame.QUIT: break screen.blit(image(current_map), (0, 0)) for j in range(square_index + 1): screen.blit(brick, (path[j][1] * 20, path[j][0] * 20)) for square in path[:square_index + 1]: if current_map.is_tuple_sensor(square): i, j = square[0], square[1] neighbours = [[i, j]] * 4 for _ in range(square[2]): for direction_index, direction in enumerate(DIRECTIONS): new_neighbour = deepcopy(neighbours[direction_index]) new_neighbour[0] += direction[0] new_neighbour[1] += direction[1] if (current_map.is_tuple_in_bounds(new_neighbour) and not current_map.is_tuple_wall(new_neighbour)): neighbours[direction_index] = new_neighbour screen.blit( seen_square, (new_neighbour[1] * 20, new_neighbour[0] * 20)) for i in range(current_map.n): for j in range(current_map.m): if current_map.surface[i][j] == SENSOR: screen.blit(sensor, (j * 20, i * 20)) screen.blit(drona, (path[square_index][1] * 20, path[square_index][0] * 20)) pygame.display.flip() time.sleep(speed) pygame.quit()
class Command: def __init__(self): self.map = Map() self.map.random_map() self.controller = Controller(Drone(PARAM_BATTERY), self.map) def run(self): solution: Optional[Ant] = None for _ in range(PARAM_ITERATIONS): current_solution = self.controller.iterate() if not solution or solution.check_coverage( ) < current_solution.check_coverage(): solution = current_solution print(solution.check_coverage()) move_drone(self.map, solution.path)
def deserialize(Type, v): if Type in [float, int, NoneType, str]: result = v elif issubclass(Type, enum.Enum): result = Type[v] elif Type in [Direction, Position]: x, y = v result = Type(x=deserialize(float, x), y=deserialize(float, y)) elif Type == Map: result = Map( materials=[deserialize(Material, m) for m in v['materials']], objects=[(deserialize(Object, o), deserialize(Position, p)) for o, p in v['objects']], width=deserialize(int, v['width'])) elif Type == Player: result = Player(name=deserialize(str, v['name']), position=deserialize(Position, v['position']), forward=deserialize(Direction, v['forward'])) else: raise Exception("Cannot deserialize object of type {}".format(Type)) assert isinstance(result, Type), "{} is not a {}".format(v, Type) return result
from use_case import * from domain import Direction, initial_input, Map, Material, Position MAP = Map(materials=[ Material.FLOOR, Material.FLOOR, Material.FLOOR, Material.FLOOR, Material.FLOOR, Material.FLOOR, Material.FLOOR, Material.FLOOR, Material.FLOOR ], objects=[], width=3) PLAYER = Player(name='test', position=Position(1.5, 1.5), forward=Direction(0.0, -1.0)) def test_move_forward(): state = initial_state(initial_input, PLAYER, MAP, 3, 5) state, _ = handle_event(state, 'input', initial_input._replace(forward=True)) state, _ = handle_event(state, 'tick', 0.070) assert state.player.position.x == 1.5 assert state.player.position.y < 1.5 assert state.player.forward.x == 0.0 assert state.player.forward.y == -1.0 def test_turn_right(): state = initial_state(initial_input, PLAYER, MAP, 3, 5) state, _ = handle_event(state, 'input', initial_input._replace(turn_right=True)) state, _ = handle_event(state, 'tick', 0.070)
global root root.quit root.destroy if (command == 'look'): display.show("""There's an old book on the ground. It shakes violently. You want to open it, but your instinct tells you to leave it""") # if (command == 'open'): # display.show("""You are swallowed whole by the book. The end""") # if (command == 'look'): # display.show("""The title reads 'The Book of Life'""") # if (command == 'open'): # display.show("""You are swallowed whole by the book. The end""") # music.kill() locations = Location.bootstrap() map = Map() # music = subprocess.Popen(["afplay", "./soundtrack.mp3"]) command_handler = CommandHandler() display = Display(root, command_handler) current_location = map.start() display.show_location(current_location) root.mainloop()
def __init__(self, controller): self.controller = controller self.m = Map() self.drone = Drone(0, 0)
class UserInterface: def __init__(self, controller): self.controller = controller self.m = Map() self.drone = Drone(0, 0) @staticmethod def menu(): print("Pathfinding algorithms:\n" " 1. A*\n" " 2. Greedy\n") option = input("Input option:\n>>>") return option def main(self): option = self.menu() # m.randomMap() # m.saveMap("test2.map") self.m.loadMap("test1.map") # initialize the pygame module pygame.init() # load and set the logo logo = pygame.image.load("logo32x32.png") pygame.display.set_icon(logo) pygame.display.set_caption("Path in simple environment") # we position the drone somewhere in the area x = randint(0, 19) y = randint(0, 19) while self.m.surface[x][y] != 0: x = randint(0, 19) y = randint(0, 19) # x = 14 # y = 18 # create drona self.drone = Drone(x, y) # destination x_d = randint(0, 19) y_d = randint(0, 19) while self.m.surface[x_d][y_d] != 0: x_d = randint(0, 19) y_d = randint(0, 19) # x_d = 1 # y_d = 7 # # print((x,y)) # print((x_d, y_d)) # print("Old Time: 0.0005340000000018108") self.m.surface[x][y] = 2 # mark start self.m.surface[x_d][y_d] = 3 # mark destination # create a surface on screen that has the size of 400 x 480 screen = pygame.display.set_mode((400, 400)) screen.fill(WHITE) # define a variable to control the main loop running = True # main loop while running: # event handling, gets all event from the event queue for event in pygame.event.get(): # only do something if the event is of type QUIT if event.type == pygame.QUIT: # change the value to False, to exit the main loop running = False # if event.type == KEYDOWN: # self.drone.move(self.m) # this call will be erased screen.blit(self.drone.mapWithDrone(self.m.image()), (0, 0)) pygame.display.flip() if option == "1": path = self.controller.searchAStar(self.m, self.drone, x, y, x_d, y_d) else: path = self.controller.searchGreedy(self.m, self.drone, x, y, x_d, y_d) if path is not None: screen.blit(self.controller.displayWithPath(self.m.image(), path), (0, 0)) else: print("No path") pygame.display.flip() time.sleep(3) pygame.quit()
def __init__(self): self.map = Map() self.map.random_map() self.controller = Controller(Drone(PARAM_BATTERY), self.map)