create_light_points() in Flashlight Klasse gemacht und die Klasse umbenannt
This commit is contained in:
parent
30c3b2fe8d
commit
08004b8d3b
2 changed files with 35 additions and 32 deletions
BIN
ZIPTE.png
Normal file
BIN
ZIPTE.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 398 KiB |
65
game.py
65
game.py
|
|
@ -52,7 +52,7 @@ class Game:
|
||||||
self.player = Player(self.player_pos, width=PLAYER_SIZE, height=PLAYER_SIZE)
|
self.player = Player(self.player_pos, width=PLAYER_SIZE, height=PLAYER_SIZE)
|
||||||
|
|
||||||
# Flashlight
|
# Flashlight
|
||||||
self.flashlight = Taschenlampe(self)
|
self.flashlight_player = Flashlight(self, self.player)
|
||||||
|
|
||||||
# Items
|
# Items
|
||||||
self.test_item = Item(140, 300, 20, 20, "blue")
|
self.test_item = Item(140, 300, 20, 20, "blue")
|
||||||
|
|
@ -78,16 +78,16 @@ class Game:
|
||||||
if event.key == pygame.K_LSHIFT:
|
if event.key == pygame.K_LSHIFT:
|
||||||
self.player.speed = 300
|
self.player.speed = 300
|
||||||
if event.key == pygame.K_q:
|
if event.key == pygame.K_q:
|
||||||
self.flashlight.light_state = not self.flashlight.light_state
|
self.flashlight_player.light_state = not self.flashlight_player.light_state
|
||||||
if self.flashlight.render_item:
|
if self.flashlight_player.render_item:
|
||||||
self.flashlight.render_item = False
|
self.flashlight_player.render_item = False
|
||||||
|
|
||||||
if event.type == pygame.KEYUP and event.key == pygame.K_LSHIFT:
|
if event.type == pygame.KEYUP and event.key == pygame.K_LSHIFT:
|
||||||
self.player.speed = 150
|
self.player.speed = 150
|
||||||
|
|
||||||
# Spielzustände aktualisieren
|
# Spielzustände aktualisieren
|
||||||
self.player.update(walls=self.walls, dt=self.dt)
|
self.player.update(walls=self.walls, dt=self.dt)
|
||||||
self.flashlight.update()
|
self.flashlight_player.update()
|
||||||
|
|
||||||
def render(self) -> None:
|
def render(self) -> None:
|
||||||
# Hintergrund
|
# Hintergrund
|
||||||
|
|
@ -110,8 +110,8 @@ class Game:
|
||||||
# -> [][][][][][]
|
# -> [][][][][][]
|
||||||
|
|
||||||
# Licht
|
# Licht
|
||||||
self.flashlight.render(self.screen)
|
self.flashlight_player.render(self.screen)
|
||||||
if self.flashlight.render_item:
|
if self.flashlight_player.render_item:
|
||||||
self.test_item.render(self.screen)
|
self.test_item.render(self.screen)
|
||||||
|
|
||||||
# Wände
|
# Wände
|
||||||
|
|
@ -124,24 +124,7 @@ class Game:
|
||||||
# Surfaces aktualisieren
|
# Surfaces aktualisieren
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
|
|
||||||
def create_light_points(self) -> list[pygame.Vector2]:
|
|
||||||
points: list[pygame.Vector2] = []
|
|
||||||
origin: Vector2 = self.player.pos
|
|
||||||
base_ray = pygame.Vector2(LIGHT_RADIUS, 0)
|
|
||||||
|
|
||||||
# Lichtstrahlen berechnen
|
|
||||||
for angle in range(360):
|
|
||||||
ray_end = origin + base_ray.rotate(angle)
|
|
||||||
|
|
||||||
# Überschneidung mit den Wänden prüfen
|
|
||||||
for wall in self.walls:
|
|
||||||
# Überschneidung
|
|
||||||
intersection = wall.clipline(origin, ray_end)
|
|
||||||
if intersection:
|
|
||||||
ray_end = pygame.Vector2(intersection[0])
|
|
||||||
points.append(ray_end)
|
|
||||||
|
|
||||||
return points
|
|
||||||
|
|
||||||
def run(self) -> None:
|
def run(self) -> None:
|
||||||
while self.running:
|
while self.running:
|
||||||
|
|
@ -207,8 +190,8 @@ class Player:
|
||||||
pygame.draw.rect(surface, (255, 0, 0), self.rect)
|
pygame.draw.rect(surface, (255, 0, 0), self.rect)
|
||||||
|
|
||||||
|
|
||||||
class Taschenlampe:
|
class Flashlight:
|
||||||
def __init__(self, game):
|
def __init__(self, game, sprite):
|
||||||
self.light_points: list[pygame.Vector2]
|
self.light_points: list[pygame.Vector2]
|
||||||
self.light_surface: pygame.Surface = pygame.Surface(
|
self.light_surface: pygame.Surface = pygame.Surface(
|
||||||
(SCREEN_WIDTH, SCREEN_HEIGHT), pygame.SRCALPHA
|
(SCREEN_WIDTH, SCREEN_HEIGHT), pygame.SRCALPHA
|
||||||
|
|
@ -216,15 +199,16 @@ class Taschenlampe:
|
||||||
self.light_state: bool = True
|
self.light_state: bool = True
|
||||||
self.game = game
|
self.game = game
|
||||||
self.render_item = False
|
self.render_item = False
|
||||||
|
self.sprite = sprite
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
self.light_points: list[Vector2] = self.game.create_light_points()
|
self.light_points: list[Vector2] = self.create_light_points()
|
||||||
|
|
||||||
def render(self, surface) -> bool:
|
def render(self, surface) -> bool:
|
||||||
if self.light_state:
|
if self.light_state:
|
||||||
self.light_surface.fill((0, 0, 0, 0))
|
self.light_surface.fill((0, 0, 0, 0))
|
||||||
pygame.draw.polygon(
|
pygame.draw.polygon(
|
||||||
self.light_surface, (243, 255, 74, 255), self.game.create_light_points()
|
self.light_surface, (243, 255, 74, 255), self.create_light_points()
|
||||||
)
|
)
|
||||||
surface.blit(self.light_surface, (0, 0))
|
surface.blit(self.light_surface, (0, 0))
|
||||||
light_mask = pygame.mask.from_surface(self.light_surface)
|
light_mask = pygame.mask.from_surface(self.light_surface)
|
||||||
|
|
@ -241,15 +225,34 @@ class Taschenlampe:
|
||||||
|
|
||||||
# Item zeichnen, wenn Masken sich überlappen
|
# Item zeichnen, wenn Masken sich überlappen
|
||||||
|
|
||||||
|
def create_light_points(self) -> list[pygame.Vector2]:
|
||||||
|
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)
|
||||||
|
|
||||||
|
# Überschneidung mit den Wänden prüfen
|
||||||
|
for wall in self.game.walls:
|
||||||
|
# Überschneidung
|
||||||
|
intersection = wall.clipline(origin, ray_end)
|
||||||
|
if intersection:
|
||||||
|
ray_end = pygame.Vector2(intersection[0])
|
||||||
|
points.append(ray_end)
|
||||||
|
|
||||||
|
return points
|
||||||
|
|
||||||
game = Game()
|
game = Game()
|
||||||
game.run()
|
game.run()
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
class Taschenlampe erstellen
|
in Klasse Flashlight:
|
||||||
|
|
||||||
Methoden: init(), update(), render()
|
|
||||||
|
|
||||||
WICHTIG: Alles in ihre richtigen Methoden
|
create_light_points() in Flashlight machen
|
||||||
|
|
||||||
|
render_item in Item Klasse machen
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue