53 lines
1.5 KiB
Python
53 lines
1.5 KiB
Python
import pygame
|
|
from pygame.typing import SequenceLike
|
|
|
|
|
|
class Item:
|
|
def __init__(
|
|
self,
|
|
x: float,
|
|
y: float,
|
|
width: float,
|
|
height: float,
|
|
color,
|
|
flashlight,
|
|
game,
|
|
image: pygame.Surface = None,
|
|
):
|
|
self.x: float = x
|
|
self.y: float = y
|
|
self.vector = pygame.Vector2(self.x, self.y)
|
|
self.width: float = width
|
|
self.height: float = height
|
|
self.color: SequenceLike[int] | str | int = color
|
|
self.visible: bool = False
|
|
self.flashlight = flashlight
|
|
self.game = game
|
|
self.image = image
|
|
self.mask = pygame.mask.Mask(self.rect.size, fill=True)
|
|
|
|
@property
|
|
def rect(self) -> pygame.Rect:
|
|
if not self.image:
|
|
return pygame.Rect(self.x, self.y, self.width, self.height)
|
|
|
|
else:
|
|
return pygame.Rect(
|
|
self.x, self.y, self.image.get_width(), self.image.get_height()
|
|
)
|
|
|
|
def update(self):
|
|
self.visible = False
|
|
self.visible = (
|
|
self.flashlight.light_mask.overlap(self.mask, self.rect.topleft) is not None
|
|
)
|
|
|
|
def render(self, surface: pygame.Surface):
|
|
if self.visible and self.flashlight.light_state:
|
|
if not self.image:
|
|
pygame.draw.rect(surface, self.color, self.rect)
|
|
else:
|
|
surface.blit(
|
|
self.image,
|
|
(self.x, self.y, self.image.get_width(), self.image.get_height()),
|
|
)
|