Exemple #1
0
    def divide(self):
        self.divided = True
        cx = self.boundary.center.x
        cy = self.boundary.center.y
        w = self.boundary.w
        h = self.boundary.h

        ul_rect = Shapes.Rect(Shapes.Point(cx - w / 4., cy - h / 4.), w / 2.,
                              h / 2.)
        self.ul = QuadTree(ul_rect, self.capacity)
        ur_rect = Shapes.Rect(Shapes.Point(cx + w / 4., cy - h / 4.), w / 2.,
                              h / 2.)
        self.ur = QuadTree(ur_rect, self.capacity)
        ll_rect = Shapes.Rect(Shapes.Point(cx - w / 4., cy + h / 4.), w / 2.,
                              h / 2.)
        self.ll = QuadTree(ll_rect, self.capacity)
        lr_rect = Shapes.Rect(Shapes.Point(cx + w / 4., cy + h / 4.), w / 2.,
                              h / 2.)
        self.lr = QuadTree(lr_rect, self.capacity)

        for object in self.objects:
            self.ul.insert(object)
            self.ur.insert(object)
            self.ll.insert(object)
            self.lr.insert(object)

        self.objects = []
Exemple #2
0
                to_add += self.lr.getLocal(center, radius, lpoints, keys)

        return to_add

    def show(self, canvas):
        self.boundary.show(canvas, stroke=1)
        if (self.divided):
            self.ul.show(canvas)
            self.ur.show(canvas)
            self.ll.show(canvas)
            self.lr.show(canvas)


if __name__ == '__main__':
    p1 = Shapes.Point(400, 400)
    r1 = Shapes.Rect(p1, 800, 800)
    q1 = QuadTree(r1, 4)
    rad = 50

    pygame.init()
    gameDisplay = pygame.display.set_mode((800, 800))
    gameDisplay.fill((255, 255, 255))
    clock = pygame.time.Clock()

    points = [Shapes.Point(rx=800, ry=800, rvx=10, rvy=10) for i in range(200)]
    running = True
    while running:
        pygame.display.update()
        gameDisplay.fill((255, 255, 255))

        keys = pygame.key.get_pressed()
Exemple #3
0
width = 900
length = 900
pygame.init()
gameDisplay = pygame.display.set_mode((width, length))
gameDisplay.fill((255, 255, 255))
clock = pygame.time.Clock()
rad = 100

players = [player(gameDisplay, radius=rad, rpos=True) for i in range(nPlayers)]
opponents = [
    player(gameDisplay, color=(255, 0, 0), rpos=True, rvel=True, max_vel=2)
    for i in range(100)
]

p1 = Shapes.Point(width / 2., length / 2.)
r1 = Shapes.Rect(p1, width, length)

running = True
gens = 0
while running:
    gens += 1
    start_zone = Shapes.Rect(p1, w=100, h=100)
    for opponent in opponents:
        pos_i = Shapes.Point(opponent.x, opponent.y)
        while (start_zone.pointInside(pos_i)):
            opponent.x = random.uniform(0, width)
            opponent.y = random.uniform(0, length)
            pos_i = Shapes.Point(opponent.x, opponent.y)

    running1 = True
    pygame.init()