Compare commits
2 commits
215d2c4865
...
2a0066deca
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2a0066deca | ||
|
|
9e66b315d6 |
6 changed files with 270 additions and 14 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -220,6 +220,5 @@ __marimo__/
|
|||
.streamlit/secrets.toml
|
||||
|
||||
|
||||
der_test.py
|
||||
rechner.py
|
||||
data/
|
||||
104
der_test.py
Normal file
104
der_test.py
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
import math
|
||||
|
||||
import pygame
|
||||
|
||||
# pygame setup
|
||||
pygame.init()
|
||||
screen = pygame.display.set_mode((1280, 720))
|
||||
clock = pygame.time.Clock()
|
||||
running = True
|
||||
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),
|
||||
]
|
||||
|
||||
|
||||
|
||||
|
||||
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
|
||||
|
||||
if event.type == pygame.KEYDOWN:
|
||||
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)
|
||||
|
||||
end_pos = pygame.Vector2(player_pos[0] + 300, player_pos[1])
|
||||
|
||||
# pygame.draw.line(screen, "cyan", (640, 360), vector2)
|
||||
|
||||
"""--------------------------------------- WICHTIG: Vector2.rotate() ---------------------------------------"""
|
||||
|
||||
|
||||
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]:
|
||||
player_pos.y -= 300 * dt
|
||||
if keys[pygame.K_s]:
|
||||
player_pos.y += 300 * dt
|
||||
if keys[pygame.K_a]:
|
||||
player_pos.x -= 300 * dt
|
||||
if keys[pygame.K_d]:
|
||||
player_pos.x += 300 * dt
|
||||
|
||||
# flip() the display to put your work on screen
|
||||
pygame.display.flip()
|
||||
|
||||
# limits FPS to 60
|
||||
# dt is delta time in seconds since last frame, used for framerate-
|
||||
# independent physics.
|
||||
dt = clock.tick(60) / 1000
|
||||
|
||||
|
||||
pygame.quit()
|
||||
53
game.py
53
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()
|
||||
|
|
|
|||
44
lesen.txt
Normal file
44
lesen.txt
Normal file
File diff suppressed because one or more lines are too long
52
polygon.py
Normal file
52
polygon.py
Normal 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
30
testo.py
Normal 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()
|
||||
Loading…
Reference in a new issue