pythonpygamecollision

Adding object collision in pygame


I am trying to implement Collision in my game but the way I am doing it right now the player gets stuck at the hitbox and I can't figure out how to do it. The object I am currently trying to collide with is wall1. For sake of clarity, I have only added the Wall object and the main function

                class wall(object):
                    def __init__(self, x, y, width, height):
                        self.x = x
                        self.y = y
                        self.width = width
                        self.height = height 
                        #self.rect = pygame.Rect(x, y, width, height)
                        global ColliderRect 
                        ColliderRect = pygame.Rect(x, y, width, height)
                    def playerCollide(self):
                        if (man.x + man.vel > self.x - 45 and man.y + man.vel > self.y -50 and man.y + man.vel < self.y+self.height and man.x + man.vel < self.x + self.width):
                            return True
                        else:
                            return False

                #mainloop
                collider1 = wall(500, 400, 200, 200)

                man = player(200, 410, 64,64)
                run = True
                while run:
                    clock.tick(60)
                    wall1 = collider1.playerCollide()

                    for event in pygame.event.get():
                        if event.type == pygame.QUIT:
                            run = False

                    keys = pygame.key.get_pressed()

                    if keys[pygame.K_a] and man.x > man.vel and  wall1== False:
                        man.x -= man.vel
                        man.left = True
                        man.right = False
                        print(wall1)
                    elif keys[pygame.K_d] and man.x < scrWidth - man.width - man.vel and wall1 == False:
                        man.x += man.vel
                        man.right = True
                        man.left = False
                        print(wall1)
                    elif keys[pygame.K_w] and man.y > man.vel and wall1 == False:
                        man.y -= man.vel
                        man.left = False
                        man.right = False
                        print(wall1)
                    elif keys[pygame.K_s] and man.y < scrHeight - man.height - man.vel and wall1 == False:
                        man.y += man.vel
                        man.left = False
                        man.right = False
                        print(wall1)
                    else:
                        man.right = False
                        man.left = False
                        man.walkCount = 0
                        if (wall1 == True):
                            wall1 = False
                    man.hitbox(15, 0, 31, 17)
                    #man.drawhitbox()
                    redrawGameWindow()
                pygame.quit()

Solution

  • Use pygame.Rect objects and colliderect() for the collision test.

    Pass an pygame.Rect object to the method playerCollide and test if it collides with the wall:

    class wall(object):
        def __init__(self, x, y, width, height):
            self.x = x
            self.y = y
            self.width = width
            self.height = height 
            self.rect = pygame.Rect(x, y, width, height)
            self.rect = pygame.Rect(x, y, width, height)
        
        def playerCollide(self, test_rect):
            return self.rect.colliderect(test_rect)
    

    Compute a new position for the player (x, y). Create a pygame.Rect with the size of the player at the new position (player_rect). Change the actual position of the player, if player_rect doesn't collide with the wall:

    collider1 = wall(500, 400, 200, 200)
    man = player(200, 410, 64,64)
    run = True
    while run:
        clock.tick(60)
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
    
        keys = pygame.key.get_pressed()
        x, y = man.x, man.y
    
        if keys[pygame.K_a] and man.x > man.vel:
            x -= man.vel
            man.left = True
            man.right = False
        elif keys[pygame.K_d] and man.x < scrWidth - man.width - man.vel:
            x += man.vel
            man.right = True
            man.left = False
        elif keys[pygame.K_w] and man.y > man.vel:
            y -= man.vel
            man.left = False
            man.right = False
        elif keys[pygame.K_s] and man.y < scrHeight - man.height - man.vel:
            y+= man.vel
            man.left = False
            man.right = False
        else:
            man.right = False
            man.left = False
            man.walkCount = 0
            
        player_rect = pygame.Rect(x, y, 64, 64)
        if not collider1.playerCollide(player_rect):
            man.x, man.y = x, y