1
votes

I am relatively new to Pygame and I'm attempting to create a Pacman game for a project. I'm having trouble with collision detection between Pacman and this block (imported image). Pacman goes right through the block which I don't want to happen; I want it so that Pacman can't pass through it. I've searched many websites and many forums and tried many different methods that I've seen other people have used but I can't seem to get my head around collision detection. Below is my current code.

def Pacman():
    pygame.init()
    # Creating screen
    global screen
    screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
    screen = pygame.display.set_mode((0,0), pygame.RESIZABLE)
    # Setting window caption
    pygame.display.set_caption('Pacman')
    # Loading two images
    pacman_image = pygame.image.load("pacmanphoto.png").convert_alpha()
    block_image = pygame.image.load("blockphoto.png").convert_alpha()
    rect1 = pacman_image.get_rect()
    rect2 = block_image.get_rect()
    rect1.x = 100
    rect1.y = 200
    rect2.x = 300
    rect2.y = 400

    clock = pygame.time.Clock()
    x = 10
    y = 10
    pygame.key.set_repeat(10, 10)
    # Movement
    while True:
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    y -= 10
                if event.key == pygame.K_DOWN:
                    y += 10
                if event.key == pygame.K_RIGHT:
                    x += 10
                if event.key == pygame.K_LEFT:
                    x -= 10
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        if rect1.colliderect(rect2):
            y -=10
        ##### where collision detection should be ####


        # Displaying images on screen
        screen.blit(pacman_image, (x,y))
        screen.blit(block_image, (50,50))
        clock.tick(12)
        pygame.display.update()
        screen.fill(BLACK)

If anyone can help me in any way please let me know :) Thanks

1

1 Answers

2
votes

First of all, your code is fairly unorganized. I recommend using classes so that the Pacman function looks a lot clearer, readable and everything isn't squeezed inside only one function. If you don't want the Pacman to go through the blocks, you should know what you're up to. You don't want the Pacman to pass through the blocks, which basically means that if the Pacman's y(y-axis value) is between the block's y-axis and the same for the x-axis, then collision occurred, from here on you can just set the pacman_x and pacman's y to the block's x and y.

# Inside The Pacman Function
pac_x = 10
pac_y = 10
block_x = 50
block_y = 50
# getting the block's height and width
block_width, block_height = block_image.get_size()
##### where collision detection should be ####
if block_x + block_width > pac_x > block_x:
    if block_y + block_height > pac_y > block_y:
        print('Collision occurred.')
        pac_x = block_x
        pac_y = block_y
        # Here you can add more functionality to your game
# Displaying images on screen
screen.blit(pacman_image, (pac_x,pac_y))
screen.blit(block_image, (block_x, block_y))