code-check-jay #1
1 changed files with 22 additions and 17 deletions
39
game.py
39
game.py
|
|
@ -1,8 +1,11 @@
|
|||
import pygame
|
||||
from pygame.math import Vector2
|
||||
from pygame.rect import Rect
|
||||
from pygame.typing import SequenceLike
|
||||
|
||||
SCREEN_WIDTH = 1280
|
||||
SCREEN_HEIGHT = 720
|
||||
SCREEN_CENTER = (SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)
|
||||
PLAYER_SIZE = 40
|
||||
LIGHT_RADIUS = 150
|
||||
|
||||
|
|
@ -14,7 +17,7 @@ class Item:
|
|||
self.vector = pygame.Vector2(self.x, self.y)
|
||||
self.width: float = width
|
||||
self.height: float = height
|
||||
self.color = color
|
||||
self.color: SequenceLike[int] | str | int = color
|
||||
|
||||
@property
|
||||
def rect(self)-> pygame.Rect:
|
||||
|
|
@ -36,20 +39,20 @@ class Game:
|
|||
self.dt: int = 0
|
||||
# Spieler
|
||||
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
|
||||
self.test_item = Item(140 ,300, 20, 20, "blue")
|
||||
self.test_item_mask = pygame.mask.Mask(self.test_item.rect.size, fill=True)
|
||||
# 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
|
||||
self.light_points: list[pygame.Vector2]
|
||||
self.light_surface: pygame.Surface = pygame.Surface((SCREEN_WIDTH,SCREEN_HEIGHT), pygame.SRCALPHA)
|
||||
|
||||
|
||||
def create_light_points(self)-> list[pygame.Vector2]:
|
||||
light_points: list[pygame.Vector2] = []
|
||||
origin = self.player.pos
|
||||
points: list[pygame.Vector2] = []
|
||||
origin: Vector2 = self.player.pos
|
||||
base_ray = pygame.Vector2(LIGHT_RADIUS, 0)
|
||||
|
||||
# Lichtstrahlen berechnen
|
||||
|
|
@ -62,12 +65,13 @@ class Game:
|
|||
intersection = wall.clipline(origin, ray_end)
|
||||
if intersection:
|
||||
ray_end = pygame.Vector2(intersection[0])
|
||||
light_points.append(ray_end)
|
||||
points.append(ray_end)
|
||||
|
||||
return light_points
|
||||
return points
|
||||
|
||||
|
||||
def update(self) -> None:
|
||||
# Ereignisse
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
self.running = False
|
||||
|
|
@ -78,30 +82,32 @@ class Game:
|
|||
self.player.speed = 300
|
||||
if event.type == pygame.KEYUP and event.key == pygame.K_LSHIFT:
|
||||
self.player.speed = 150
|
||||
|
||||
# Spielzustände aktualisieren
|
||||
self.player.update(walls=self.walls, dt=self.dt)
|
||||
self.light_points: list[Vector2] = self.create_light_points()
|
||||
|
||||
|
||||
def render(self) -> None:
|
||||
# Hintergrund
|
||||
self.screen.fill((0, 0, 0, 0))
|
||||
|
||||
# Licht
|
||||
self.light_surface.fill((0, 0, 0, 0))
|
||||
pygame.draw.polygon(self.light_surface, (243, 255, 74, 255), self.create_light_points())
|
||||
|
||||
self.screen.blit(self.light_surface, (0, 0))
|
||||
|
||||
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:
|
||||
# Item
|
||||
# Item zeichnen, wenn Masken sich überlappen
|
||||
self.test_item.render(self.screen)
|
||||
|
||||
# Wände
|
||||
for wall in self.walls:
|
||||
pygame.draw.rect(self.screen, "grey", wall)
|
||||
|
||||
# Player
|
||||
# Spieler
|
||||
self.player.render(self.screen)
|
||||
|
||||
# Surfaces aktualisieren
|
||||
pygame.display.update()
|
||||
|
||||
|
||||
|
|
@ -113,11 +119,10 @@ class Game:
|
|||
|
||||
|
||||
class Player:
|
||||
def __init__(self, game, pos: pygame.Vector2, width, height):
|
||||
self.game = game
|
||||
self.pos = pos
|
||||
self.width = width
|
||||
self.height = height
|
||||
def __init__(self, pos: pygame.Vector2, width, height):
|
||||
self.pos: Vector2 = pos
|
||||
self.width: float = width
|
||||
self.height: float = height
|
||||
self.speed = 150
|
||||
self.rect = pygame.Rect(self.pos[0] - self.width / 2, self.pos[1] - self.height / 2, width, height)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue