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))