Commit damit Jay es sich anschauen kann
This commit is contained in:
parent
0476dadfa4
commit
9ccdf57c7e
1 changed files with 59 additions and 22 deletions
81
game.py
81
game.py
|
|
@ -13,7 +13,7 @@ class Game:
|
||||||
self.clock = pygame.Clock()
|
self.clock = pygame.Clock()
|
||||||
self.screen: pygame.Surface = pygame.display.set_mode(
|
self.screen: pygame.Surface = pygame.display.set_mode(
|
||||||
(SCREEN_WIDTH, SCREEN_HEIGHT)
|
(SCREEN_WIDTH, SCREEN_HEIGHT)
|
||||||
)
|
)
|
||||||
|
|
||||||
self.display: pygame.Surface = pygame.Surface(
|
self.display: pygame.Surface = pygame.Surface(
|
||||||
(self.screen.get_width() // 2, self.screen.get_height() // 2)
|
(self.screen.get_width() // 2, self.screen.get_height() // 2)
|
||||||
|
|
@ -24,6 +24,7 @@ class Game:
|
||||||
|
|
||||||
self.dt: int = 0
|
self.dt: int = 0
|
||||||
|
|
||||||
|
|
||||||
self.player_pos = pygame.Vector2(self.display.get_width() / 2, self.display.get_height() / 2)
|
self.player_pos = pygame.Vector2(self.display.get_width() / 2, self.display.get_height() / 2)
|
||||||
self.player_width = 20
|
self.player_width = 20
|
||||||
self.player_height = 20
|
self.player_height = 20
|
||||||
|
|
@ -53,7 +54,6 @@ class Game:
|
||||||
self.dt: float = self.clock.tick(60) / 1000.0
|
self.dt: float = self.clock.tick(60) / 1000.0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
self.update()
|
self.update()
|
||||||
self.calculate_collsions()
|
self.calculate_collsions()
|
||||||
|
|
||||||
|
|
@ -85,6 +85,9 @@ class Game:
|
||||||
if event.key == pygame.K_LSHIFT:
|
if event.key == pygame.K_LSHIFT:
|
||||||
self.player.speed = 150
|
self.player.speed = 150
|
||||||
|
|
||||||
|
if...:
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
self.player.update(walls=self.walls, dt=self.dt)
|
self.player.update(walls=self.walls, dt=self.dt)
|
||||||
|
|
||||||
|
|
@ -107,10 +110,12 @@ class Game:
|
||||||
ray_end = self.player_pos + rotated_vector
|
ray_end = self.player_pos + rotated_vector
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Überschneidung mit den Wänden prüfen
|
# Überschneidung mit den Wänden prüfen
|
||||||
for wall in self.walls:
|
for wall in self.walls:
|
||||||
überschneidung = wall.clipline(self.player_pos, ray_end)
|
überschneidung = wall.clipline(self.player_pos, ray_end)
|
||||||
|
|
||||||
|
|
||||||
if überschneidung:
|
if überschneidung:
|
||||||
schnittpunkt = pygame.Vector2(überschneidung[0])
|
schnittpunkt = pygame.Vector2(überschneidung[0])
|
||||||
|
|
||||||
|
|
@ -120,12 +125,12 @@ class Game:
|
||||||
self.light_points.append(ray_end)
|
self.light_points.append(ray_end)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
self.flashlight_rect = pygame.draw.polygon(
|
self.flashlight_rect = pygame.draw.polygon(
|
||||||
self.display,
|
self.display,
|
||||||
(243, 255, 74),
|
(243, 255, 74),
|
||||||
self.light_points
|
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)
|
||||||
|
|
||||||
|
|
@ -139,11 +144,48 @@ class Game:
|
||||||
|
|
||||||
|
|
||||||
def calculate_collsions(self) -> None:
|
def calculate_collsions(self) -> None:
|
||||||
if self.item_rect.colliderect(self.flashlight_rect):
|
|
||||||
self.color_item = (0, 0, 255)
|
for light_ray in self.light_points:
|
||||||
# print("HIII")
|
|
||||||
else:
|
nearest_x = self.player_pos.x
|
||||||
self.color_item = (0, 0, 0)
|
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:
|
class Player:
|
||||||
|
|
@ -237,24 +279,19 @@ game = Game()
|
||||||
game.run()
|
game.run()
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
------------------------------------- HAUSAUFGABE -------------------------------------
|
||||||
|
|
||||||
|
------------------------------------- MACHE, DASS -------------------------------------
|
||||||
|
|
||||||
|
------------------------------------- DU NICHT W- -------------------------------------
|
||||||
|
|
||||||
# def rect_circle_collision(self, rect, circle):
|
------------------------------------- EITERLÄUFST -------------------------------------
|
||||||
# rect_x, rect_y, rect_w, rect_h = rect
|
|
||||||
|
|
||||||
# circle_x = circle[0][0]
|
------------------------------------- WENN DU AUS -------------------------------------
|
||||||
# circle_y = circle[0][1]
|
|
||||||
# circle_r = circle[1]
|
|
||||||
|
|
||||||
|
------------------------------------- DEM SCREEN -------------------------------------
|
||||||
|
|
||||||
# # Calculate the closest point on the rectangle to the center of the circle
|
------------------------------------- GEHST. -------------------------------------
|
||||||
# closest_x = max(rect_x, min(circle_x, rect_x + rect_w))
|
"""
|
||||||
# closest_y = max(rect_y, min(circle_y, rect_y + rect_h))
|
|
||||||
|
|
||||||
# # 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
|
|
||||||
Loading…
Reference in a new issue