Methode calculate_collisions geleert + Lichtpunkte-Erstellung etwas umstrukturiert.
Tipp für die Zukunft: Bevor du eine Instanzvariable (wie z. B. self.light_points" erstellst, überlege dir zuerst, ob es sich um Daten handelt, die du an verschiedenen Stellen erreichen musst. Denn nur dafür sollten Instanzvariablen angelegt werden.
This commit is contained in:
parent
2b31c1de54
commit
820ba42213
1 changed files with 11 additions and 30 deletions
41
game.py
41
game.py
|
|
@ -1,4 +1,6 @@
|
||||||
import pygame
|
import pygame
|
||||||
|
from pygame.math import Vector2
|
||||||
|
from pygame.rect import Rect
|
||||||
|
|
||||||
SCREEN_WIDTH = 1280
|
SCREEN_WIDTH = 1280
|
||||||
SCREEN_HEIGHT = 720
|
SCREEN_HEIGHT = 720
|
||||||
|
|
@ -25,9 +27,10 @@ class Game:
|
||||||
self.item_rect = pygame.Rect(70, 150, 10, 10)
|
self.item_rect = pygame.Rect(70, 150, 10, 10)
|
||||||
self.flashlight_rect = pygame.Rect(0, 0, 0, 0)
|
self.flashlight_rect = pygame.Rect(0, 0, 0, 0)
|
||||||
self.color_item = (0, 0, 0)
|
self.color_item = (0, 0, 0)
|
||||||
self.light_points: list[pygame.Vector2] = []
|
|
||||||
# Level
|
# Level
|
||||||
self.walls = [pygame.Rect(100, 100, 20, 50), pygame.Rect(300, 300, 200, 20), pygame.Rect(550, 80, 20, 200)]
|
self.walls = [pygame.Rect(100, 100, 20, 50), pygame.Rect(300, 300, 200, 20), pygame.Rect(550, 80, 20, 200)]
|
||||||
|
# Licht
|
||||||
|
self.light_points: list[pygame.Vector2]
|
||||||
|
|
||||||
def run(self) -> None:
|
def run(self) -> None:
|
||||||
while self.running:
|
while self.running:
|
||||||
|
|
@ -50,11 +53,11 @@ class Game:
|
||||||
self.player.speed = 150
|
self.player.speed = 150
|
||||||
|
|
||||||
self.player.update(walls=self.walls, dt=self.dt)
|
self.player.update(walls=self.walls, dt=self.dt)
|
||||||
self.create_light_points()
|
self.light_points: list[Vector2] = self.create_light_points()
|
||||||
|
|
||||||
|
|
||||||
def create_light_points(self):
|
def create_light_points(self)-> list[pygame.Vector2]:
|
||||||
self.light_points = []
|
light_points: list[pygame.Vector2] = []
|
||||||
vector = pygame.Vector2(LIGHT_RADIUS, 0)
|
vector = pygame.Vector2(LIGHT_RADIUS, 0)
|
||||||
|
|
||||||
# Lichtstrahlen berechnen
|
# Lichtstrahlen berechnen
|
||||||
|
|
@ -68,15 +71,14 @@ class Game:
|
||||||
if überschneidung:
|
if überschneidung:
|
||||||
schnittpunkt = pygame.Vector2(überschneidung[0])
|
schnittpunkt = pygame.Vector2(überschneidung[0])
|
||||||
ray_end = schnittpunkt
|
ray_end = schnittpunkt
|
||||||
self.light_points.append(ray_end)
|
light_points.append(ray_end)
|
||||||
|
return light_points
|
||||||
|
|
||||||
def render(self) -> None:
|
def render(self) -> None:
|
||||||
|
|
||||||
self.display.fill((0, 0, 0, 0))
|
self.display.fill((0, 0, 0, 0))
|
||||||
|
|
||||||
self.flashlight_rect = pygame.draw.polygon(self.display, (243, 255, 74), self.light_points)
|
self.flashlight_rect: Rect = pygame.draw.polygon(self.display, (243, 255, 74), self.light_points)
|
||||||
pygame.draw.rect(self.display, (255, 0, 0), self.flashlight_rect, 1)
|
|
||||||
self.player.render(surface=self.display)
|
self.player.render(surface=self.display)
|
||||||
pygame.draw.rect(self.display, self.color_item, self.item_rect)
|
pygame.draw.rect(self.display, self.color_item, self.item_rect)
|
||||||
|
|
||||||
|
|
@ -87,28 +89,7 @@ class Game:
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
|
|
||||||
def calculate_collsions(self) -> None:
|
def calculate_collsions(self) -> None:
|
||||||
|
pass
|
||||||
for light_ray in self.light_points:
|
|
||||||
nearest_x = self.player_pos.x
|
|
||||||
if self.player_pos.x < self.item_rect.left:
|
|
||||||
nearest_x = self.item_rect.left
|
|
||||||
elif self.player_pos.x > self.item_rect.right:
|
|
||||||
nearest_x = self.item_rect.right
|
|
||||||
|
|
||||||
nearest_y = self.player_pos.y
|
|
||||||
if self.player_pos.y < self.item_rect.top:
|
|
||||||
nearest_y = self.item_rect.top
|
|
||||||
elif self.player_pos.y > self.item_rect.bottom:
|
|
||||||
nearest_y = self.item_rect.bottom
|
|
||||||
|
|
||||||
dx = self.player_pos.x - nearest_x
|
|
||||||
dy = self.player_pos.y - nearest_y
|
|
||||||
distance_squared = dx * dx + dy * dy
|
|
||||||
|
|
||||||
if distance_squared < light_ray.length_squared():
|
|
||||||
self.color_item = (0, 0, 255)
|
|
||||||
else:
|
|
||||||
self.color_item = (0, 0, 0)
|
|
||||||
|
|
||||||
|
|
||||||
class Player:
|
class Player:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue