1

im trying to detect a collision between pacman and the boxes, but its not detecting any collision, any help or advice? at the moment ive tried creating a list of instances but that hasnt worked, i dont know what else to do. also its telling me to add more detail but i dont really know what i can add to be honest, sorry

import pygame
import os
import sys

#intialise the game 
pygame.init()
screen = pygame.display.set_mode((448, 576))
done = False

y = 320
x = 216

#sets up clock and loads pacman image
clock = pygame.time.Clock()
PACMANSPRITE = pygame.image.load("pacman.png").convert_alpha()

#gets pacman intro music, sets music to lower volume then plays it
pygame.mixer.music.load('pacman_beginning.WAV')
pygame.mixer.music.set_volume(0.01)
pygame.mixer.music.play(0)


#box class, used for boxes to border pacmans map
class boxcollisions(pygame.sprite.Sprite):
    def __init__(self, x, y):
        self.y = y
        self.x = x
        self.rect = pygame.Rect(self.x, self.y, 15, 15)
        self.color = (0, 128, 255)

    def draw(self, screen):
        pygame.draw.rect(screen, self.color, self.rect)



#pacmans class
class pacman(pygame.sprite.Sprite):
    def __init__(self, image, x, y):
        self.image = image
        self.y=y
        self.x=x
        self.rect = self.image.get_rect()
        self.rect.left = self.x
        self.rect.top = self.y
        self.rect.width=16
        self.rect.height=16


    # move pacman 
    def movement(self):
        pressed= pygame.key.get_pressed()
        if pressed[pygame.K_UP]:
            self.y -= 2
        if pressed[pygame.K_DOWN]:
            self.y += 2
        if pressed[pygame.K_LEFT]:
            self.x -= 2
        if pressed[pygame.K_RIGHT]:
            self.x += 2


    def draw(self, surface):
        """ Draw on surface """
        # blit yourself at your current position
        surface.blit(self.image, (self.x, self.y))

#instances the pacman class
sprite = pacman(PACMANSPRITE, x ,y)


#main game loop
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
                    done = True
                    pygame.quit()
                    sys.exit()


    screen.fill((100,0,0))

    #co-ordinates for boxes to set up map boundaries
    boundaries=[

        [],
        [],
        [],
        [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28],
        [1,14,15,28], #5
        [1,3,4,5,6,8,9,10,11,12,14,15,17,18,19,20,21,23,24,25,26,28],
        [1,3,4,5,6,8,9,10,11,12,14,15,17,18,19,20,21,23,24,25,26,28],
        [1,3,4,5,6,8,9,10,11,12,14,15,17,18,19,20,21,23,24,25,26,28],
        [1,28],
        [1,3,4,5,6,8,9,11,12,13,14,15,16,17,18,20,21,23,24,25,26,28], #10
        [1,3,4,5,6,8,9,11,12,13,14,15,16,17,18,20,21,23,24,25,26,28],
        [1,8,9,14,15,20,21,28],
        [1,2,3,4,5,6,8,9,10,11,12,14,15,17,18,19,20,21,23,24,25,26,27,28],
        [1,2,3,4,5,6,8,9,10,11,12,14,15,17,18,19,20,21,23,24,25,26,27,28],
        [6,8,9,20,21,23], #15
        [6,8,9,11,12,13,14,15,16,17,18,20,21,23],
        [1,2,3,4,5,6,8,9,11,12,13,14,15,16,17,18,20,21,23,24,25,26,27,28],
        [1,11,12,13,14,15,16,17,18,28],
        [1,2,3,4,5,6,8,9,11,12,13,14,15,16,17,18,20,21,23,24,25,26,27,28],
        [6,8,9,11,12,13,14,15,16,17,18,20,21,23,24,25,26,27,28], #20
        [6,8,9,20,21,23],
        [6,8,9,11,12,13,14,15,16,17,18,20,21,23],
        [1,2,3,4,5,6,8,9,11,12,13,14,15,16,17,18,20,21,23,24,25,26,27,28],
        [1,14,15,28],
        [1,3,4,5,6,8,9,10,11,12,14,15,17,18,19,20,21,23,24,25,26,28], #25
        [1,3,4,5,6,8,9,10,11,12,14,15,17,18,19,20,21,23,24,25,26,28],
        [1,5,6,23,24,28],
        [1,2,3,5,6,8,9,11,12,13,14,15,16,17,18,20,21,23,24,26,27,28],
        [1,2,3,5,6,8,9,11,12,13,14,15,16,17,18,20,21,23,24,26,27,28],
        [1,8,9,14,15,20,21,28], # 30
        [1,3,4,5,6,7,8,9,10,11,12,14,15,17,18,19,20,21,22,23,24,25,26,28],
        [1,3,4,5,6,7,8,9,10,11,12,14,15,17,18,19,20,21,22,23,24,25,26,28],
        [1,28],
        [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28],            

      ]

    #builds the boxes
    bx=0
    by=-16
    for row in boundaries:
        #y co ordinate
        by=by+16    
        for n in row:
            #x co ordinate
            n=n-1
            bx=n*16
            box = boxcollisions(bx, by)
            box.draw(screen)


    #moves pacman
    sprite.movement()
    sprite.draw(screen)

    #tests for collision
    print(pygame.sprite.collide_rect(sprite, box))



    pygame.display.flip()
    clock.tick(60)
Brown Bear
  • 19,655
  • 10
  • 58
  • 76
cdd
  • 109
  • 11

2 Answers2

2

1 - You need update the top and left position at moviment method. look:

# move pacman 
def movement(self):
    pressed= pygame.key.get_pressed()
    if pressed[pygame.K_UP]:
        self.y -= 2
    if pressed[pygame.K_DOWN]:
        self.y += 2
    if pressed[pygame.K_LEFT]:
        self.x -= 2
    if pressed[pygame.K_RIGHT]:
        self.x += 2
    self.rect.left = self.x
    self.rect.top = self.y

2 - You have to verificate the collision into loop, for verification with all boxes

for row in boundaries:
    #y co ordinate
    by=by+16    
    for n in row:
        #x co ordinate
        n=n-1
        bx=n*16
        box = boxcollisions(bx, by)
        box_list.append(box)
        box.draw(screen)
        if pygame.sprite.collide_rect(sprite, box):
            print("collided")
0

Use rect.collidelist to test if pacman is colliding with a wall sprites in your wall sprites list. It will return -1 as long as no collision is detected

  • how do i implement it into my code, its having this error: Axel Villalonga TypeError: descriptor 'collidelist' requires a 'pygame.Rect' object but received a 'boxcollisions' – cdd Aug 19 '17 at 15:20
  • You should create a list where you stock all of your boxes who are created with your boundaries, then use the pacman.rect.collidelist(my_boxes_list) instead of just drawing them then use a methode who draw each one of them in the background, so you can test collision between pacman and your boxes – Axel Villalonga Aug 19 '17 at 15:28
  • descriptor 'collidelist' requires a 'pygame.Rect' object but received a 'list'.....i get thid error – cdd Aug 19 '17 at 15:32
  • I forgot to say. You need to create a class for your boxes who inherit from the pygame.sprite.Sprite class too and then give them a rect too. Then create all your boxes with this class and store them in a list. Edit: You should check the pygame tutorial on the pygame site – Axel Villalonga Aug 19 '17 at 15:38
  • i have, look at the box collision class, its inherited pygame.sprite.Sprite, i have create a box lsit and its still not working, sorry for the trouble – cdd Aug 19 '17 at 15:42
  • Ok, i get it. In your main loop, you are creating a new box object with your for loop every time the main loop is running. You shouldn't do that, you should create your box object juste one time before the main loop in a list of boxes, then in your main loop draw them and check collisions – Axel Villalonga Aug 19 '17 at 15:46