Search code examples
pythonpygamepygame-surface

Unable to detect multiple Rect Collisions in Pygame


I'm been working on a simple Pygame project that involves simulating motion sensor lights in open areas, and have created multiple instances of delivery riders, smokers and civilians. When users are within a certain distance, the lights on the ground would light up. The problem I faced was, when objects came into collision with the drivers, colliderect did not trigger at all (tested with print statements). Ideally all the civilians should collide and reflect off one another except the driver and smokers.

Here is a recording of my virtual prototype: LUmen Simulator

Display setup and distance function

screen = width,height = 800,600
fps = 60 
cellsize = 50 
padding = 40 # create identations from the window 
rows = cols = (width - 50) // cellsize 
print(rows, cols) 

def euclid_dist(mX, mY, x, y): # distance to grid 
        dist = math.sqrt((mX - x)**2 + (mY - y)**2)

        if dist <= 80: 
            return True
        else: 
            return False 

Instantiation of objects

class Cell_1: # sample of user object 
    def __init__(self):
        self.x = random.randrange(20, width-20) #x position
        self.y = random.randrange(20, height-20) #y position
        self.x_speed = 2
        self.y_speed = 2 

        self.image = char # pre-loaded img 
        self.image = pygame.transform.scale(self.image, (cellsize, cellsize+20))
        self.rect = pygame.Surface.get_rect(self.image, center= (self.x, self.y))

    def wander(self):
        self.x += self.x_speed 
        self.y += self.y_speed

        if self.x <= 30 or self.x >= width - 30:
            self.x_speed *= -1 

        elif self.y <= 30 or self.y >= height - 30:
            self.y_speed *= -1

    def draw(self): 
        surface.blit(self.image, (self.x, self.y))

class Cell_2: 
    def __init__(self):
        self.x = random.randrange(20, width-20) #x position
        self.y = random.randrange(20, height-20) #y position
        self.x_speed = 2
        self.y_speed = 2 

        self.image = char # pre-loaded img 
        self.image = pygame.transform.scale(self.image, (cellsize, cellsize+20))
        self.rect = pygame.Surface.get_rect(self.image, center= (self.x, self.y))

    def wander(self):
        self.x += self.x_speed 
        self.y += self.y_speed

        if self.x <= 30 or self.x >= width - 30:
            self.x_speed *= -1 

        elif self.y <= 30 or self.y >= height - 30:
            self.y_speed *= -1

    def draw(self): 
        surface.blit(self.image, (self.x, self.y))


class Driver: 
    # make driving linear 
    def __init__(self):
        self.x = random.randrange(20, width-20) #x position
        self.y = height - 20 #bottom of screen 
        self.y_speed = 12
        self.x_speed = 12

        self.image = char3
        self.image = pygame.transform.scale(self.image, (cellsize+20, cellsize+20))
        self.rect = pygame.Surface.get_rect(self.image, center = (self.x, self.y))

    def wander(self):
        if self.y <= 20: # height 
            self.y = height 
            self.x = random.randrange(20, width-20)

        else: 
            self.y -= self.y_speed

    def draw(self): 
        surface.blit(self.image, (self.x, self.y))

rects = [] 
cells_1 = []
for i in range(2): 
    cell = Cell_1()
    cells_1.append(cell)
    rects.append(cell) # for collision detection 

cells_2 = []
for i in range(2): 
    cell = Cell_2()
    cells_2.append(cell)
    rects.append(cell)

driver = Driver()

Running the game

while running:
    warm_col = (255, random.randint(0, 255), 0)
    surface.fill(black)

    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            # upon closing the window with mouse 
            running = False 

    driver.wander()
    driver.draw() 
        
    for npc in cells_1: 
        npc.wander()
        npc.draw()

    for npc in cells_2: 
        npc.wander()
        npc.draw()

    for rectangle in rects:
        if (rectangle.rect.colliderect(driver.rect)): 
            rectangle.x_speed *= -1 
            rectangle.y_speed *= -1 
        
    (mX, mY) = pygame.mouse.get_pos()
    char1 = pygame.transform.scale(char1, (cellsize-10, cellsize+20)) 
    surface.blit(char1, (mX, mY))

    for row in range(rows): 
        for col in range(cols): 
            for i in rects: 
                    x = col * cellsize + padding
                    y = row * cellsize + padding 
                    within_dist = euclid_dist(mX, mY, x, y)
                    npc_within = euclid_dist(i.x, i.y, x, y) 

                    if within_dist == True or npc_within == True:
                        pygame.draw.circle(surface, warm_col, (x,y), 3) 


for row in range(rows): 
        for col in range(cols): 
            for i in cells_1: 
                    x = col * cellsize + padding
                    y = row * cellsize + padding 
                    within_dist = euclid_dist(mX, mY, x, y)
                    npc_within = euclid_dist(i.x, i.y, x, y) 

                    if within_dist == True or npc_within == True:
                        pygame.draw.circle(surface, warm_col, (x,y), 3) 

    for row in range(rows): 
        for col in range(cols): 
            for i in cells_2: 
                    x = col * cellsize + padding
                    y = row * cellsize + padding 
                    within_dist = euclid_dist(mX, mY, x, y)
                    npc_within = euclid_dist(i.x, i.y, x, y) 

                    if within_dist == True or npc_within == True:
                        pygame.draw.circle(surface, warm_col, (x,y), 3) 

    for row in range(rows): 
        for col in range(cols):  
                    x = col * cellsize + padding
                    y = row * cellsize + padding 
                    within_dist = euclid_dist(mX, mY, x, y)
                    npc_within = euclid_dist(driver.x, driver.y, x, y) 

                    if within_dist == True or npc_within == True:
                        pygame.draw.circle(surface, warm_col, (x,y), 3)   

              

    pygame.display.update() 
    pygame.time.Clock().tick(fps) 
    pygame.mouse.set_visible(False) 

pygame.quit()

I have tried euclidean distance but it only worked for the edges of the display (gif) I also tried implementing colliderect logic here https://youtu.be/1_H7InPMjaY, but to no avail

May I know if there is a more efficient way to go about this? I was thinking of using sprites instead of blit, but not sure if it would be any different. Also, I'm new to Pygame, so any help or suggestions would be appreciated!


Solution

  • You have to update the position of the rectangle before the collision test:

    driver.rect.x = round(self.y)
    driver.rect.y = round(self.y)
    for rectangle in rects:
        rectangle.rect.x = round(rectangle.x)
        rectangle.rect.y = round(rectangle.y)
        if rectangle.rect.colliderect(driver.rect): 
            rectangle.x_speed *= -1 
            rectangle.y_speed *= -1