def test_path_intersect_no_mirror_3(self): anchor_points_0 = [ Point(600, 300), Point(763, 232), Point(760, 248), Point(763.3812601455536, 279.9618353762123), Point(759.093503294539, 324.7570933974201), Point(752.2129530873079, 369.22796058983914), Point(728.8190694506658, 407.66916515467096) ] anchor_points_1 = [ Point(100, 100), Point(638, 170), Point(727, 212), Point(594, 357), Point(476, 177), Point(261, 207), Point(167.76045105799363, 197.0495733490862) ] path = Path.from_points(anchor_points_0) path2 = Path.from_points(anchor_points_1) intersections = path.intersects(path2) intersections_2 = path2.intersects(path) show_paths_2([path, path2], intersections + intersections_2) self.assertEqual(len(intersections), len(intersections_2))
def init_dead_ends_0(): positions = {0: (0, 0), 1: (30, 0), 2: (30, 20), 3: (60, 0)} points = init_points(positions) paths = [ Path.from_points([points[0], points[1]]), Path.from_points([points[0], Point(30, -10), points[3]]), Path.from_points([points[1], points[2]]), Path.from_points([points[1], points[3]]) ] return points, init_edge_map(paths)
def test_path_intersect_no_mirror_2(self): anchor_points_0 = [ GraphicsPoint(100, 100), Point(473.7132417184837, 208.4978496002687), GraphicsPoint(457, 125) ] anchor_points_1 = [ GraphicsPoint(500, 400), Point(573, 371), Point(550, 177), Point(586, 146), Point(337, 129), Point(536, 22), Point(689, 67), Point(728, 344), Point(650, 472) ] p_start = GraphicsPoint(100, 100) p_end = GraphicsPoint(457, 125) path = Path([ GraphicsBezier(p_start, p_end, Point(278.5, 112.5), Point(404.8987230708711, 188.92401096739675)) ], p_start, p_end) path2 = Path.from_points(anchor_points_1) intersections = path.intersects(path2) intersections_2 = path2.intersects(path) show_paths_2([path, path2], intersections + intersections_2) self.assertEqual(len(intersections), len(intersections_2))
def test_path_intersect_5(self): anchor_points_0 = [ GraphicsPoint(403.01382054213786, 117.09675232181752), GraphicsPoint(450, 100) ] anchor_points_1 = [ GraphicsPoint(422.05639822420903, 417.833171793951), GraphicsPoint(450.55461679819734, 407.4265792951324), GraphicsPoint(500, 400) ] path = Path.from_points(anchor_points_0) path2 = Path.from_points(anchor_points_1) intersections = path.intersects(path2) show_paths_2([path, path2], intersections) self.assertEqual(False, len(intersections) > 0)
def test_path_intersect_4(self): anchor_points_0 = [ GraphicsPoint(440.3994196807172, 149.06963274300102), GraphicsPoint(450, 100) ] anchor_points_1 = [ GraphicsPoint(450, 100), GraphicsPoint(402.0865680692103, 419.70860894843224), GraphicsPoint(422.05639822420903, 417.833171793951) ] path = Path.from_points(anchor_points_0) path2 = Path.from_points(anchor_points_1) intersections = path.intersects(path2) show_paths_2([path, path2], intersections) self.assertEqual(False, len(intersections) > 0)
def test_path_intersect_3(self): anchor_points_0 = [ GraphicsPoint(100, 100), Point(376, 61), Point(639, 217), GraphicsPoint(546, 357) ] anchor_points_1 = [ GraphicsPoint(450, 100), GraphicsPoint(440.3994196807172, 149.06963274300102) ] path = Path.from_points(anchor_points_0) path2 = Path.from_points(anchor_points_1) intersections = path2.intersects(path) show_paths_2([path, path2], intersections) self.assertEqual(False, len(intersections) > 0)
def test_path_self_no_intersect(self): start_end = GraphicsPoint(376, 95) anchor_points = [ start_end, Point(154, 199), Point(302, 315), Point(493, 120), start_end ] path = Path.from_points(anchor_points) intersections = path.self_intersections() show_paths_2([path], intersections) self.assertEqual(0, len(intersections))
def test_path_intersect_no_mirror(self): anchor_points_0 = [ GraphicsPoint(450, 100), Point(473.7132417184837, 208.4978496002687), GraphicsPoint(466, 268) ] anchor_points_1 = [ GraphicsPoint(100, 100), Point(230, 177), Point(548, 178), GraphicsPoint(701, 347) ] path = Path.from_points(anchor_points_0) path2 = Path.from_points(anchor_points_1) intersections = path.intersects(path2) intersections_2 = path2.intersects(path) show_paths_2([path, path2], intersections + intersections_2) self.assertEqual(len(intersections), len(intersections_2))
def init_connected_faces(): positions = {0: (0, 0), 1: (30, 0), 2: (50, 0), 3: (70, 0), 4: (90, 0)} points = init_points(positions) paths = [ Path.from_points([points[0], points[1]]), Path.from_points([points[0], Point(15, -10), points[1]]), Path.from_points([points[1], points[2]]), Path.from_points([points[2], points[3]]), Path.from_points([points[3], points[4]]), Path.from_points([points[3], Point(15, -10), points[4]]) ] return points, init_edge_map(paths)
def init_onion_graph(): positions = { 0: (0, 0), 1: (0, 30), 2: (0, -20), 3: (0, 10), 4: (0, 20), 5: (0, -10) } points = init_points(positions) paths = [ Path.from_points([points[0], Point(-10, 5), points[3]]), Path.from_points([points[0], Point(10, 5), points[3]]), Path.from_points([points[0], points[5]]), Path.from_points([points[5], points[2]]), Path.from_points([points[1], Point(-30, 0), points[2]]), Path.from_points([points[1], Point(30, 0), points[2]]), Path.from_points([points[1], points[4]]), Path.from_points([points[3], points[4]]), ] return points, init_edge_map(paths)
def run_game(self, initial_points, initial_paths): self.set_initial_state(initial_points, initial_paths) last_mouse_pos = (0, 0) spacing = " " legend_text = "Esc: cancel path" + spacing + "Right Click: Path suggestion" + spacing + "Left Click: Draw path" + spacing + "Backspace: Main menu" legend = Label(0, 0, 800, 30, pygame.SRCALPHA, pygame.SRCALPHA, 0, legend_text, 18, gc.UI_BUTTON_TEXT_COLOR) player_1_label = Label(0, 0, 60, 30, gc.PLAYER_1_COLOR, gc.BLACK, 2, "Player 1", 20, gc.UI_BUTTON_TEXT_COLOR) player_2_label = Label(gc.WINDOW_WIDTH - 60, 0, 60, 30, gc.PLAYER_2_COLOR, gc.PLAYER_2_COLOR, 2, "Player 2", 20, gc.UI_BUTTON_TEXT_COLOR) surfaces = [] surfaces.extend( [legend.draw(), player_1_label.draw(), player_2_label.draw()]) if self.game_over(self.base_region): self.update_screen([]) return (State.GAME_OVER, [initial_points, initial_paths]) while True: current_mouse_pos = pygame.mouse.get_pos() surfaces.extend( [legend.draw(), player_1_label.draw(), player_2_label.draw()]) for event in pygame.event.get(): # exit via X in window corner if event.type == pygame.QUIT: sys.exit() # key pressed if event.type == pygame.KEYDOWN: if event.key == pygame.K_BACKSPACE: # self.SCREEN.fill(gc.BACKGROUND_COLOR) self.hard_reset() return (State.MAIN_MENU, []) self.key_event_handler(event) # mouse clicked if event.type == pygame.MOUSEBUTTONDOWN: mouse_point = Point(*event.pos) current_region = self.base_region.find_region( Point(*event.pos)) # p is a Point if collision exists, else None p = self.point_collision(current_region.game_points, event.pos) self.update_screen(surfaces) # right click = suggest a path (if point pressed is GraphicsPoint if event.button == 3 and p: self.suggest_points.append(p) if len(self.suggest_points) == 2: suggested_path_points = self.find_path() # User canceled if not suggested_path_points: self.soft_reset() break self.finalize_path(suggested_path_points) self.change_turn(player_1_label, player_2_label) # check for game over state if self.game_over(self.base_region): self.update_screen([]) return (State.GAME_OVER, [initial_points, initial_paths]) # reset control variables self.soft_reset() break elif event.button == 3: break # Cant draw straight lines by pressing twice in the same pixel if len(self.preview_points) > 0 and mouse_point.equals( self.preview_points[-1]): break # point collision and not drawing a path already if p and not self.creating_path: self.preview_path = None self.all_intersections = [] self.creating_path = True self.preview_points = [p] self.preview_path = Path.from_points( self.preview_points + [Point(*event.pos)]) # no point collision and creating path already elif not p and self.creating_path: self.preview_points.append(Point(*event.pos)) # point collision and creating path already elif p and self.creating_path: if p == self.preview_points[0] and len( self.preview_points) == 1: self.soft_reset() break self.preview_points.append(p) self.preview_path = Path.from_points( self.preview_points) (self.all_intersections, valid_path) = self.validate_path(self.preview_path) # path is not valid, show intersections if not valid_path: self.creating_path = False self.preview_path.change_color(gc.RED) self.all_intersections = self.points_to_graphicspoints( self.all_intersections, gc.BLACK, 5) self.soft_reset() break # Split path in two and calculate new point self.finalize_path(self.preview_points) self.change_turn(player_1_label, player_2_label) if self.game_over(self.base_region): self.update_screen([]) return (State.GAME_OVER, [initial_points, initial_paths]) self.soft_reset() if self.preview_points and self.creating_path: cur_point = Point(*pygame.mouse.get_pos()) self.preview_path.redraw(self.preview_points + [cur_point], self.get_color()) # Update the screen if the cursor has moved if last_mouse_pos != current_mouse_pos: self.SCREEN.fill(gc.BACKGROUND_COLOR) last_mouse_pos = current_mouse_pos self.update_screen(surfaces) pygame.display.update() surfaces = [] # controls fps self.CLOCK.tick(gc.FPS)
def build_complete_path(self, node): path_points = [] while node: path_points.append(node.point) node = node.parent return path_points, Path.from_points(path_points)