168 lines
5.3 KiB
Python
168 lines
5.3 KiB
Python
import pygame
|
|
from pygame.math import Vector2
|
|
|
|
SCREEN_WIDTH = 1280
|
|
SCREEN_HEIGHT = 720
|
|
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 = 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:
|
|
def __init__(self):
|
|
# Pygame setup
|
|
pygame.init()
|
|
pygame.display.set_caption("School Game")
|
|
self.clock = pygame.Clock()
|
|
# Surfaces
|
|
self.screen: pygame.Surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
|
# Pygame-Variablen
|
|
self.running: bool = True
|
|
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)
|
|
# Items
|
|
self.test_item = Item(140 ,300, 20, 20, "blue")
|
|
# 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)]
|
|
# Licht
|
|
self.light_points: list[pygame.Vector2]
|
|
|
|
|
|
def calculate_collsions(self) -> None:
|
|
pass
|
|
|
|
|
|
def create_light_points(self)-> list[pygame.Vector2]:
|
|
light_points: list[pygame.Vector2] = []
|
|
vector = pygame.Vector2(LIGHT_RADIUS, 0)
|
|
|
|
# Lichtstrahlen berechnen
|
|
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
|
|
light_points.append(ray_end)
|
|
return light_points
|
|
|
|
|
|
def update(self) -> None:
|
|
for event in pygame.event.get():
|
|
if event.type == pygame.QUIT:
|
|
self.running = False
|
|
if event.type == pygame.KEYDOWN:
|
|
if event.key == pygame.K_ESCAPE:
|
|
self.running = False
|
|
if event.key == pygame.K_LSHIFT:
|
|
self.player.speed = 300
|
|
if event.type == pygame.KEYUP and event.key == pygame.K_LSHIFT:
|
|
self.player.speed = 150
|
|
|
|
self.player.update(walls=self.walls, dt=self.dt)
|
|
self.light_points: list[Vector2] = self.create_light_points()
|
|
|
|
|
|
def render(self) -> None:
|
|
self.screen.fill((0, 0, 0, 0))
|
|
|
|
# Licht
|
|
pygame.draw.polygon(self.screen, (243, 255, 74), self.light_points)
|
|
# Item
|
|
pygame.draw.rect(self.screen, "blue", self.test_item)
|
|
# Player
|
|
self.player.render(surface=self.screen)
|
|
|
|
for wall in self.walls:
|
|
pygame.draw.rect(self.screen, "grey", wall)
|
|
|
|
self.test_item.render(self.screen)
|
|
|
|
pygame.display.update()
|
|
|
|
|
|
def run(self) -> None:
|
|
while self.running:
|
|
self.dt: float = self.clock.tick(60) / 1000.0
|
|
self.update()
|
|
self.calculate_collsions()
|
|
self.render()
|
|
|
|
|
|
class Player:
|
|
def __init__(self, game, pos: pygame.Vector2, width, height):
|
|
self.game = game
|
|
self.pos = pos
|
|
self.width = width
|
|
self.height = height
|
|
self.speed = 150
|
|
self.rect = pygame.Rect(self.pos[0] - self.width / 2, self.pos[1] - self.height / 2, width, height)
|
|
|
|
def update(self, walls, dt):
|
|
keys = pygame.key.get_pressed()
|
|
direction = pygame.Vector2(keys[pygame.K_d] - keys[pygame.K_a], keys[pygame.K_s] - keys[pygame.K_w])
|
|
|
|
if direction.length_squared() > 0:
|
|
direction = direction.normalize()
|
|
|
|
movement: pygame.Vector2 = direction * self.speed * dt
|
|
|
|
self.pos.x += movement.x
|
|
|
|
if self.pos.x - self.width / 2 < 0:
|
|
self.pos.x = self.width / 2
|
|
if self.pos.x + self.width / 2 > SCREEN_WIDTH:
|
|
self.pos.x = SCREEN_WIDTH - self.width / 2
|
|
self.rect.centerx = round(self.pos.x)
|
|
|
|
for wall in walls:
|
|
if self.rect.colliderect(wall):
|
|
if movement.x > 0:
|
|
self.rect.right = wall.left
|
|
elif movement.x < 0:
|
|
self.rect.left = wall.right
|
|
self.pos.x = self.rect.centerx
|
|
|
|
self.pos.y += movement.y
|
|
|
|
if self.pos.y - self.height / 2 < 0:
|
|
self.pos.y = self.height / 2
|
|
if self.pos.y + self.height / 2 > SCREEN_HEIGHT:
|
|
self.pos.y = SCREEN_HEIGHT - self.height / 2
|
|
self.rect.centery = round(self.pos.y)
|
|
|
|
for wall in walls:
|
|
if self.rect.colliderect(wall):
|
|
if movement.y > 0:
|
|
self.rect.bottom = wall.top
|
|
elif movement.y < 0:
|
|
self.rect.top = wall.bottom
|
|
self.pos.y = self.rect.centery
|
|
|
|
def render(self, surface):
|
|
pygame.draw.rect(surface, (255, 0, 0), self.rect)
|
|
|
|
|
|
game = Game()
|
|
game.run()
|