Skip to content
Advertisement

Pygame, Collision between 2 objects in the same group

So, i am trying to create a game where aliens spawn from 3 specific places. Each Alien will spawn randomly in one of the 3. But there will always be at least one alien, that will spawn on top of another one. I want to delete that alien and spawn him randomly in another spawn point. If it is empty he will stay if not the process will be repeated. The thing is that i cannot find a way to detect collision of 2 objects that are in the same group.

I just started learning pygame so 1) My question may be stupid 2) My way of spawning probably is very inefficient

Here is the Alien class:

class Alien(pygame.sprite.Sprite):
def __init__(self):
    pygame.sprite.Sprite.__init__(self)
    self.image = pygame.Surface((80,60))
    self.image.fill(GREY)
    self.rect = self.image.get_rect()
    spawn_point1 = x1,y1 = -30, 70
    spawn_point2 = x2,y2 = -30, 150
    spawn_point3 = x3,y3 = -30, 230
    random_spawn = random.choice([spawn_point1,spawn_point2,spawn_point3])
    self.rect.center = random_spawn
    self.speedx = 10

def update(self):
    spawn_point1 = x1,y1 = -30, 70
    spawn_point2 = x2,y2 = -30, 150
    spawn_point3 = x3,y3 = -30, 230
    self.speedx = 10
    random_spawn = random.choice([spawn_point1,spawn_point2,spawn_point3])
    self.rect.x += self.speedx

    if self.rect.x > WIDTH + 20:
        self.rect.center = random_spawn

And here is the part where i detect collision(This part doesnt work)

aliens_col = pygame.sprite.groupcollide(aliens, aliens, True, False)

for i in aliens_col:
    alien = Alien()
    aliens.add(alien)
    all_sprites.add(aliens)

Advertisement

Answer

Here is an implementation of the Bounding Box test.

import random


class Rectangle:

    def __init__(self, height, width, x, y):
        self.height = height
        self.width = width
        self.x = x
        self.y = y

    def collided_with_another_rectangle(self, rect):
        """ Assumes rectangles are same size or that this rectangle is smaller than the other rectangle"""
        if self.x > (rect.x + rect.width):
            # Is to the right of the other rectangle
            return False
        elif (self.x + self.width) < rect.x:
            # is to the left of the other rectangle
            return False
        elif (self.y + self.height) < rect.y:
            # is above the other rectangle
            return False
        elif self.y > (rect.y + rect.height):
            # is below the other rectangle
            return False
        else:
            return True


collision_count = 0
for i in range(0, 1000):
    # Here I pick random locations on a 1000X1000 screen for the first rectangle 
    x1 = random.randint(0, 1000)
    y1 = random.randint(0, 1000)
    # Here I pick random locations on a 1000X1000 screen for the second rectangle 
    rect1 = Rectangle(100, 100, x1, y1)
    x2 = random.randint(0, 1000)
    y2 = random.randint(0, 1000)
    rect2 = Rectangle(100, 100, x2, y2)
    """
     I use the collided with another rectangle function to test if the first rectangle is above,below, 
     to the right or to the left of the other rectangle. If neither of these are true then the rectangles 
     have collided.
    """
    if rect1.collided_with_another_rectangle(rect2):
        collision_count += 1
        print("Rect1 X and Y:" + str(x1) + " " + str(y1))
        print("Rect2 X and Y:" + str(x2) + " " + str(y2))
        print("collided")

print("Collision Count:" + str(collision_count))
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement