class Wall(pygame.sprite.Sprite):
def __init__(self, x, y, width, height):
super().__init__()
self.x = x
self.y = y
self.width = width
self.height = height
self.image = pygame.Surface((width, height))
self.image.fill(BLACK) # Black color represents the wall
self.rect = self.image.get_rect()
self.mask = pygame.mask.from_surface(self.image)
self.rect.x = self.x
self.rect.y = self.y
def draw(self):
screen.blit(self.image, (self.x, self.y))
def check_collision(self):
offset_x = self.x - player.x
offset_y = self.y - player.y
return player.mask.overlap(self.mask, (offset_x, offset_y))
This is my wall class
and
for wall in walls:
wall.draw()
if wall.check_collision():
if player.x > wall.x: # 오른쪽 벽에 닿았을 때
player.x += wall.width
else: # 왼쪽 벽에 닿았을 때
player.x = wall.x - player.rect.width
if player.y > wall.y: # 아래쪽 벽에 닿았을 때
player.y = wall.y + wall.height
else: # 위쪽 벽에 닿았을 때
player.y = wall.y - player.rect.height
This is my game loop about walls. But when I collide with walls with my sprite The sprite when opposite of the wall.
I hope the walls would work very normal. Please help.