My problem: I have created a level map and tried to implement some basic collision detection. I have blocks which I named dirt_bg And I got the blocks rect with the .get_rect function and set its name to dirt_rect
But when I do self.rect.colliderect(tile.dirt_rect) - inside the player class- it only detects collision on the top left corner
Useful info: I have a Tile class where I create the level map I have a player class where I work with characters
Code: class Tiles(pygame.sprite.Sprite):
def __init__(self):
self.offsetX = 0
self.offsetY = 0
pygame.sprite.Sprite.__init__(self)
global dirt_bg
dirt_bg = pygame.image.load("dirt_block.png").convert_alpha()
dirt_bg = pygame.transform.scale(dirt_bg, (102, 102))
dirt_bg_rect = dirt_bg.get_rect()
global coin
coin = pygame.image.load ("1.jpg").convert_alpha()
coin = pygame.transform.scale(coin, (52, 52))
# coin = pygame.transform.scale(coin,(32,32))
def tile_maker(self):
rects_tile = []
y_position = 0
for row in level_map:
x_position = 0
for tile in row:
if tile == 'X':
screen.blit(dirt_bg, (x_position * 102 + self.offsetX, y_position * 102 + self.offsetY))
if tile != 'X':
rects_tile.append(pygame.Rect(x_position * 102, y_position * 102, 102, 102))
if tile == 'C':
screen.blit(coin, (x_position * 102 + self.offsetX, y_position * 102 + self.offsetY))
if tile != 'C':
rects_tile.append(pygame.Rect(x_position * 102, y_position * 102, 102, 102))
x_position += 1
y_position += 1
Would appreciate any help