ENDLICH HAUSUAFGABE GELÖST!!!

This commit is contained in:
Benjamin 2026-08-26 18:35:06 +02:00
parent 9e66b315d6
commit 2a0066deca
5 changed files with 213 additions and 28 deletions

View file

@ -1,6 +1,7 @@
import pygame
import math
import pygame
# pygame setup
pygame.init()
screen = pygame.display.set_mode((1280, 720))
@ -10,11 +11,12 @@ dt = 0
player_pos = pygame.Vector2(screen.get_width() / 2, screen.get_height() / 2)
walls = [ pygame.Rect(980, 100, 50, 440), pygame.Rect(280, 650, 720, 35), ]
walls = [
pygame.Rect(980, 100, 50, 440),
pygame.Rect(280, 650, 720, 35),
]
vector2 = pygame.Vector2(30, 30)
schnittpunkt = pygame.Vector2(0, 0)
while running:
@ -28,26 +30,57 @@ while running:
if event.key == pygame.K_ESCAPE:
running = False
if event.key == pygame.K_ESCAPE:
running = False
# fill the screen with a color to wipe away anything from last frame
screen.fill("black")
for wall in walls:
pygame.draw.rect(screen, "grey", wall)
pygame.draw.circle(screen, "red", player_pos, 40)
end_pos = pygame.Vector2(player_pos[0]+300, player_pos[1])
pygame.draw.line(screen, (255, 255, 130), player_pos, end_pos, width=3)
end_pos = pygame.Vector2(player_pos[0] + 300, player_pos[1])
# pygame.draw.line(screen, "cyan", (640, 360), vector2)
"""--------------------------------------- WICHTIG: Vector2.rotate() ---------------------------------------"""
for wall in walls:
schnittpunkt = wall.clipline(player_pos, end_pos)[0]
ray_end = pygame.Vector2()
light_points = []
vector = pygame.Vector2(300, 0)
# Lichtstrahlen berechnen
for i in range(360):
rotated = vector.rotate(i)
ray_end = player_pos + rotated
# Überschneidung mit den Wänden prüfen
for wall in walls:
überschneidung = wall.clipline(player_pos, ray_end)
if überschneidung:
schnittpunkt = pygame.Vector2(überschneidung[0])
ray_end = schnittpunkt
light_points.append(ray_end)
pygame.draw.polygon(
screen,
"yellow",
light_points
)
pygame.draw.circle(screen, "red", player_pos, 40)
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
@ -68,5 +101,4 @@ while running:
dt = clock.tick(60) / 1000
pygame.quit()
pygame.quit()

53
game.py
View file

@ -1,6 +1,7 @@
import pygame
import math
import pygame
SCREEN_WIDTH = 1280
SCREEN_HEIGHT = 720
@ -74,14 +75,40 @@ class Game:
# Player-Draw
# self.flashlight = pygame.draw.circle(self.display, (255, 255, 255, 0), self.flashlight_rect[0], self.flashlight_rect[1])
"""RAYCASTING"""
ray_end = pygame.Vector2()
light_points = []
vector = pygame.Vector2(75, 0)
# Lichtstrahlen berechnen
for i in range(360):
end_pos = self.calculate_flashlight_radiate(i, self.player_pos, 50)
# rect = pygame.Rect(self.player_pos[0], self.player_pos[1], end_pos[0], end_pos[1])
rotated = vector.rotate(i)
ray_end = self.player_pos + rotated
# pygame.draw.rect(self.display, (255, 255, 130), rect)
# Überschneidung mit den Wänden prüfen
for wall in self.walls:
überschneidung = wall.clipline(self.player_pos, ray_end)
pygame.draw.line(self.display, (255, 255, 130), self.player_pos, end_pos, width=3)
if überschneidung:
schnittpunkt = pygame.Vector2(überschneidung[0])
ray_end = schnittpunkt
light_points.append(ray_end)
pygame.draw.polygon(
self.display,
"yellow",
light_points
)
pygame.draw.rect(self.display, self.color_item, self.item_rect)
@ -154,16 +181,16 @@ class Game:
return distance <= circle_r
def calculate_flashlight_radiate(self, degrees, start_pos, length):
angle_deg = degrees # Angle in degrees
# def calculate_flashlight_radiate(self, degrees: int, start_pos: tuple[int, int] | list[int, int] | pygame.Vector2, length: int) -> tuple[int, int]:
# angle_deg = degrees # Angle in degrees
# Convert degrees to radians for math functions
angle_rad = math.radians(angle_deg)
end_x = start_pos[0] + length * math.cos(angle_rad)
end_y = start_pos[1] - length * math.sin(angle_rad) # Minus because y increases downward in Pygame
end_pos = (int(end_x), int(end_y))
# # Convert degrees to radians for math functions
# angle_rad = math.radians(angle_deg)
# end_x = start_pos[0] + length * math.cos(angle_rad)
# end_y = start_pos[1] - length * math.sin(angle_rad) # Minus because y increases downward in Pygame
# end_pos = (int(end_x), int(end_y))
return end_pos
# return end_pos
game = Game()
game.run()

44
lesen.txt Normal file

File diff suppressed because one or more lines are too long

52
polygon.py Normal file
View file

@ -0,0 +1,52 @@
# Example file showing a basic pygame "game loop"
import pygame
import math
# pygame setup
pygame.init()
screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock()
running = True
def calculate_flashlight_radiate(degrees: int, start_pos: tuple[int, int] | list[int, int] | pygame.Vector2, length: int) -> tuple[int, int]:
angle_deg = degrees # Angle in degrees
# Convert degrees to radians for math functions
angle_rad = math.radians(angle_deg)
end_x = start_pos[0] + length * math.cos(angle_rad)
end_y = start_pos[1] - length * math.sin(angle_rad) # Minus because y increases downward in Pygame
end_pos = (int(end_x), int(end_y))
return end_pos
points = []
while running:
# poll for events
# pygame.QUIT event means the user clicked X to close your window
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# fill the screen with a color to wipe away anything from last frame
screen.fill("purple")
# RENDER YOUR GAME HERE
for i in range(360):
point = calculate_flashlight_radiate(i, (640, 360), 50)
points.append(point)
pygame.draw.polygon(screen, (27, 134, 3), points)
points = []
# flip() the display to put your work on screen
pygame.display.flip()
clock.tick(60) # limits FPS to 60
pygame.quit()

30
testo.py Normal file
View file

@ -0,0 +1,30 @@
# Example file showing a basic pygame "game loop"
import pygame
# pygame setup
pygame.init()
screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock()
running = True
while running:
# poll for events
# pygame.QUIT event means the user clicked X to close your window
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# fill the screen with a color to wipe away anything from last frame
screen.fill("purple")
# RENDER YOUR GAME HERE
vector = pygame.Vector2(640, 360)
for i in range(10):
print(vector.rotate(i))
# flip() the display to put your work on screen
pygame.display.flip()
clock.tick(60) # limits FPS to 60
pygame.quit()