Refactor Flashlight-Klasse: Lichtpunkte erstellen und Rendering-Logik aktualisieren
This commit is contained in:
parent
08004b8d3b
commit
45d9d71310
1 changed files with 33 additions and 32 deletions
65
game.py
65
game.py
|
|
@ -192,44 +192,47 @@ class Player:
|
|||
|
||||
class Flashlight:
|
||||
def __init__(self, game, sprite):
|
||||
self.light_points: list[pygame.Vector2]
|
||||
self.light_surface: pygame.Surface = pygame.Surface(
|
||||
(SCREEN_WIDTH, SCREEN_HEIGHT), pygame.SRCALPHA
|
||||
)
|
||||
self.light_state: bool = True
|
||||
self.game = game
|
||||
self.render_item = False
|
||||
self.sprite = sprite
|
||||
|
||||
def update(self):
|
||||
self.light_points: list[Vector2] = self.create_light_points()
|
||||
|
||||
def render(self, surface) -> bool:
|
||||
if self.light_state:
|
||||
self.light_surface.fill((0, 0, 0, 0))
|
||||
pygame.draw.polygon(
|
||||
self.light_surface, (243, 255, 74, 255), self.create_light_points()
|
||||
)
|
||||
surface.blit(self.light_surface, (0, 0))
|
||||
light_mask = pygame.mask.from_surface(self.light_surface)
|
||||
|
||||
if (
|
||||
light_mask.overlap(
|
||||
self.game.test_item_mask, self.game.test_item.rect.topleft
|
||||
)
|
||||
is not None
|
||||
):
|
||||
self.render_item = True
|
||||
else:
|
||||
self.render_item = False
|
||||
|
||||
# Item zeichnen, wenn Masken sich überlappen
|
||||
self.light_points: list[pygame.Vector2] = []
|
||||
self.light_surface: pygame.Surface = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.SRCALPHA)
|
||||
self.light_state: bool = True
|
||||
self.render_item = False
|
||||
|
||||
def create_light_points(self) -> list[pygame.Vector2]:
|
||||
points: list[pygame.Vector2] = []
|
||||
origin: Vector2 = self.sprite.pos
|
||||
base_ray = pygame.Vector2(LIGHT_RADIUS, 0)
|
||||
|
||||
for angle in range(360):
|
||||
ray_end = origin + base_ray.rotate(angle)
|
||||
for wall in self.game.walls:
|
||||
intersection = wall.clipline(origin, ray_end)
|
||||
if intersection:
|
||||
ray_end = pygame.Vector2(intersection[0])
|
||||
points.append(ray_end)
|
||||
|
||||
return points
|
||||
|
||||
def update(self):
|
||||
self.render_item = False
|
||||
if not self.light_state:
|
||||
return
|
||||
|
||||
self.light_points = self.create_light_points()
|
||||
self.light_surface.fill((0, 0, 0, 0))
|
||||
pygame.draw.polygon(self.light_surface, (243, 255, 74, 255), self.light_points)
|
||||
light_mask = pygame.mask.from_surface(self.light_surface)
|
||||
item = self.game.test_item
|
||||
self.render_item = light_mask.overlap(self.game.test_item_mask, item.rect.topleft) is not None
|
||||
|
||||
def render(self, surface):
|
||||
if self.light_state:
|
||||
surface.blit(self.light_surface, (0, 0))
|
||||
points: list[pygame.Vector2] = []
|
||||
origin: Vector2 = self.sprite.pos
|
||||
base_ray = pygame.Vector2(LIGHT_RADIUS, 0)
|
||||
|
||||
# Lichtstrahlen berechnen
|
||||
for angle in range(360):
|
||||
ray_end = origin + base_ray.rotate(angle)
|
||||
|
|
@ -252,7 +255,5 @@ game.run()
|
|||
in Klasse Flashlight:
|
||||
|
||||
|
||||
create_light_points() in Flashlight machen
|
||||
|
||||
render_item in Item Klasse machen
|
||||
"""
|
||||
|
|
|
|||
Loading…
Reference in a new issue