def __init__(self, name='Taxi Agent Editor', use_ai=False, map_name=None): self.name = name self.map_name = map_name self.use_ai = use_ai pygame.init() self.evManager = Event.EventManager() if self.name == 'Taxi Agent': self.player = Player.Player(self.evManager) self.character = Car.Car(Settings.CAR_IMAGE, self.evManager) self.map = Map.Map(self.evManager) if self.map_name is not None: self.map.load_map(self.map_name) self.mode = Game.Mode(Game.Mode.PLAY_MODE) else: self.mode = Game.Mode(Game.Mode.POINT_EDITING) self.game = Game.Game(self.player, self.character, self.map, self.mode, self.evManager) self.gameController = Game.GameController(self.game, self.evManager) self.gameView = None self.evManager.post(Event.ChangeModeEvent(self.mode))
def step(self, action): self.evManager.post( Event.MovePlayerEvent(Vector2(action[0], action[1]))) now = pygame.time.get_ticks() delta = (now - self.previous) / 1000.0 self.previous = now self.evManager.post(Event.TickEvent(delta)) car = self.game.character new_state = car.get_state() if car.is_on_path(): self.reward = car.velocity[0] / car.max_front_velocity else: self.reward = -1.0 self.score += self.reward done = not self.keepGoing self.step_counter += 1 if self.step_counter == self.life_span: self.keepGoing = False if not car.is_on_path(): self.step_outside_path_counter += 1 if self.step_outside_path_counter == self.life_span_outside_path: self.keepGoing = False return new_state, self.reward, done, self.info
def create_point(self): # Add a new point mouse = pygame.mouse.get_pos() if self.is_space_available(mouse): self.points.append(Node.from_coord(mouse)) self.evManager.post(Event.MapUpdatedEvent(self))
def run(self): while self.keepGoing: now = pygame.time.get_ticks() delta = (now - self.previous) / 1000.0 self.previous = now self.evManager.post(Event.TickEvent(delta)) pygame.quit()
def remove_point(self): # Remove the point and all lines connected to it mouse = pygame.mouse.get_pos() point_to_remove = self.get_point_selected(mouse) if point_to_remove is not None: self.points.remove(point_to_remove) for line in reversed(self.lines): if point_to_remove in line: self.lines.remove(line) self.evManager.post(Event.MapUpdatedEvent(self))
def create_line(self): for point in self.points: mouse = pygame.mouse.get_pos() if Map.is_point_selected(point, mouse): # First point selected if self.firstPoint is None: self.firstPoint = point # Second point selected elif point != self.firstPoint and not self.is_crossing( self.get_building_line()): # Creation of a new line self.lines.append([self.firstPoint, point]) self.firstPoint = None self.evManager.post(Event.MapUpdatedEvent(self))
def load_map(self, name): self.reset() with open(name, 'r') as file: for point in file.readline().split(' ')[:-1]: point = point.split(',') point = Node(int(point[0]), int(point[1])) self.points.append(point) for line in file.readlines(): new_line = [] for point in line.split(' ')[:-1]: point = point.split(',') point = Node(int(point[0]), int(point[1])) new_line.append(point) self.lines.append(new_line) self.evManager.post(Event.MapUpdatedEvent(self))
def notify(self, event): if isinstance(event, Event.TickEvent): for event in pygame.event.get(): ev = None # Quit event from pygame if event.type == pygame.QUIT: ev = Event.QuitEvent() # If keyboard key is pressed elif event.type == pygame.KEYDOWN: if event.key == Settings.KEY_QUIT: ev = Event.QuitEvent() elif event.key == Settings.KEY_POINT_EDITING: ev = Event.ChangeModeEvent(Game.Mode(Game.Mode.POINT_EDITING)) elif event.key == Settings.KEY_LINE_EDITING: ev = Event.ChangeModeEvent(Game.Mode(Game.Mode.LINE_EDITING)) elif event.key == Settings.KEY_PLAY_MODE: ev = Event.ChangeModeEvent(Game.Mode(Game.Mode.PLAY_MODE)) elif event.key == Settings.KEY_TOGGLE_DEBUG: ev = Event.ToggleDebugEvent() elif event.key == Settings.KEY_SAVE_MAP: ev = Event.SaveMapEvent(input('Map name : ')) elif event.key == Settings.KEY_LOAD_MAP: ev = Event.LoadMapEvent(input('Map name : ')) # If mouse is pressed elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == Settings.MOUSE_CREATE: ev = Event.CreationEvent() elif event.button == Settings.MOUSE_REMOVE: ev = Event.RemovingEvent() if ev is not None: self.evManager.post(ev) # Keyboard inputs that has to be maintained key_pressed = pygame.key.get_pressed() direction = Vector2(0, 0) if key_pressed[Settings.KEY_MOVE_FRONT]: direction += Vector2(1, 0) if key_pressed[Settings.KEY_MOVE_BACK]: direction += Vector2(-1, 0) if key_pressed[Settings.KEY_MOVE_LEFT]: direction += Vector2(0, 1) if key_pressed[Settings.KEY_MOVE_RIGHT]: direction += Vector2(0, -1) if direction.x != 0 or direction.y != 0: self.evManager.post(Event.MovePlayerEvent(direction))