def test_delete9(self): tree = Tree2D(10, 20) tree.insert(6, 30) tree.insert(20, 8) tree.insert(7, 15) tree.insert(5, 40) tree.insert(15, 6) tree.insert(12, 10) tree.insert(1, 1) tree.insert(4, 50) tree.insert(7, 40) tree.insert(10, 10) tree.insert(12, 35) tree.insert(3, 20) tree.insert(15, 15) print(tree) tree.delete(20, 8) tree.delete(7, 15) tree.delete(10, 20) print(tree) corr_tree = Tree2D(10, 10) corr_tree.insert(6, 30) corr_tree.insert(12, 10) corr_tree.insert(1, 1) corr_tree.insert(5, 40) corr_tree.insert(15, 6) corr_tree.insert(12, 35) corr_tree.insert(3, 20) corr_tree.insert(4, 50) corr_tree.insert(7, 40) corr_tree.insert(15, 15) self.assertEqual(tree, corr_tree)
def insertInTreeOfCollision(tree: Tree2D, newSquare: Square, approx): newPoints = [] for i in range(approx + 1): newPoints.append((newSquare.pointList[0][0] + newSquare.side * i // approx, newSquare.pointList[0][1])) newPoints.append((newSquare.pointList[1][0], newSquare.pointList[1][1] + newSquare.side * i // approx)) newPoints.append((newSquare.pointList[2][0] - newSquare.side * i // approx, newSquare.pointList[2][1])) newPoints.append((newSquare.pointList[3][0], newSquare.pointList[3][1] - newSquare.side * i // approx)) tree.insert_list(newPoints, newSquare)
def test_delete3(self): tree = Tree2D() tree.insert(10, 10) tree.insert(6, 30) tree.insert(12, 10) tree.delete(6, 30) corr_tree = Tree2D() corr_tree.insert(10, 10) corr_tree.insert(12, 10) self.assertEqual(tree, corr_tree)
def test_eq1(self): tree = Tree2D() tree.insert(10, 10) tree.insert(6, 30) tree.insert(12, 10) corr_tree = Tree2D() corr_tree.insert(10, 10) corr_tree.insert(6, 30) corr_tree.insert(12, 10) self.assertEqual(corr_tree, tree)
def test_delete7(self): # we do not have right son, but have left tree = Tree2D() tree.insert(10, 10) tree.insert(6, 30) tree.insert(5, 40) tree.insert(1, 1) tree.delete(10, 10) corr_tree = Tree2D() corr_tree.insert(1, 1) corr_tree.insert(6, 30) corr_tree.insert(5, 40) self.assertEqual(tree, corr_tree)
def __create_test_tree1__(self): tree = Tree2D() tree.insert(10, 10) tree.insert(6, 30) tree.insert(12, 10) tree.insert(1, 1) tree.insert(5, 40) tree.insert(15, 6) tree.insert(12, 35) return tree
def test_delete5(self): # we have right son tree = self.__create_test_tree1__() tree.delete(6, 30) corr_tree = Tree2D() corr_tree.insert(10, 10) corr_tree.insert(5, 40) corr_tree.insert(12, 10) corr_tree.insert(1, 1) corr_tree.insert(15, 6) corr_tree.insert(12, 35) self.assertEqual(tree, corr_tree)
def mainDraw(): # TODO we can store points of squares in Tree2d, or points of circle. What will be effective? # I think store points of circle will be effective, cos so count of requests to areas(squares) will be less FPS = 30 approx = 50 # count of collision points on each side area = pygame.display.set_mode((WIDTH, HEIGHT)) areaColor = (150, 150, 150) area.fill(areaColor) spawnPlace = (WIDTH // 2, HEIGHT // 2) spawnPlace = (167, 364) ballsList = [] squareList = [Square(area, (250, 250))] treeOfCollision = Tree2D() insertInTreeOfCollision(treeOfCollision, squareList[0], approx) # border of screen treeOfCollision.insert_list([(-1, i) for i in range(HEIGHT)] + [(i, -1) for i in range(WIDTH)]) treeOfCollision.insert_list([(WIDTH + 1, i) for i in range(HEIGHT)] + [(i, HEIGHT + 1) for i in range(WIDTH)]) for _ in range(0): # angle = random.random() * 2 * math.pi angle = 0 # 3 * np.pi / 2 + np.pi / 6 sp = (int(random.random() * WIDTH/3), int(random.random() * HEIGHT/3.5)) # sp = (260, 100) dx, dy = math.sin(angle + math.pi / 2), math.cos(angle + math.pi / 2) ball = Ball(area, sp, dx, dy) ballsList.append(ball) for i in range(10): sq = Square(area, (i * 51, 300)) squareList.append(sq) insertInTreeOfCollision(treeOfCollision, sq, approx) sq = Square(area, (i * 51, 500)) squareList.append(sq) insertInTreeOfCollision(treeOfCollision, sq, approx) while True: area.fill(areaColor) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() if event.type == pygame.MOUSEBUTTONUP: if event.button == 1: angle = angleBetweenTwoPoints(spawnPlace, event.pos) dx, dy = math.sin(angle + math.pi / 2), math.cos(angle + math.pi / 2) print(f"New ball №{len(ballsList)} with angle:", angle) ballsList.append(Ball(area, spawnPlace, dx, dy)) if event.button == 2: squareList.append(Square(area, event.pos)) print(f"New square №{len(squareList)} with pos:", squareList[-1].pos) insertInTreeOfCollision(treeOfCollision, squareList[-1], approx) if event.button == 3: spawnPlace = (event.pos[0], event.pos[1]) print("New Spawn:", spawnPlace) for index, ball in enumerate(ballsList): hit = isCollision(ball, treeOfCollision) ball.move(ball.deltas[0], ball.deltas[1]) if ball.pos[0] > WIDTH or ball.pos[0] < 0 or ball.pos[1] > HEIGHT or ball.pos[1] < 0: ballsList.pop(index) if hit is not None: hit.lifePoints -= 1 if hit.lifePoints <= 0: deleteInTreeOfCollision(treeOfCollision, hit, approx) for ball in ballsList: ball.draw() for index, square in enumerate(squareList): if square.lifePoints <= 0: squareList[index] = None else: square.draw() for _ in range(squareList.count(None)): squareList.remove(None) font = pygame.font.Font(None, 20) fpsShower = font.render(str(int(timer.get_fps())), True, pygame.Color('white')) area.blit(fpsShower, (WIDTH - 20, 7)) timer.tick(FPS) pygame.display.update()
def deleteInTreeOfCollision(tree: Tree2D, square: Square, approx): for i in range(approx + 1): tree.delete(square.pointList[0][0] + square.side * i // approx, square.pointList[0][1]) tree.delete(square.pointList[1][0], square.pointList[1][1] + square.side * i // approx) tree.delete(square.pointList[2][0] - square.side * i // approx, square.pointList[2][1]) tree.delete(square.pointList[3][0], square.pointList[3][1] - square.side * i // approx)
def test_delete2(self): tree = Tree2D(10, 10) tree.delete(10, 10) corr_tree = Tree2D() self.assertEqual(tree, corr_tree)
def test_delete1(self): tree = Tree2D() tree.delete(0, 0) corr_tree = Tree2D() self.assertEqual(tree, corr_tree)
def test_eq3(self): tree = Tree2D(1, 1) corr_tree = Tree2D() corr_tree.insert(1, 1) self.assertEqual(corr_tree, tree)
def test_eq2(self): tree = Tree2D() corr_tree = Tree2D() self.assertEqual(corr_tree, tree)
def test_of_request_area0_3(self): tree = Tree2D(1, 1) points = tree.request_of_area(10, 1, 1) corr_points = [] self.assertListEqual(points, corr_points)