Clean up
This commit is contained in:
parent
926092fe11
commit
dbe1ca4a2d
1 changed files with 22 additions and 17 deletions
39
game.py
39
game.py
|
|
@ -1,8 +1,11 @@
|
||||||
import pygame
|
import pygame
|
||||||
from pygame.math import Vector2
|
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
|
||||||
|
SCREEN_CENTER = (SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)
|
||||||
PLAYER_SIZE = 40
|
PLAYER_SIZE = 40
|
||||||
LIGHT_RADIUS = 150
|
LIGHT_RADIUS = 150
|
||||||
|
|
||||||
|
|
@ -14,7 +17,7 @@ class Item:
|
||||||
self.vector = pygame.Vector2(self.x, self.y)
|
self.vector = pygame.Vector2(self.x, self.y)
|
||||||
self.width: float = width
|
self.width: float = width
|
||||||
self.height: float = height
|
self.height: float = height
|
||||||
self.color = color
|
self.color: SequenceLike[int] | str | int = color
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def rect(self)-> pygame.Rect:
|
def rect(self)-> pygame.Rect:
|
||||||
|
|
@ -36,20 +39,20 @@ class Game:
|
||||||
self.dt: int = 0
|
self.dt: int = 0
|
||||||
# Spieler
|
# Spieler
|
||||||
self.player_pos = pygame.Vector2(self.screen.get_width() / 2, self.screen.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.test_item = Item(140 ,300, 20, 20, "blue")
|
self.test_item = Item(140 ,300, 20, 20, "blue")
|
||||||
self.test_item_mask = pygame.mask.Mask(self.test_item.rect.size, fill=True)
|
self.test_item_mask = pygame.mask.Mask(self.test_item.rect.size, fill=True)
|
||||||
# Level
|
# Level
|
||||||
self.walls = [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)]
|
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
|
# Licht
|
||||||
self.light_points: list[pygame.Vector2]
|
self.light_points: list[pygame.Vector2]
|
||||||
self.light_surface: pygame.Surface = pygame.Surface((SCREEN_WIDTH,SCREEN_HEIGHT), pygame.SRCALPHA)
|
self.light_surface: pygame.Surface = pygame.Surface((SCREEN_WIDTH,SCREEN_HEIGHT), pygame.SRCALPHA)
|
||||||
|
|
||||||
|
|
||||||
def create_light_points(self)-> list[pygame.Vector2]:
|
def create_light_points(self)-> list[pygame.Vector2]:
|
||||||
light_points: list[pygame.Vector2] = []
|
points: list[pygame.Vector2] = []
|
||||||
origin = self.player.pos
|
origin: Vector2 = self.player.pos
|
||||||
base_ray = pygame.Vector2(LIGHT_RADIUS, 0)
|
base_ray = pygame.Vector2(LIGHT_RADIUS, 0)
|
||||||
|
|
||||||
# Lichtstrahlen berechnen
|
# Lichtstrahlen berechnen
|
||||||
|
|
@ -62,12 +65,13 @@ class Game:
|
||||||
intersection = wall.clipline(origin, ray_end)
|
intersection = wall.clipline(origin, ray_end)
|
||||||
if intersection:
|
if intersection:
|
||||||
ray_end = pygame.Vector2(intersection[0])
|
ray_end = pygame.Vector2(intersection[0])
|
||||||
light_points.append(ray_end)
|
points.append(ray_end)
|
||||||
|
|
||||||
return light_points
|
return points
|
||||||
|
|
||||||
|
|
||||||
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
|
||||||
|
|
@ -78,30 +82,32 @@ 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()
|
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.screen.fill((0, 0, 0, 0))
|
||||||
|
|
||||||
# Licht
|
# Licht
|
||||||
self.light_surface.fill((0, 0, 0, 0))
|
self.light_surface.fill((0, 0, 0, 0))
|
||||||
pygame.draw.polygon(self.light_surface, (243, 255, 74, 255), self.create_light_points())
|
pygame.draw.polygon(self.light_surface, (243, 255, 74, 255), self.create_light_points())
|
||||||
|
|
||||||
self.screen.blit(self.light_surface, (0, 0))
|
self.screen.blit(self.light_surface, (0, 0))
|
||||||
|
|
||||||
light_mask = pygame.mask.from_surface(self.light_surface)
|
light_mask = pygame.mask.from_surface(self.light_surface)
|
||||||
if light_mask.overlap(self.test_item_mask, self.test_item.rect.topleft) is not None:
|
if light_mask.overlap(self.test_item_mask, self.test_item.rect.topleft) is not None:
|
||||||
# Item
|
# Item zeichnen, wenn Masken sich überlappen
|
||||||
self.test_item.render(self.screen)
|
self.test_item.render(self.screen)
|
||||||
|
|
||||||
|
# Wände
|
||||||
for wall in self.walls:
|
for wall in self.walls:
|
||||||
pygame.draw.rect(self.screen, "grey", wall)
|
pygame.draw.rect(self.screen, "grey", wall)
|
||||||
|
|
||||||
# Player
|
# Spieler
|
||||||
self.player.render(self.screen)
|
self.player.render(self.screen)
|
||||||
|
|
||||||
|
# Surfaces aktualisieren
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -113,11 +119,10 @@ class Game:
|
||||||
|
|
||||||
|
|
||||||
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)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue