py_stepik/pygame-wasm/phantomcastle/game/wall.py

56 lines
1.7 KiB
Python
Raw Normal View History

import pygame
from coords import Coords
from common import DrawableGameObject
class WallBlock(DrawableGameObject):
"""объект элемента стены"""
def __init__(
self,
coords: Coords,
parent: DrawableGameObject,
assets: dict | None = None,
):
super().__init__(coords, parent, assets)
2024-04-07 17:37:02 +00:00
self._surface = pygame.image.load(self.assets["brick.png"]).convert_alpha()
self.rect = self.surface.get_rect()
self.rect.topleft = coords
# уменьшаем размер монетки
sf = Coords(1, 1)
self._surface, self.rect = self.scene.scale_box(self.surface, self.rect, sf)
self._mask = pygame.mask.Mask(self.rect.size, fill=True)
def draw(self):
self.parent.surface.blit(self.surface, self.rect)
class Walls(DrawableGameObject):
"""объект стен"""
def __init__(
self,
parent: DrawableGameObject,
maze: list[list[int]],
box_sz: Coords,
assets: dict | None = None,
):
super().__init__(Coords.zero(), parent, assets)
self.box_sz = box_sz
self.blocks = [
WallBlock(Coords(j, i).transform(box_sz), self, self.assets)
for i, row in enumerate(maze)
for j, item in enumerate(row)
if item > 0
]
def draw(self):
for block in self.blocks:
block.draw()
def check_collision(self, rect: pygame.Rect, mask: pygame.Mask) -> bool:
for block in self.blocks:
if block.overlap(rect, mask):
return True
return False