PyGame Collision?

20,316

Solution 1

If you use the pygame Rect class to represent the boundaries of your object, you can detect whether two are colliding by using the Rect.colliderect function. For example:

import pygame

a = pygame.Rect((1, 1), (2, 2))
b = pygame.Rect((0, 0), (2, 2))
c = pygame.Rect((0, 0), (1, 1))
a.colliderect(b)
# 1
a.colliderect(c)
# 0
b.colliderect(c)
# 1

a is colliding with b, and b is colliding with c, but a is not colliding with c. Note that rects that share a boundary are not colliding.

Pygame also supports letting you use a Rect as the position for an image when you want to 'blit' it onto the screen.

Solution 2

import pygame
pygame.init()
class Sprite(pygame.sprite.Sprite):
    def __init__(self, image, location):
        self.image = pygame.image.load(urimage)
...

then make collision groups and use pygame.sprite.spritecollide()

Solution 3

Create a function that checks for the x, y, w, h perimeters of both objects and create an if statement as such that will check if the two objects are colliding:

def col_check(x,y,w,h,x2,y2,w2,h2):
if (x < (x2 + w2) and (x + w) > x2 and y < (y2 + h2) and (h + y) > y2):                     
    # Do something here                                                

Now you can just call the function with the objects perimeters as the arguments.

Solution 4

Lets say you have a player class that looks something like this, I like using pygame.sprite.collide_rect() in this case we'll also have a wall class.

class Player(pygame.sprite.Sprite):
    def __init__(self,x,y):

        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((32,32))
        self.rect = Rect(x, y, 32, 32)

    def move(self, px, py):

        if px != 0:
            self.move_on_axis(px, 0)
        if py != 0:
            self.move_on_axis(0, py)

    def move_on_axis(self, px, py):
        self.rect.x += px
        self.rect.y += py

Here is where we cheek for Collisions with anything in walls group, which we add when ever we make a new wall.

        for wall in walls:
            if pygame.sprite.collide_rect(self, wall):   
                if px > 0:
                     self.rect.right = wall.rect.left
                if px < 0:
                    self.rect.left = wall.rect.right
                if py > 0:
                    self.rect.bottom = wall.rect.top                        
                if py < 0:
                    self.rect.top = wall.rect.bottom

class Wall(pygame.sprite.Sprite):
    def __init__(self, wx):
        super().__init__()
        all_Sprite_List.add(self)
        walls.add(self)
        self.image = pygame.Surface((32,32))
        self.rect = Rect(wx[0], wx[1], 32, 32)
Share:
20,316
pixelgeer
Author by

pixelgeer

Updated on July 22, 2022

Comments

  • pixelgeer
    pixelgeer almost 2 years

    How do I find collisions between characters and images within PyGame? I have drawn a player from an image, and have drawn the walls from tiles, so how would I detect these collisions?