Es funktioniert, aber das Spiel laggt seeeeeehr (Jay probier es mal selber aus. In Zoom sieht man es wahrscheinlich nicht)

This commit is contained in:
Benjamin 2026-10-23 16:14:13 +02:00
parent 01e925902a
commit d7f2b54f3b
5 changed files with 55 additions and 31 deletions

33
game.py
View file

@ -5,6 +5,7 @@ from scripts.flashlight import Flashlight
from scripts.level import Level
from scripts.player import Player
from scripts.utils import load_image
from scripts.wall import Wall
class Game:
@ -40,12 +41,13 @@ class Game:
game=self,
items=[],
walls=[
pygame.Rect(0, 0, SCREEN_WIDTH, TILE_SIZE),
pygame.Rect(0, TILE_SIZE, TILE_SIZE, SCREEN_HEIGHT),
pygame.Rect(
TILE_SIZE, SCREEN_HEIGHT - TILE_SIZE, SCREEN_WIDTH, TILE_SIZE
Wall(self, 0, 0, SCREEN_WIDTH, TILE_SIZE),
Wall(self, 0, TILE_SIZE, TILE_SIZE, SCREEN_HEIGHT),
Wall(
self, TILE_SIZE, SCREEN_HEIGHT - TILE_SIZE, SCREEN_WIDTH, TILE_SIZE
),
pygame.Rect(SCREEN_WIDTH - TILE_SIZE, 0, SCREEN_WIDTH, SCREEN_HEIGHT),
Wall(self, SCREEN_WIDTH - TILE_SIZE, 0, SCREEN_WIDTH, SCREEN_HEIGHT),
Wall(self, 500, 500, 128, 128)
],
)
@ -118,28 +120,11 @@ class Game:
self.render()
class Wall:
def __init__(self, game, x: float, y: float, width: float, height: float):
self.game = game
self.x = x
self.y = y
self.width = width
self.height = height
def update(self):
pass
def render(self, surface):
pass
game = Game()
game.run()
"""
Ein paar Assets schonmal zurechtschneiden.
Fertigen Raum machen.
Immer alles strukturieren
"""

View file

@ -23,7 +23,7 @@ class Flashlight:
for angle in range(360):
ray_end = origin + base_ray.rotate(angle)
for wall in self.game.walls:
intersection = wall.clipline(origin, ray_end)
intersection = wall.get_rect().clipline(origin, ray_end)
if intersection:
ray_end = pygame.Vector2(intersection[0])
points.append(ray_end)
@ -53,7 +53,7 @@ class Flashlight:
# Überschneidung mit den Wänden prüfen
for wall in self.game.walls:
# Überschneidung
intersection = wall.clipline(origin, ray_end)
intersection = wall.get_rect().clipline(origin, ray_end)
if intersection:
ray_end = pygame.Vector2(intersection[0])
points.append(ray_end)

View file

@ -2,10 +2,11 @@ import pygame
from scripts.constants import *
from scripts.item import Item
from scripts.wall import Wall
class Level:
def __init__(self, game, items: list[Item], walls: list[pygame.Rect]):
def __init__(self, game, items: list[Item], walls: list[Wall]):
self.game = game
# Items
@ -24,7 +25,5 @@ class Level:
# Wände
for wall in self.walls:
surface.blit(self.game.assets["wall"], wall)
wall.render(surface)
def draw_walls(self, surface: pygame.Surface):
pass

View file

@ -34,6 +34,7 @@ class Player:
self.rect.centerx = round(self.pos.x)
for wall in walls:
wall = wall.get_rect()
if self.rect.colliderect(wall):
if movement.x > 0:
self.rect.right = wall.left
@ -50,6 +51,7 @@ class Player:
self.rect.centery = round(self.pos.y)
for wall in walls:
wall = wall.get_rect()
if self.rect.colliderect(wall):
if movement.y > 0:
self.rect.bottom = wall.top

View file

@ -0,0 +1,38 @@
import pygame
from scripts.constants import *
class Wall:
def __init__(self, game, x: float, y: float, width: float, height: float):
self.game = game
self.x = x
self.y = y
self.width = width
self.height = height
self.rect = pygame.Rect(x, y, width, height)
for i in range(self.width):
if i % 64 == 0:
end_ergebnis_width = i
for i in range(self.height):
if i % 64 == 0:
end_ergebnis_height = i
self.length: dict = {
"width": end_ergebnis_width+TILE_SIZE,
"height": end_ergebnis_height+TILE_SIZE,
}
def update(self):
pass
def render(self, surface: pygame.Surface):
for x in range(self.length["width"]):
for y in range(self.length["height"]):
if y % TILE_SIZE == 0 and x % TILE_SIZE == 0:
surface.blit(self.game.assets["wall"], pygame.Rect(self.x+x, self.y+y, self.width, self.height))
def get_rect(self) -> pygame.Rect:
return self.rect