Ende 28.08.2026
This commit is contained in:
parent
181024191e
commit
737f7b693e
8 changed files with 188 additions and 31 deletions
61
game.py
61
game.py
|
|
@ -1,5 +1,3 @@
|
||||||
import math
|
|
||||||
|
|
||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
SCREEN_WIDTH = 1280
|
SCREEN_WIDTH = 1280
|
||||||
|
|
@ -47,7 +45,7 @@ class Game:
|
||||||
pygame.Rect(300, 300, 200, 20),
|
pygame.Rect(300, 300, 200, 20),
|
||||||
pygame.Rect(550, 80, 20, 200)
|
pygame.Rect(550, 80, 20, 200)
|
||||||
]
|
]
|
||||||
|
self.light_radius = 75
|
||||||
|
|
||||||
# self.circle = pygame.geometry.Circle(self.player_pos[0], self.player_pos[1], 30)
|
# self.circle = pygame.geometry.Circle(self.player_pos[0], self.player_pos[1], 30)
|
||||||
|
|
||||||
|
|
@ -79,16 +77,19 @@ class Game:
|
||||||
# self.flashlight = pygame.draw.circle(self.display, (255, 255, 255, 0), self.flashlight_rect[0], self.flashlight_rect[1])
|
# self.flashlight = pygame.draw.circle(self.display, (255, 255, 255, 0), self.flashlight_rect[0], self.flashlight_rect[1])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
"""RAYCASTING"""
|
"""RAYCASTING"""
|
||||||
ray_end = pygame.Vector2()
|
ray_end = pygame.Vector2()
|
||||||
light_points = []
|
light_points = []
|
||||||
vector = pygame.Vector2(75, 0)
|
vector = pygame.Vector2(self.light_radius, 0)
|
||||||
|
|
||||||
# Lichtstrahlen berechnen
|
# Lichtstrahlen berechnen
|
||||||
for i in range(360):
|
for i in range(360):
|
||||||
|
|
||||||
rotated = vector.rotate(i)
|
rotated_vector = vector.rotate(i)
|
||||||
ray_end = self.player_pos + rotated
|
ray_end = self.player_pos + rotated_vector
|
||||||
|
|
||||||
|
|
||||||
# Überschneidung mit den Wänden prüfen
|
# Überschneidung mit den Wänden prüfen
|
||||||
|
|
@ -108,7 +109,7 @@ class Game:
|
||||||
|
|
||||||
pygame.draw.polygon(
|
pygame.draw.polygon(
|
||||||
self.display,
|
self.display,
|
||||||
"yellow",
|
(243, 255, 74),
|
||||||
light_points
|
light_points
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -121,36 +122,44 @@ class Game:
|
||||||
for wall in self.walls:
|
for wall in self.walls:
|
||||||
pygame.draw.rect(self.display, "grey", wall)
|
pygame.draw.rect(self.display, "grey", wall)
|
||||||
|
|
||||||
|
player_pos_before = self.player_pos
|
||||||
|
|
||||||
|
velocity = self.speed * self.dt
|
||||||
|
|
||||||
|
|
||||||
# Smooth Movement
|
# Smooth Movement
|
||||||
keys = pygame.key.get_pressed()
|
keys = pygame.key.get_pressed()
|
||||||
|
|
||||||
if keys[pygame.K_w]:
|
if keys[pygame.K_w]:
|
||||||
|
self.player_pos.y -= velocity
|
||||||
self.player_pos.y -= self.speed * self.dt
|
|
||||||
|
|
||||||
if keys[pygame.K_s]:
|
if keys[pygame.K_s]:
|
||||||
self.player_pos.y += self.speed * self.dt
|
self.player_pos.y += velocity
|
||||||
|
|
||||||
if keys[pygame.K_a]:
|
if keys[pygame.K_a]:
|
||||||
self.player_pos.x -= self.speed * self.dt
|
self.player_pos.x -= velocity
|
||||||
|
|
||||||
if keys[pygame.K_d]:
|
if keys[pygame.K_d]:
|
||||||
self.player_pos.x += self.speed * self.dt
|
self.player_pos.x += velocity
|
||||||
|
|
||||||
|
|
||||||
if self.player_pos[1] < self.player_height/2:
|
self.player_pos[1] = max(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] = min(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] = max(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] = min(self.player_pos[0], SCREEN_WIDTH/2-self.player_width/2)
|
||||||
self.player_pos[0] = SCREEN_WIDTH/2-self.player_width/2
|
|
||||||
|
|
||||||
|
for wall in self.walls:
|
||||||
|
if self.collide_objects(wall, self.player_rect):
|
||||||
|
self.player_pos = player_pos_before
|
||||||
|
|
||||||
|
if self.player_pos == player_pos_before:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
print("Ungleich")
|
||||||
|
# print(f"Player Pos: {self.player_pos}, Player Pos Before: {player_pos_before}")
|
||||||
|
|
||||||
if self.rect_circle_collision(self.item_rect, self.flashlight_rect):
|
if self.rect_circle_collision(self.item_rect, self.flashlight_rect):
|
||||||
self.color_item = (0, 0, 255)
|
self.color_item = (0, 0, 255)
|
||||||
|
|
@ -183,16 +192,10 @@ class Game:
|
||||||
|
|
||||||
return distance <= circle_r
|
return distance <= circle_r
|
||||||
|
|
||||||
# def calculate_flashlight_radiate(self, degrees: int, start_pos: tuple[int, int] | list[int, int] | pygame.Vector2, length: int) -> tuple[int, int]:
|
def collide_objects(self, rect1: pygame.Rect, rect2: pygame.Rect) -> bool:
|
||||||
# angle_deg = degrees # Angle in degrees
|
if rect1.colliderect(rect2):
|
||||||
|
return True
|
||||||
|
|
||||||
# # 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
|
|
||||||
|
|
||||||
game = Game()
|
game = Game()
|
||||||
game.run()
|
game.run()
|
||||||
|
|
|
||||||
120
testen/anderer_TS.py
Normal file
120
testen/anderer_TS.py
Normal file
|
|
@ -0,0 +1,120 @@
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
import pygame
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Collisions:
|
||||||
|
top: bool = False
|
||||||
|
right: bool = False
|
||||||
|
bottom: bool = False
|
||||||
|
left: bool = False
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# 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)
|
||||||
|
|
||||||
|
speed = 300
|
||||||
|
|
||||||
|
movement = [False, False, False, False]
|
||||||
|
|
||||||
|
velocity = [0, 0]
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
wall_rect = pygame.Rect(640, 260, 600, 30)
|
||||||
|
player_rect = pygame.Rect(player_pos.x, player_pos.y, 40, 40)
|
||||||
|
|
||||||
|
# fill the screen with a color to wipe away anything from last frame
|
||||||
|
screen.fill("purple")
|
||||||
|
pygame.draw.rect(screen, "blue", player_rect)
|
||||||
|
pygame.draw.rect(screen, "red", wall_rect)
|
||||||
|
|
||||||
|
velocity = speed * dt
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
keys = pygame.key.get_pressed()
|
||||||
|
|
||||||
|
if keys[pygame.K_w]:
|
||||||
|
velocity -= speed * dt
|
||||||
|
movement[0] = True
|
||||||
|
|
||||||
|
if keys[pygame.K_s]:
|
||||||
|
velocity += speed * dt
|
||||||
|
movement[2] = True
|
||||||
|
|
||||||
|
if keys[pygame.K_a]:
|
||||||
|
velocity -= speed * dt
|
||||||
|
movement[3] = True
|
||||||
|
|
||||||
|
if keys[pygame.K_d]:
|
||||||
|
velocity += speed * dt
|
||||||
|
movement[1] = True
|
||||||
|
|
||||||
|
collisions = Collisions()
|
||||||
|
|
||||||
|
frame_movement: tuple[float, float] = (
|
||||||
|
(movement[0] + velocity[0]),
|
||||||
|
movement[1] + velocity[1],
|
||||||
|
)
|
||||||
|
|
||||||
|
entity_rect = player_rect
|
||||||
|
if entity_rect.colliderect(wall_rect):
|
||||||
|
if frame_movement[0] > 0:
|
||||||
|
entity_rect.right = wall_rect.left
|
||||||
|
collisions.right = True
|
||||||
|
|
||||||
|
if frame_movement[0] < 0:
|
||||||
|
entity_rect.left = wall_rect.right
|
||||||
|
collisions.left = True
|
||||||
|
|
||||||
|
player_pos.x = entity_rect.x
|
||||||
|
|
||||||
|
player_pos[1] += frame_movement[1]
|
||||||
|
|
||||||
|
entity_rect = player_rect
|
||||||
|
|
||||||
|
|
||||||
|
if entity_rect.colliderect(wall_rect):
|
||||||
|
if frame_movement[1] > 0:
|
||||||
|
entity_rect.bottom = wall_rect.top
|
||||||
|
collisions.bottom = True
|
||||||
|
|
||||||
|
if frame_movement[1] < 0:
|
||||||
|
entity_rect.top = wall_rect.bottom
|
||||||
|
collisions.top = True
|
||||||
|
|
||||||
|
player_pos[1] = entity_rect.y
|
||||||
|
|
||||||
|
movement = [False, False, False, False] # NORDEN OSTEN SÜDEN WESTEN
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# 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()
|
||||||
34
testen/mini_test.py
Normal file
34
testen/mini_test.py
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
# 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
|
||||||
|
jays_vector = pygame.Vector2(100, 100)
|
||||||
|
jays_anderer_vector = pygame.Vector2(600, 300)
|
||||||
|
pygame.draw.line(screen, (0, 0, 0), (0, 0), jays_vector)
|
||||||
|
pygame.draw.line(screen, (0, 0, 0), (0, 0), jays_anderer_vector)
|
||||||
|
|
||||||
|
pygame.draw.line(screen, (255, 255, 255), jays_vector, jays_anderer_vector)
|
||||||
|
|
||||||
|
|
||||||
|
# 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