Beispiel #1
0
    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
Beispiel #2
0
	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))