0

I have created a Map Class within pygame and currently working on collision with blocks.(self.grass_block). Is there a specific method I need to use in order to cause collision when a player class hits a grass block?

def __init__(self, game):
    """This class handles the map."""
    super().__init__()
    self.screen = game.screen
    self.settings = game.settings

    self.x = 30
    self.y = 30
    self.dirt_block = pygame.image.load("Graphics\\dirtbrick.png").convert_alpha()
    self.grass_block = pygame.image.load("Graphics\\topgrass.png").convert_alpha()
    self.map = ["33333333333333333333333333333333333333333333333333333333333333333333333333333333",
                "33333333333333333333333333333333333333333333333333333333333333333333333333333333",
                "33333333333333333333333333333333333333333333333333333333333333333333333333333333",
                "33333333333333333333333111111113333333333333111111111333333333333333333333333333",
                "33333333333333333333333000000003333333333333000000000333333333333333333333333333",
                "33333333333333333333333000000003333333333333000000000333333333333333333333333333",
                "11111111333333333333333000000003333331111111000000000333333333331111111113333333",
                "00000000333333311111111000000003333330000000000000000333333333330000000003333333",
                "00000000333333300000000000000003333330000000000000000333333333330000000003333333",
                "00000000333333300000000000000003333330000000000000000111111111330000000003333333",
                "00000000333333300000000000000001111110000000000000000000000000330000000001111111",
                "00000000331111100000000000000000000000000000000000000000000000330000000000000000",
                "00000000330000000000000000000000000000000000000000000000000000330000000000000000",
                "00000000330000000000000000000000000000000000000000000000000000330000000000000000",
                "00000000330000000000000000000000000000000000000000000000000000330000000000000000",
                "00000000330000000000000000000000000000000000000000000000000000330000000000000000",
                "00000000330000000000000000000000000000000000000000000000000000330000000000000000",
                "00000000330000000000000000000000000000000000000000000000000000330000000000000000",
                "00000000330000000000000000000000000000000000000000000000000000330000000000000000",
                "00000000330000000000000000000000000000000000000000000000000000330000000000000000"]

def draw(self):
    """This method draws the map."""
    for y, line in enumerate(self.map):
        for x, c in enumerate(line):
            if c == "0":
                self.screen.blit(self.dirt_block, (x * 30, y * 30))
            if c == "1":
                self.screen.blit(self.grass_block, (x * 30, y * 30))
  • 1
    What have you tried so far? I suggest reading [How do I detect collision in pygame?](https://stackoverflow.com/questions/29640685/how-do-i-detect-collision-in-pygame/65064907#65064907). – Rabbid76 Oct 30 '21 at 08:22

1 Answers1

0

For two rects rect1 and rect2, you can call rect1.colliderect(rect2), which will return either true or false depending on whether they are touching. Assuming the player class has a rect already defined, you just need a rect for the grass block so the two rects can be compared. You could generate a list of grass rects at the end of __init__:

def __init__(self, game):
    # ...
    self.grass_block_rects = []
    for y, line in enumerate(self.map):
        for x, c in enumerate(line):
            if c == "0":
                center = ((x*30)+15, (y*30)+15)
                self.grass_block_rects.append(self.grass_block.get_rect(center=center))

Then you can check if the player rect is colliding with any of these grass_block_rects:

for grass_block_rect in self.grass_block_rects:
    if player.rect.colliderect(grass_block_rect):
        # Do stuff based on collision here...

Note: you also need to make sure the grass rects and surfaces are the correct size (which seems to be 30 by 30) so on the lines where you load the images, you should scale them:

pygame.transform.scale(pygame.image.load("Graphics\\dirtbrick.png").convert_alpha(), (30,30))
pygame.transform.scale(pygame.image.load("Graphics\\topgrass.png").convert_alpha(), (30,30))
Daniel
  • 275
  • 1
  • 9