diff --git a/game.py b/game.py index 225a004..8d2a69e 100644 --- a/game.py +++ b/game.py @@ -13,7 +13,7 @@ class Game: self.clock = pygame.Clock() self.screen: pygame.Surface = pygame.display.set_mode( (SCREEN_WIDTH, SCREEN_HEIGHT) - ) + ) self.display: pygame.Surface = pygame.Surface( (self.screen.get_width() // 2, self.screen.get_height() // 2) @@ -24,6 +24,7 @@ class Game: self.dt: int = 0 + self.player_pos = pygame.Vector2(self.display.get_width() / 2, self.display.get_height() / 2) self.player_width = 20 self.player_height = 20 @@ -53,7 +54,6 @@ class Game: self.dt: float = self.clock.tick(60) / 1000.0 - self.update() self.calculate_collsions() @@ -85,6 +85,9 @@ class Game: if event.key == pygame.K_LSHIFT: self.player.speed = 150 + if...: + ... + self.player.update(walls=self.walls, dt=self.dt) @@ -107,10 +110,12 @@ class Game: ray_end = self.player_pos + rotated_vector + # Überschneidung mit den Wänden prüfen for wall in self.walls: überschneidung = wall.clipline(self.player_pos, ray_end) + if überschneidung: schnittpunkt = pygame.Vector2(überschneidung[0]) @@ -120,12 +125,12 @@ class Game: self.light_points.append(ray_end) - self.flashlight_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) @@ -139,11 +144,48 @@ class Game: def calculate_collsions(self) -> None: - if self.item_rect.colliderect(self.flashlight_rect): - self.color_item = (0, 0, 255) - # print("HIII") - else: - self.color_item = (0, 0, 0) + + 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) + + def overlaps_rect(self, rect): + nearest_x = self.x + if self.x < rect.left: + nearest_x = rect.left + elif self.x > rect.right: + nearest_x = rect.right + + nearest_y = self.y + if self.y < rect.top: + nearest_y = rect.top + elif self.y > rect.bottom: + nearest_y = rect.bottom + + dx = self.x - nearest_x + dy = self.y - nearest_y + distance_squared = dx * dx + dy * dy + return distance_squared < self.radius * self.radius + class Player: @@ -237,24 +279,19 @@ game = Game() game.run() +""" +------------------------------------- HAUSAUFGABE ------------------------------------- +------------------------------------- MACHE, DASS ------------------------------------- +------------------------------------- DU NICHT W- ------------------------------------- - # def rect_circle_collision(self, rect, circle): - # rect_x, rect_y, rect_w, rect_h = rect +------------------------------------- EITERLÄUFST ------------------------------------- - # circle_x = circle[0][0] - # circle_y = circle[0][1] - # circle_r = circle[1] +------------------------------------- WENN DU AUS ------------------------------------- +------------------------------------- DEM SCREEN ------------------------------------- - # # Calculate the closest point on the rectangle to the center of the circle - # closest_x = max(rect_x, min(circle_x, rect_x + rect_w)) - # closest_y = max(rect_y, min(circle_y, rect_y + rect_h)) +------------------------------------- GEHST. ------------------------------------- +""" - # # Calculate the distance between the center of the circle and the closest point on the rectangle - # dx = circle_x - closest_x - # dy = circle_y - closest_y - # distance = (dx ** 2 + dy ** 2) ** 0.5 - - # return distance <= circle_r \ No newline at end of file