Collision detection against player and blocks in the map

301 views Asked by At

I made a game map like this

class Game_map:
    def __init__(self):
        self.land = pygame.image.load(r"C:\Users\name\OneDrive\Documents\A level python codes\final game\land.png").convert()
        self.height = 200
        self.width = 200
        self.land = pygame.transform.scale(self.land, (420,  250))
        self.land.set_colorkey((0, 0, 0))

        self.map_width = 6000
        self.map_height = 2000

        # 0 = emepty
        # 1 = land

        self.layout = [[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
                              [ 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0 ],
                              [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
                              [ 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1 ],
                              [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
                              [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
                              [ 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0 ],
                              [ 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0 ],
                              [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
                              [0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0 ]]



    def draw_map(self):
        y = 0
        for layer in self.layout:
            x = 0
            for land in layer:
                if land == 1:
                    D.blit(self.land, (x * 400 - scroll[0],  y * 250 - scroll[1]))

                if land == 0:
                    pass

                x += 1
            y += 1




game_map = Game_map()

I have a player class as well with self.x, self.y, self.width and self.height . How would i implement collision detection with this? My initial idea was that i would use self.x and self.y in the draw_map() function so that i could later check for collisions like this

 def draw_map(self):
        for layer in self.layout:
            for land in layer:
                if land == 1:
                    D.blit(self.land, (self.x * 400 - scroll[0], self. y * 250 - scroll[1]))

                if land == 0:
                    pass

                x += 1
            y += 1

But when i do this, nothing gets drawn on the screen. I also tried adding x and y values in the original draw_map function mentioned above to a list and detect collision with that but it got really messy and didnt work. Is there any other way to do collision detection ? Thanks

1

There are 1 answers

0
Rabbid76 On BEST ANSWER

The straight forward method is to find the collisions of the player rectangle and the obstacle rectangles.

Create a list of the field indices of all obstacles:

obstacles = [(i, j) for i in range(len(game_map.layout))
                    for j in range(len(game_map.layout[i]))
                    if game_map.layout[i][j] == 1]

Setup a pygame.Rect for the player:

player_rect = pygame.Rect(player.x, player.y, player.width, player.height) 

Use colliderect() to find the collisions of the player and the obstacles:

hit = [obj for obj in obstacles
           if player_rect.colliderect(
               pygame.Rect(obj[0]*400-scroll[0], obj[1]*250-scroll[1], 400, 250))]

if any(hit):
    print("hit")

The more tricky by far more efficient method is to compute the field indices for the 4 corners of the player rectangle and to evaluate if one of the corners is in a field with an obstacle.

Setup the player rectangle:

player_rect = pygame.Rect(player.x, player.y, player.width, player.height) 

Compute the coordinates for corners of the rectangle:

player_corners = [player_rect.topleft, player_rect.topright, 
                  player_rect.bottomleft, player_rect.bottomright]

Compute the field indices, where the corner points are on:

corner_indices = [((p[0]+scroll[0])//400, (p[1]+scroll[1])//250) for p in player_corners]

Evaluate if any of the field contains an obstacle:

hit = [ind for ind in corner_indices if game_map.layout[ind[0]][ind[1]]]

if (any(hit)):
    print("hit")