From 2a0066deca119edc0ce0e40d5a5a6deaee6c1955 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Wed, 26 Aug 2026 18:35:06 +0200 Subject: [PATCH] =?UTF-8?q?ENDLICH=20HAUSUAFGABE=20GEL=C3=96ST!!!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- der_test.py | 62 ++++++++++++++++++++++++++++++++++++++++------------- game.py | 53 ++++++++++++++++++++++++++++++++++----------- lesen.txt | 44 +++++++++++++++++++++++++++++++++++++ polygon.py | 52 ++++++++++++++++++++++++++++++++++++++++++++ testo.py | 30 ++++++++++++++++++++++++++ 5 files changed, 213 insertions(+), 28 deletions(-) create mode 100644 lesen.txt create mode 100644 polygon.py create mode 100644 testo.py diff --git a/der_test.py b/der_test.py index 29f3e4d..7379903 100644 --- a/der_test.py +++ b/der_test.py @@ -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() \ No newline at end of file +pygame.quit() diff --git a/game.py b/game.py index 013ac6f..b31bc5f 100644 --- a/game.py +++ b/game.py @@ -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() diff --git a/lesen.txt b/lesen.txt new file mode 100644 index 0000000..5455dc9 --- /dev/null +++ b/lesen.txt @@ -0,0 +1,44 @@ +[Vector2(633.62, 371.115), + Vector2(627.046, 382.116), + Vector2(620.282, 393.002), + Vector2(613.329, 403.767), + Vector2(606.189, 414.41), + Vector2(598.864, 424.926), + Vector2(591.357, 435.313), + Vector2(583.669, 445.567), + Vector2(575.804, 455.686), + Vector2(567.764, 465.666), + Vector2(559.55, 475.504), + Vector2(551.166, 485.197), + Vector2(542.614, 494.742), + Vector2(533.897, 504.136), + Vector2(525.018, 513.377), + Vector2(515.978, 522.462), + Vector2(506.781, 531.388), + Vector2(497.43, 540.151), + Vector2(487.927, 548.75), + Vector2(478.276, 557.182), + Vector2(468.479, 565.444), + Vector2(458.539, 573.534), + Vector2(448.46, 581.45), + Vector2(438.244, 589.188), + Vector2(427.894, 596.746), + Vector2(417.415, 604.123), + Vector2(406.808, 611.316), + Vector2(396.077, 618.323), + Vector2(385.225, 625.141), + Vector2(374.256, 631.769), + Vector2(363.173, 638.205), + Vector2(351.98, 644.446), + Vector2(340, 650), + Vector2(334, 650), + Vector2(328, 650), + Vector2(322, 650), + Vector2(316, 650), + Vector2(310, 650), + Vector2(304, 650), + Vector2(298, 650), + Vector2(292, 650), + Vector2(285, 650), + Vector2(273.613, 654.875), + Vector2(262.142, 659.55), Vector2(250.591, 664.025), Vector2(238.964, 668.297), Vector2(227.264, 672.366), Vector2(215.495, 676.23), Vector2(203.661, 679.888), Vector2(191.764, 683.339), Vector2(179.809, 686.581), Vector2(167.799, 689.615), Vector2(155.738, 692.438), Vector2(143.629, 695.051), Vector2(131.477, 697.452), Vector2(119.285, 699.64), Vector2(107.056, 701.615), Vector2(94.7953, 703.377), Vector2(82.5052, 704.924), Vector2(70.1901, 706.257), Vector2(57.8535, 707.374), Vector2(45.4993, 708.276), Vector2(33.1313, 708.962), Vector2(20.7531, 709.432), Vector2(8.36865, 709.687), Vector2(-4.01836, 709.724), Vector2(-16.4042, 709.546), Vector2(-28.7849, 709.152), Vector2(-41.157, 708.542), Vector2(-53.5165, 707.715), Vector2(-65.8596, 706.674), Vector2(-78.1828, 705.417), Vector2(-90.4821, 703.945), Vector2(-102.754, 702.258), Vector2(-114.994, 700.358), Vector2(-127.2, 698.244), Vector2(-139.366, 695.918), Vector2(-151.491, 693.38), Vector2(-163.569, 690.63), Vector2(-175.597, 687.671), Vector2(-187.572, 684.501), Vector2(-199.489, 681.123), Vector2(-211.346, 677.538), Vector2(-223.139, 673.746), Vector2(-234.863, 669.749), Vector2(-246.516, 665.549), Vector2(-258.094, 661.145), Vector2(-269.593, 656.54), Vector2(-281.01, 651.735), Vector2(-292.342, 646.731), Vector2(-303.584, 641.531), Vector2(-314.734, 636.135), Vector2(-325.789, 630.545), Vector2(-336.743, 624.763), Vector2(-347.596, 618.791), Vector2(-358.342, 612.63), Vector2(-368.98, 606.283), Vector2(-379.504, 599.751), Vector2(-389.914, 593.036), Vector2(-400.204, 586.141), Vector2(-410.373, 579.067), Vector2(-420.417, 571.817), Vector2(-430.332, 564.393), Vector2(-440.117, 556.797), Vector2(-449.767, 549.031), Vector2(-459.28, 541.098), Vector2(-468.654, 533), Vector2(-477.885, 524.739), Vector2(-486.97, 516.319), Vector2(-495.907, 507.742), Vector2(-504.692, 499.01), Vector2(-513.324, 490.125), Vector2(-521.8, 481.092), Vector2(-530.117, 471.912), Vector2(-538.272, 462.588), Vector2(-546.263, 453.124), Vector2(-554.088, 443.521), Vector2(-561.744, 433.784), Vector2(-569.229, 423.914), Vector2(-576.541, 413.915), Vector2(-583.677, 403.79), Vector2(-590.635, 393.542), Vector2(-597.414, 383.174), Vector2(-604.01, 372.689), Vector2(-610.422, 362.091), Vector2(-616.649, 351.382), Vector2(-622.687, 340.567), Vector2(-628.536, 329.647), Vector2(-634.193, 318.628), Vector2(-639.658, 307.511), Vector2(-644.927, 296.301), Vector2(-650, 285), Vector2(-654.875, 273.613), Vector2(-659.55, 262.142), Vector2(-664.025, 250.591), Vector2(-668.297, 238.964), Vector2(-672.366, 227.264), Vector2(-676.23, 215.495), Vector2(-679.888, 203.661), Vector2(-683.339, 191.764), Vector2(-686.581, 179.809), Vector2(-689.615, 167.799), Vector2(-692.438, 155.738), Vector2(-695.051, 143.629), Vector2(-697.452, 131.477), Vector2(-699.64, 119.285), Vector2(-701.615, 107.056), Vector2(-703.377, 94.7953), Vector2(-704.924, 82.5052), Vector2(-706.257, 70.1901), Vector2(-707.374, 57.8535), Vector2(-708.276, 45.4993), Vector2(-708.962, 33.1313), Vector2(-709.432, 20.7531), Vector2(-709.687, 8.36865), Vector2(-709.724, -4.01836), Vector2(-709.546, -16.4042), Vector2(-709.152, -28.7849), Vector2(-708.542, -41.157), Vector2(-707.715, -53.5165), Vector2(-706.674, -65.8596), Vector2(-705.417, -78.1828), Vector2(-703.945, -90.4821), Vector2(-702.258, -102.754), Vector2(-700.358, -114.994), Vector2(-698.244, -127.2), Vector2(-695.918, -139.366), Vector2(-693.38, -151.491), Vector2(-690.63, -163.569), Vector2(-687.671, -175.597), Vector2(-684.501, -187.572), Vector2(-681.123, -199.489), Vector2(-677.538, -211.346), Vector2(-673.746, -223.139), Vector2(-669.749, -234.863), Vector2(-665.549, -246.516), Vector2(-661.145, -258.094), Vector2(-656.54, -269.593), Vector2(-651.735, -281.01), Vector2(-646.731, -292.342), Vector2(-641.531, -303.584), Vector2(-636.135, -314.734), Vector2(-630.545, -325.789), Vector2(-624.763, -336.743), Vector2(-618.791, -347.596), Vector2(-612.63, -358.342), Vector2(-606.283, -368.98), Vector2(-599.751, -379.504), Vector2(-593.036, -389.914), Vector2(-586.141, -400.204), Vector2(-579.067, -410.373), Vector2(-571.817, -420.417), Vector2(-564.393, -430.332), Vector2(-556.797, -440.117), Vector2(-549.031, -449.767), Vector2(-541.098, -459.28), Vector2(-533, -468.654), Vector2(-524.739, -477.885), Vector2(-516.319, -486.97), Vector2(-507.742, -495.907), Vector2(-499.01, -504.692), Vector2(-490.125, -513.324), Vector2(-481.092, -521.8), Vector2(-471.912, -530.117), Vector2(-462.588, -538.272), Vector2(-453.124, -546.263), Vector2(-443.521, -554.088), Vector2(-433.784, -561.744), Vector2(-423.914, -569.229), Vector2(-413.915, -576.541), Vector2(-403.79, -583.677), Vector2(-393.542, -590.635), Vector2(-383.174, -597.414), Vector2(-372.689, -604.01), Vector2(-362.091, -610.422), Vector2(-351.382, -616.649), Vector2(-340.567, -622.687), Vector2(-329.647, -628.536), Vector2(-318.628, -634.193), Vector2(-307.511, -639.658), Vector2(-296.301, -644.927), Vector2(-285, -650), Vector2(-273.613, -654.875), Vector2(-262.142, -659.55), Vector2(-250.591, -664.025), Vector2(-238.964, -668.297), Vector2(-227.264, -672.366), Vector2(-215.495, -676.23), Vector2(-203.661, -679.888), Vector2(-191.764, -683.339), Vector2(-179.809, -686.581), Vector2(-167.799, -689.615), Vector2(-155.738, -692.438), Vector2(-143.629, -695.051), Vector2(-131.477, -697.452), Vector2(-119.285, -699.64), Vector2(-107.056, -701.615), Vector2(-94.7953, -703.377), Vector2(-82.5052, -704.924), Vector2(-70.1901, -706.257), Vector2(-57.8535, -707.374), Vector2(-45.4993, -708.276), Vector2(-33.1313, -708.962), Vector2(-20.7531, -709.432), Vector2(-8.36865, -709.687), Vector2(4.01836, -709.724), Vector2(16.4042, -709.546), Vector2(28.7849, -709.152), Vector2(41.157, -708.542), Vector2(53.5165, -707.715), Vector2(65.8596, -706.674), Vector2(78.1828, -705.417), Vector2(90.4821, -703.945), Vector2(102.754, -702.258), Vector2(114.994, -700.358), Vector2(127.2, -698.244), Vector2(139.366, -695.918), Vector2(151.491, -693.38), Vector2(163.569, -690.63), Vector2(175.597, -687.671), Vector2(187.572, -684.501), Vector2(199.489, -681.123), Vector2(211.346, -677.538), Vector2(223.139, -673.746), Vector2(234.863, -669.749), Vector2(246.516, -665.549), Vector2(258.094, -661.145), Vector2(269.593, -656.54), Vector2(281.01, -651.735), Vector2(292.342, -646.731), Vector2(303.584, -641.531), Vector2(314.734, -636.135), Vector2(325.789, -630.545), Vector2(336.743, -624.763), Vector2(347.596, -618.791), Vector2(358.342, -612.63), Vector2(368.98, -606.283), Vector2(379.504, -599.751), Vector2(389.914, -593.036), Vector2(400.204, -586.141), Vector2(410.373, -579.067), Vector2(420.417, -571.817), Vector2(430.332, -564.393), Vector2(440.117, -556.797), Vector2(449.767, -549.031), Vector2(459.28, -541.098), Vector2(468.654, -533), Vector2(477.885, -524.739), Vector2(486.97, -516.319), Vector2(495.907, -507.742), Vector2(504.692, -499.01), Vector2(513.324, -490.125), Vector2(521.8, -481.092), Vector2(530.117, -471.912), Vector2(538.272, -462.588), Vector2(546.263, -453.124), Vector2(554.088, -443.521), Vector2(561.744, -433.784), Vector2(569.229, -423.914), Vector2(576.541, -413.915), Vector2(583.677, -403.79), Vector2(590.635, -393.542), Vector2(597.414, -383.174), Vector2(604.01, -372.689), Vector2(610.422, -362.091), Vector2(616.649, -351.382), Vector2(622.687, -340.567), Vector2(628.536, -329.647), Vector2(634.193, -318.628), Vector2(639.658, -307.511), Vector2(644.927, -296.301), Vector2(650, -285), Vector2(654.875, -273.613), Vector2(659.55, -262.142), Vector2(664.025, -250.591), Vector2(668.297, -238.964), Vector2(672.366, -227.264), Vector2(676.23, -215.495), Vector2(679.888, -203.661), Vector2(683.339, -191.764), Vector2(686.581, -179.809), Vector2(689.615, -167.799), Vector2(692.438, -155.738), Vector2(695.051, -143.629), Vector2(697.452, -131.477), Vector2(699.64, -119.285), Vector2(701.615, -107.056), Vector2(703.377, -94.7953), Vector2(704.924, -82.5052), Vector2(706.257, -70.1901), Vector2(707.374, -57.8535), Vector2(708.276, -45.4993), Vector2(708.962, -33.1313), Vector2(709.432, -20.7531), Vector2(709.687, -8.36865), Vector2(709.724, 4.01836), Vector2(709.546, 16.4042), Vector2(709.152, 28.7849), Vector2(708.542, 41.157), Vector2(707.715, 53.5165), Vector2(706.674, 65.8596), Vector2(705.417, 78.1828), Vector2(703.945, 90.4821), Vector2(702.258, 102.754), Vector2(700.358, 114.994), Vector2(698.244, 127.2), Vector2(695.918, 139.366), Vector2(693.38, 151.491), Vector2(690.63, 163.569), Vector2(687.671, 175.597), Vector2(684.501, 187.572), Vector2(681.123, 199.489), Vector2(677.538, 211.346), Vector2(673.746, 223.139), Vector2(669.749, 234.863), Vector2(665.549, 246.516), Vector2(661.145, 258.094), Vector2(656.54, 269.593), Vector2(651.735, 281.01), Vector2(646.731, 292.342)] \ No newline at end of file diff --git a/polygon.py b/polygon.py new file mode 100644 index 0000000..a6d5122 --- /dev/null +++ b/polygon.py @@ -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() \ No newline at end of file diff --git a/testo.py b/testo.py new file mode 100644 index 0000000..57f5d6e --- /dev/null +++ b/testo.py @@ -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() \ No newline at end of file