2026-08-19 16:27:21 +00:00
|
|
|
import math
|
2026-07-17 15:45:18 +00:00
|
|
|
|
2026-08-26 16:35:06 +00:00
|
|
|
import pygame
|
|
|
|
|
|
2026-08-07 16:20:24 +00:00
|
|
|
SCREEN_WIDTH = 1280
|
|
|
|
|
SCREEN_HEIGHT = 720
|
2026-07-17 15:45:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class Game:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
pygame.init()
|
2026-07-24 16:33:42 +00:00
|
|
|
|
2026-07-17 15:45:18 +00:00
|
|
|
pygame.display.set_caption("School 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)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.movement: list[bool] = [False, False, False, False]
|
|
|
|
|
|
|
|
|
|
self.running: bool = True
|
|
|
|
|
|
2026-07-24 15:50:34 +00:00
|
|
|
self.dt: int = 0
|
2026-07-17 15:45:18 +00:00
|
|
|
|
2026-07-24 15:50:34 +00:00
|
|
|
self.player_pos = pygame.Vector2(self.display.get_width() / 2, self.display.get_height() / 2)
|
2026-07-17 15:45:18 +00:00
|
|
|
|
2026-07-24 16:33:42 +00:00
|
|
|
self.item_rect = pygame.Rect(60, 40, 10, 10)
|
|
|
|
|
|
2026-08-07 16:20:24 +00:00
|
|
|
self.flashlight_rect = (self.player_pos, 50)
|
2026-08-06 09:38:54 +00:00
|
|
|
|
2026-07-24 16:33:42 +00:00
|
|
|
self.flashlight = 0
|
|
|
|
|
|
|
|
|
|
self.color_item = (0, 0, 0)
|
|
|
|
|
|
2026-08-07 16:20:24 +00:00
|
|
|
self.speed = 300
|
|
|
|
|
|
2026-08-07 16:36:14 +00:00
|
|
|
self.player_width = 20
|
|
|
|
|
self.player_height = 20
|
|
|
|
|
|
2026-08-19 16:27:21 +00:00
|
|
|
self.walls = [
|
2026-08-27 18:52:03 +00:00
|
|
|
pygame.Rect(100, 100, 20, 50),
|
|
|
|
|
pygame.Rect(300, 300, 200, 20),
|
|
|
|
|
pygame.Rect(550, 80, 20, 200)
|
2026-08-19 16:27:21 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# self.circle = pygame.geometry.Circle(self.player_pos[0], self.player_pos[1], 30)
|
2026-07-24 16:33:42 +00:00
|
|
|
|
2026-07-17 15:45:18 +00:00
|
|
|
def run(self) -> None:
|
|
|
|
|
while self.running:
|
2026-07-24 15:50:34 +00:00
|
|
|
# Background
|
2026-07-17 15:45:18 +00:00
|
|
|
self.display.fill((0, 0, 0, 0))
|
|
|
|
|
|
2026-08-07 16:36:14 +00:00
|
|
|
self.player_rect = pygame.Rect(self.player_pos[0]-10, self.player_pos[1]-10, self.player_width, self.player_height)
|
2026-07-24 16:33:42 +00:00
|
|
|
|
|
|
|
|
|
2026-07-24 15:59:36 +00:00
|
|
|
# Event-Keys
|
2026-07-17 15:45:18 +00:00
|
|
|
for event in pygame.event.get():
|
|
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
|
self.running = False
|
|
|
|
|
|
|
|
|
|
if event.type == pygame.KEYDOWN:
|
|
|
|
|
if event.key == pygame.K_ESCAPE:
|
|
|
|
|
self.running = False
|
|
|
|
|
|
2026-08-07 16:20:24 +00:00
|
|
|
if event.key == pygame.K_LSHIFT:
|
|
|
|
|
self.speed = 600
|
|
|
|
|
|
|
|
|
|
if event.type == pygame.KEYUP:
|
|
|
|
|
if event.key == pygame.K_LSHIFT:
|
|
|
|
|
self.speed = 300
|
|
|
|
|
|
2026-07-24 15:59:36 +00:00
|
|
|
# Player-Draw
|
2026-08-19 16:27:21 +00:00
|
|
|
# self.flashlight = pygame.draw.circle(self.display, (255, 255, 255, 0), self.flashlight_rect[0], self.flashlight_rect[1])
|
2026-08-26 16:35:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
"""RAYCASTING"""
|
|
|
|
|
ray_end = pygame.Vector2()
|
|
|
|
|
light_points = []
|
|
|
|
|
vector = pygame.Vector2(75, 0)
|
|
|
|
|
|
|
|
|
|
# Lichtstrahlen berechnen
|
2026-08-19 16:27:21 +00:00
|
|
|
for i in range(360):
|
|
|
|
|
|
2026-08-26 16:35:06 +00:00
|
|
|
rotated = vector.rotate(i)
|
|
|
|
|
ray_end = self.player_pos + rotated
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Überschneidung mit den Wänden prüfen
|
|
|
|
|
for wall in self.walls:
|
|
|
|
|
überschneidung = wall.clipline(self.player_pos, ray_end)
|
2026-08-19 16:27:21 +00:00
|
|
|
|
2026-08-26 16:35:06 +00:00
|
|
|
if überschneidung:
|
|
|
|
|
schnittpunkt = pygame.Vector2(überschneidung[0])
|
2026-08-19 16:27:21 +00:00
|
|
|
|
2026-08-26 16:35:06 +00:00
|
|
|
ray_end = schnittpunkt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
light_points.append(ray_end)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pygame.draw.polygon(
|
|
|
|
|
self.display,
|
|
|
|
|
"yellow",
|
|
|
|
|
light_points
|
|
|
|
|
)
|
2026-07-17 15:45:18 +00:00
|
|
|
|
2026-07-24 16:33:42 +00:00
|
|
|
pygame.draw.rect(self.display, self.color_item, self.item_rect)
|
|
|
|
|
|
|
|
|
|
pygame.draw.rect(self.display, (255, 0, 0), self.player_rect)
|
2026-07-24 15:59:36 +00:00
|
|
|
|
2026-08-19 16:27:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
for wall in self.walls:
|
|
|
|
|
pygame.draw.rect(self.display, "grey", wall)
|
|
|
|
|
|
2026-07-24 15:59:36 +00:00
|
|
|
# Smooth Movement
|
2026-07-24 15:50:34 +00:00
|
|
|
keys = pygame.key.get_pressed()
|
2026-08-07 16:36:14 +00:00
|
|
|
|
2026-07-24 15:50:34 +00:00
|
|
|
if keys[pygame.K_w]:
|
2026-08-07 16:36:14 +00:00
|
|
|
|
2026-08-07 16:20:24 +00:00
|
|
|
self.player_pos.y -= self.speed * self.dt
|
2026-08-07 16:36:14 +00:00
|
|
|
|
2026-07-24 15:50:34 +00:00
|
|
|
if keys[pygame.K_s]:
|
2026-08-07 16:20:24 +00:00
|
|
|
self.player_pos.y += self.speed * self.dt
|
2026-08-07 16:36:14 +00:00
|
|
|
|
2026-07-24 15:50:34 +00:00
|
|
|
if keys[pygame.K_a]:
|
2026-08-07 16:20:24 +00:00
|
|
|
self.player_pos.x -= self.speed * self.dt
|
2026-08-07 16:36:14 +00:00
|
|
|
|
2026-07-24 15:50:34 +00:00
|
|
|
if keys[pygame.K_d]:
|
2026-08-07 16:36:14 +00:00
|
|
|
self.player_pos.x += self.speed * self.dt
|
2026-08-07 16:20:24 +00:00
|
|
|
|
2026-07-17 15:45:18 +00:00
|
|
|
|
2026-08-07 16:36:14 +00:00
|
|
|
if self.player_pos[1] < self.player_height/2:
|
|
|
|
|
self.player_pos[1] = self.player_height/2
|
|
|
|
|
|
|
|
|
|
if self.player_pos[1] > SCREEN_HEIGHT/2-self.player_height/2:
|
|
|
|
|
self.player_pos[1] = SCREEN_HEIGHT/2-self.player_height/2
|
|
|
|
|
|
|
|
|
|
if self.player_pos[0] < self.player_width/2:
|
|
|
|
|
self.player_pos[0] = self.player_width/2
|
|
|
|
|
|
|
|
|
|
if self.player_pos[0] > SCREEN_WIDTH/2-self.player_width/2:
|
|
|
|
|
self.player_pos[0] = SCREEN_WIDTH/2-self.player_width/2
|
|
|
|
|
|
|
|
|
|
|
2026-08-06 09:38:54 +00:00
|
|
|
|
|
|
|
|
if self.rect_circle_collision(self.item_rect, self.flashlight_rect):
|
2026-07-24 16:33:42 +00:00
|
|
|
self.color_item = (0, 0, 255)
|
2026-08-06 09:38:54 +00:00
|
|
|
else:
|
|
|
|
|
self.color_item = (0, 0, 0)
|
2026-07-24 16:33:42 +00:00
|
|
|
|
2026-07-17 15:45:18 +00:00
|
|
|
self.screen.blit(
|
|
|
|
|
pygame.transform.scale(self.display, self.screen.get_size())
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
pygame.display.update()
|
2026-07-24 15:50:34 +00:00
|
|
|
self.dt = self.clock.tick(60) / 3000
|
2026-07-17 15:45:18 +00:00
|
|
|
|
2026-08-06 09:38:54 +00:00
|
|
|
def rect_circle_collision(self, rect, circle):
|
|
|
|
|
rect_x, rect_y, rect_w, rect_h = rect
|
|
|
|
|
|
|
|
|
|
circle_x = circle[0][0]
|
|
|
|
|
circle_y = circle[0][1]
|
|
|
|
|
circle_r = circle[1]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 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))
|
|
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
2026-08-26 16:35:06 +00:00
|
|
|
# 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
|
2026-08-07 16:36:14 +00:00
|
|
|
|
2026-08-26 16:35:06 +00:00
|
|
|
# # 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))
|
2026-08-06 09:38:54 +00:00
|
|
|
|
2026-08-26 16:35:06 +00:00
|
|
|
# return end_pos
|
2026-07-17 15:45:18 +00:00
|
|
|
|
|
|
|
|
game = Game()
|
2026-07-24 15:50:34 +00:00
|
|
|
game.run()
|