Compare commits
10 commits
3ea58cff33
...
dbe1ca4a2d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dbe1ca4a2d | ||
|
|
926092fe11 | ||
|
|
bb1a796c1c | ||
|
|
bfe8427392 | ||
|
|
2be51cb32e | ||
|
|
6e8a94f37f | ||
|
|
1c457f6c9d | ||
|
|
820ba42213 | ||
|
|
2b31c1de54 | ||
|
|
85ec7c61e7 |
1 changed files with 82 additions and 76 deletions
158
game.py
158
game.py
|
|
@ -1,11 +1,31 @@
|
||||||
import pygame
|
import pygame
|
||||||
|
from pygame.math import Vector2
|
||||||
|
from pygame.rect import Rect
|
||||||
|
from pygame.typing import SequenceLike
|
||||||
|
|
||||||
SCREEN_WIDTH = 1280
|
SCREEN_WIDTH = 1280
|
||||||
SCREEN_HEIGHT = 720
|
SCREEN_HEIGHT = 720
|
||||||
PLAYER_SIZE = 20
|
SCREEN_CENTER = (SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)
|
||||||
LIGHT_RADIUS = 75
|
PLAYER_SIZE = 40
|
||||||
|
LIGHT_RADIUS = 150
|
||||||
|
|
||||||
|
|
||||||
|
class Item:
|
||||||
|
def __init__(self, x: float, y: float, width: float, height: float, color):
|
||||||
|
self.x: float = x
|
||||||
|
self.y: float = y
|
||||||
|
self.vector = pygame.Vector2(self.x, self.y)
|
||||||
|
self.width: float = width
|
||||||
|
self.height: float = height
|
||||||
|
self.color: SequenceLike[int] | str | int = color
|
||||||
|
|
||||||
|
@property
|
||||||
|
def rect(self)-> pygame.Rect:
|
||||||
|
return pygame.Rect(self.x, self.y, self.width, self.height)
|
||||||
|
|
||||||
|
def render(self, surface) :
|
||||||
|
pygame.draw.rect(surface, self.color, self.rect)
|
||||||
|
|
||||||
class Game:
|
class Game:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
# Pygame setup
|
# Pygame setup
|
||||||
|
|
@ -14,33 +34,44 @@ class Game:
|
||||||
self.clock = pygame.Clock()
|
self.clock = pygame.Clock()
|
||||||
# Surfaces
|
# Surfaces
|
||||||
self.screen: pygame.Surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
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))
|
|
||||||
# Pygame-Variablen
|
# Pygame-Variablen
|
||||||
self.running: bool = True
|
self.running: bool = True
|
||||||
self.dt: int = 0
|
self.dt: int = 0
|
||||||
# Spieler
|
# Spieler
|
||||||
self.player_pos = pygame.Vector2(self.display.get_width() / 2, self.display.get_height() / 2)
|
self.player_pos = pygame.Vector2(self.screen.get_width() / 2, self.screen.get_height() / 2)
|
||||||
self.player = Player(self, self.player_pos, width=PLAYER_SIZE, height=PLAYER_SIZE)
|
self.player = Player(self.player_pos, width=PLAYER_SIZE, height=PLAYER_SIZE)
|
||||||
# Items
|
# Items
|
||||||
self.item_rect = pygame.Rect(70, 150, 10, 10)
|
self.test_item = Item(140 ,300, 20, 20, "blue")
|
||||||
self.flashlight_rect = pygame.Rect(0, 0, 0, 0)
|
self.test_item_mask = pygame.mask.Mask(self.test_item.rect.size, fill=True)
|
||||||
self.color_item = (0, 0, 0)
|
|
||||||
self.light_points: list[pygame.Vector2] = []
|
|
||||||
# Level
|
# Level
|
||||||
self.walls = [pygame.Rect(100, 100, 20, 50), pygame.Rect(300, 300, 200, 20), pygame.Rect(550, 80, 20, 200)]
|
self.walls: list[Rect] = [pygame.Rect(100*2, 100*2, 20*2, 50*2), pygame.Rect(300*2, 300*2, 200*2, 20*2), pygame.Rect(550*2, 80*2, 20*2, 200*2)]
|
||||||
|
# Licht
|
||||||
|
self.light_points: list[pygame.Vector2]
|
||||||
|
self.light_surface: pygame.Surface = pygame.Surface((SCREEN_WIDTH,SCREEN_HEIGHT), pygame.SRCALPHA)
|
||||||
|
|
||||||
def run(self) -> None:
|
|
||||||
while self.running:
|
|
||||||
self.dt: float = self.clock.tick(60) / 1000.0
|
|
||||||
|
|
||||||
self.update()
|
def create_light_points(self)-> list[pygame.Vector2]:
|
||||||
self.calculate_collsions()
|
points: list[pygame.Vector2] = []
|
||||||
self.render()
|
origin: Vector2 = self.player.pos
|
||||||
|
base_ray = pygame.Vector2(LIGHT_RADIUS, 0)
|
||||||
|
|
||||||
|
# Lichtstrahlen berechnen
|
||||||
|
for angle in range(360):
|
||||||
|
ray_end = origin + base_ray.rotate(angle)
|
||||||
|
|
||||||
|
# Überschneidung mit den Wänden prüfen
|
||||||
|
for wall in self.walls:
|
||||||
|
# Überschneidung
|
||||||
|
intersection = wall.clipline(origin, ray_end)
|
||||||
|
if intersection:
|
||||||
|
ray_end = pygame.Vector2(intersection[0])
|
||||||
|
points.append(ray_end)
|
||||||
|
|
||||||
|
return points
|
||||||
|
|
||||||
self.screen.blit(pygame.transform.scale(self.display, self.screen.get_size()))
|
|
||||||
pygame.display.update()
|
|
||||||
|
|
||||||
def update(self) -> None:
|
def update(self) -> None:
|
||||||
|
# Ereignisse
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
self.running = False
|
self.running = False
|
||||||
|
|
@ -51,76 +82,51 @@ class Game:
|
||||||
self.player.speed = 300
|
self.player.speed = 300
|
||||||
if event.type == pygame.KEYUP and event.key == pygame.K_LSHIFT:
|
if event.type == pygame.KEYUP and event.key == pygame.K_LSHIFT:
|
||||||
self.player.speed = 150
|
self.player.speed = 150
|
||||||
|
# Spielzustände aktualisieren
|
||||||
self.player.update(walls=self.walls, dt=self.dt)
|
self.player.update(walls=self.walls, dt=self.dt)
|
||||||
|
self.light_points: list[Vector2] = self.create_light_points()
|
||||||
|
|
||||||
|
|
||||||
def render(self) -> None:
|
def render(self) -> None:
|
||||||
|
# Hintergrund
|
||||||
|
self.screen.fill((0, 0, 0, 0))
|
||||||
|
|
||||||
self.display.fill((0, 0, 0, 0))
|
# Licht
|
||||||
|
self.light_surface.fill((0, 0, 0, 0))
|
||||||
"""RAYCASTING"""
|
pygame.draw.polygon(self.light_surface, (243, 255, 74, 255), self.create_light_points())
|
||||||
ray_end = pygame.Vector2()
|
self.screen.blit(self.light_surface, (0, 0))
|
||||||
self.light_points = []
|
light_mask = pygame.mask.from_surface(self.light_surface)
|
||||||
vector = pygame.Vector2(LIGHT_RADIUS, 0)
|
if light_mask.overlap(self.test_item_mask, self.test_item.rect.topleft) is not None:
|
||||||
|
# Item zeichnen, wenn Masken sich überlappen
|
||||||
# Lichtstrahlen berechnen
|
self.test_item.render(self.screen)
|
||||||
for i in range(360):
|
|
||||||
rotated_vector = vector.rotate(i)
|
|
||||||
ray_end = self.player_pos + rotated_vector
|
|
||||||
|
|
||||||
# Überschneidung mit den Wänden prüfen
|
|
||||||
for wall in self.walls:
|
|
||||||
überschneidung = wall.clipline(self.player_pos, ray_end)
|
|
||||||
if überschneidung:
|
|
||||||
schnittpunkt = pygame.Vector2(überschneidung[0])
|
|
||||||
ray_end = schnittpunkt
|
|
||||||
self.light_points.append(ray_end)
|
|
||||||
|
|
||||||
self.flashlight_rect = pygame.draw.polygon(self.display, (243, 255, 74), self.light_points)
|
|
||||||
pygame.draw.rect(self.display, (255, 0, 0), self.flashlight_rect, 1)
|
|
||||||
self.player.render(surface=self.display)
|
|
||||||
pygame.draw.rect(self.display, self.color_item, self.item_rect)
|
|
||||||
|
|
||||||
|
# Wände
|
||||||
for wall in self.walls:
|
for wall in self.walls:
|
||||||
pygame.draw.rect(self.display, "grey", wall)
|
pygame.draw.rect(self.screen, "grey", wall)
|
||||||
|
|
||||||
|
# Spieler
|
||||||
|
self.player.render(self.screen)
|
||||||
|
|
||||||
def calculate_collsions(self) -> None:
|
# Surfaces aktualisieren
|
||||||
|
pygame.display.update()
|
||||||
|
|
||||||
for light_ray in self.light_points:
|
|
||||||
nearest_x = self.player_pos.x
|
|
||||||
if self.player_pos.x < self.item_rect.left:
|
|
||||||
nearest_x = self.item_rect.left
|
|
||||||
elif self.player_pos.x > self.item_rect.right:
|
|
||||||
nearest_x = self.item_rect.right
|
|
||||||
|
|
||||||
nearest_y = self.player_pos.y
|
def run(self) -> None:
|
||||||
if self.player_pos.y < self.item_rect.top:
|
while self.running:
|
||||||
nearest_y = self.item_rect.top
|
self.dt: float = self.clock.tick(60) / 1000.0
|
||||||
elif self.player_pos.y > self.item_rect.bottom:
|
self.update()
|
||||||
nearest_y = self.item_rect.bottom
|
self.render()
|
||||||
|
|
||||||
dx = self.player_pos.x - nearest_x
|
|
||||||
dy = self.player_pos.y - nearest_y
|
|
||||||
distance_squared = dx * dx + dy * dy
|
|
||||||
|
|
||||||
if distance_squared < light_ray.length_squared():
|
|
||||||
self.color_item = (0, 0, 255)
|
|
||||||
else:
|
|
||||||
self.color_item = (0, 0, 0)
|
|
||||||
|
|
||||||
|
|
||||||
class Player:
|
class Player:
|
||||||
def __init__(self, game, pos: pygame.Vector2, width, height):
|
def __init__(self, pos: pygame.Vector2, width, height):
|
||||||
self.game = game
|
self.pos: Vector2 = pos
|
||||||
self.pos = pos
|
self.width: float = width
|
||||||
self.width = width
|
self.height: float = height
|
||||||
self.height = height
|
|
||||||
self.speed = 150
|
self.speed = 150
|
||||||
self.rect = pygame.Rect(self.pos[0] - self.width / 2, self.pos[1] - self.height / 2, width, height)
|
self.rect = pygame.Rect(self.pos[0] - self.width / 2, self.pos[1] - self.height / 2, width, height)
|
||||||
|
|
||||||
def update(self, walls, dt):
|
def update(self, walls, dt):
|
||||||
|
|
||||||
# Smooth Movement
|
|
||||||
keys = pygame.key.get_pressed()
|
keys = pygame.key.get_pressed()
|
||||||
direction = pygame.Vector2(keys[pygame.K_d] - keys[pygame.K_a], keys[pygame.K_s] - keys[pygame.K_w])
|
direction = pygame.Vector2(keys[pygame.K_d] - keys[pygame.K_a], keys[pygame.K_s] - keys[pygame.K_w])
|
||||||
|
|
||||||
|
|
@ -133,8 +139,8 @@ class Player:
|
||||||
|
|
||||||
if self.pos.x - self.width / 2 < 0:
|
if self.pos.x - self.width / 2 < 0:
|
||||||
self.pos.x = self.width / 2
|
self.pos.x = self.width / 2
|
||||||
if self.pos.x + self.width / 2 > SCREEN_WIDTH / 2:
|
if self.pos.x + self.width / 2 > SCREEN_WIDTH:
|
||||||
self.pos.x = SCREEN_WIDTH / 2 - self.width / 2
|
self.pos.x = SCREEN_WIDTH - self.width / 2
|
||||||
self.rect.centerx = round(self.pos.x)
|
self.rect.centerx = round(self.pos.x)
|
||||||
|
|
||||||
for wall in walls:
|
for wall in walls:
|
||||||
|
|
@ -149,8 +155,8 @@ class Player:
|
||||||
|
|
||||||
if self.pos.y - self.height / 2 < 0:
|
if self.pos.y - self.height / 2 < 0:
|
||||||
self.pos.y = self.height / 2
|
self.pos.y = self.height / 2
|
||||||
if self.pos.y + self.height / 2 > SCREEN_HEIGHT / 2:
|
if self.pos.y + self.height / 2 > SCREEN_HEIGHT:
|
||||||
self.pos.y = SCREEN_HEIGHT / 2 - self.height / 2
|
self.pos.y = SCREEN_HEIGHT - self.height / 2
|
||||||
self.rect.centery = round(self.pos.y)
|
self.rect.centery = round(self.pos.y)
|
||||||
|
|
||||||
for wall in walls:
|
for wall in walls:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue