def _init_environment_map(self): self.environment_map = EnvironmentMap() # create moving block self.environment_map.create_moving_block( Point(GAME_SETTING.POINT_1[0][0], GAME_SETTING.POINT_1[0][1]), Point(GAME_SETTING.POINT_1[1][0], GAME_SETTING.POINT_1[1][1])) self.environment_map.create_moving_block( Point(GAME_SETTING.POINT_2[0][0], GAME_SETTING.POINT_2[0][1]), Point(GAME_SETTING.POINT_2[1][0], GAME_SETTING.POINT_2[1][1]))
def __init__(self): self.left_barrier_line = [] self.right_barrier_line = [] for (x, y) in GAME_SETTING.BARRIER_LEFT_LINE: self.left_barrier_line.append(Point(x, y)) for (x, y) in GAME_SETTING.BARRIER_RIGHT_LINE: self.right_barrier_line.append(Point(x, y)) self.build_destination_line() self.total_length = utils.calculate_length(self.left_barrier_line) self.block_list = []
def _calculate_dist_block(self, direction, env_map: EnvironmentMap): dist = GAME_SETTING.MAX_OBSERVATION for block in env_map.block_list: b_l_t = Point(block.position.x - block.size / 2, block.position.y - block.size / 2) b_l_b = Point(block.position.x - block.size / 2, block.position.y + block.size / 2) b_r_t = Point(block.position.x + block.size / 2, block.position.y - block.size / 2) b_r_b = Point(block.position.x + block.size / 2, block.position.y + block.size / 2) temp = utils.intersect_ray_line( self.position, direction, [b_l_t, b_l_b, b_r_b, b_r_t, b_l_t]) if temp < dist: dist = temp return dist
def prepare(self): self.game_status = GameStatus.NotStarted self.__show_cover() self.prepare_center = Point(GAME_SETTING.GAME_SCREEN_WIDTH / 2, GAME_SETTING.GAME_SCREEN_HEIGHT / 2) if self.game_status == GameStatus.NotStarted: self._perspective_tracking(self.prepare_center) self.__prepare_left_barrier() self.__prepare_right_barrier() self.environment_map.build_destination_line() # TODO: draw obstacle objects self.game_status = GameStatus.Running
def intersect_ray_line(p: Point, direction: float, segments: List[Point]) -> float: # ray(t) = P + t * n (t >= 0) n = Point(math.cos(direction), math.sin(direction)) min_t = float('inf') for i in range(len(segments) - 1): a, b = segments[i], segments[i + 1] # Does segment AB intersect line of the ray? if n.cross(a - p) * n.cross(b - p) > 0: continue # Now find t such that P + t * n on AB <=> (P + t * n - A) x AB = 0 # <=> AP x AB + t * n x AB = 0 ap = p - a ab = b - a t = -ap.cross(ab) / n.cross(ab) if t < 0: # On the negative side, so does not really intersects continue min_t = min(min_t, t) return min_t
def reset_car(self): self.direction = 0 self.v = GAME_SETTING.GAME_CAR_V0 self.w = 0 self.acc_v = GAME_SETTING.GAME_CAR_V_ACC self.acc_w = 0 self.position = Point(self.start_pos.x, self.start_pos.y) self.width = GAME_SETTING.GAME_CAR_WIDTH self.height = GAME_SETTING.GAME_CAR_HEIGHT self.observation = GAME_SETTING.INIT_OBSERVATION self.reward = 0 self.terminal = CarTerminal.Running
def _check_crash(self, env_map: EnvironmentMap): l_f = self.get_left_front_point() r_f = self.get_right_front_point() l_b = self.get_left_behind_point() r_b = self.get_right_behind_point() for i in range(len(env_map.left_barrier_line) - 1): line = Line(env_map.left_barrier_line[i], env_map.left_barrier_line[i + 1]) if utils.is_intersect(line, l_f, l_b, r_f, r_b): return True for i in range(len(env_map.right_barrier_line) - 1): line = Line(env_map.right_barrier_line[i], env_map.right_barrier_line[i + 1]) if utils.is_intersect(line, l_f, l_b, r_f, r_b): return True for block in env_map.block_list: b_l_t = Point(block.position.x - block.size / 2, block.position.y - block.size / 2) b_l_b = Point(block.position.x - block.size / 2, block.position.y + block.size / 2) b_r_t = Point(block.position.x + block.size / 2, block.position.y - block.size / 2) b_r_b = Point(block.position.x + block.size / 2, block.position.y + block.size / 2) line_list = [ Line(b_l_t, b_l_b), Line(b_l_t, b_r_t), Line(b_r_b, b_r_t), Line(b_r_b, b_l_b) ] for line in line_list: if utils.is_intersect(line, l_f, l_b, r_f, r_b): return True return False
def __prepare_left_barrier(self): # draw left barrier screen_width = GAME_SETTING.GAME_SCREEN_WIDTH screen_height = GAME_SETTING.GAME_SCREEN_HEIGHT map_width = GAME_SETTING.GAME_MAP_WIDTH map_height = GAME_SETTING.GAME_MAP_HEIGHT while True: enter_next_step = False for event in pygame.event.get(): if event.type == pygame.QUIT: exit() if event.type == KEYDOWN: keys = pygame.key.get_pressed() if keys[pygame.K_RETURN]: enter_next_step = True elif event.key == pygame.K_LEFT: if self.prepare_center.x >= screen_width / 2: self.prepare_center.x -= 20 self._perspective_tracking(self.prepare_center) print("left") print(self.prepare_center.x, self.prepare_center.y) elif event.key == pygame.K_RIGHT: if self.prepare_center.x <= map_width - screen_width / 2: self.prepare_center.x += 20 self._perspective_tracking(self.prepare_center) elif event.key == pygame.K_UP: if self.prepare_center.y >= screen_height / 2: self.prepare_center.y -= 20 self._perspective_tracking(self.prepare_center) elif event.key == pygame.K_DOWN: if self.prepare_center.y <= map_height - screen_height / 2: self.prepare_center.y += 20 self._perspective_tracking(self.prepare_center) if event.type == MOUSEBUTTONDOWN: pos = pygame.mouse.get_pos() current_point = Point(pos[0] + self.offset_x, pos[1] + self.offset_y) if event.button == pygame.BUTTON_LEFT: self.environment_map.extend_left_barrier_line( current_point) elif event.button == pygame.BUTTON_RIGHT: self.environment_map.cut_left_barrier_line() if enter_next_step: break illustration_text = f"click left button to draw left barriers, right button to delete, enter to next." self.render_prepare_ui(illustration_text) pygame.time.delay(GAME_SETTING.GAME_STEP_INTERVAL)
def rotate(p, a): return Point( p.x * math.cos(a) - p.y * math.sin(a), p.x * math.sin(a) + p.y * math.cos(a), )
def _init_user_car(self): self.user_car = Car( Point(GAME_SETTING.GAME_USER_CAR_START_POSX, GAME_SETTING.GAME_USER_CAR_START_POSY))
def _init_player_car(self): self.player_car = Car( Point(GAME_SETTING.GAME_PLAYER_CAR_START_POSX, GAME_SETTING.GAME_PLAYER_CAR_START_POSY))
def get_right_behind_point(self): return self.position + utils.rotate( Point(0 - self.height / 2, 0 - self.width / 2), self.direction)
def get_left_front_point(self): return self.position + utils.rotate( Point(self.height / 2, self.width / 2), self.direction)