Aufgabe gelöst
This commit is contained in:
parent
45d9d71310
commit
b2ae3d4ba7
3 changed files with 277 additions and 123 deletions
259
der_test.py
Normal file
259
der_test.py
Normal file
|
|
@ -0,0 +1,259 @@
|
|||
import pygame
|
||||
from pygame.math import Vector2
|
||||
from pygame.rect import Rect
|
||||
from pygame.typing import SequenceLike
|
||||
|
||||
from scripts.utils import load_image
|
||||
|
||||
TILE_SIZE = 64
|
||||
COLS = 20
|
||||
ROWS = 12
|
||||
SCREEN_WIDTH = TILE_SIZE * COLS
|
||||
SCREEN_HEIGHT = TILE_SIZE * ROWS
|
||||
SCREEN_CENTER = (SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)
|
||||
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:
|
||||
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.player_pos, width=PLAYER_SIZE, height=PLAYER_SIZE)
|
||||
|
||||
# Flashlight
|
||||
self.flashlight_player = Flashlight(self, self.player)
|
||||
|
||||
# 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: 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),
|
||||
]
|
||||
|
||||
# Assets
|
||||
self.assets = {"floor_01": load_image("level/ground/floor_1.png")}
|
||||
|
||||
def update(self) -> None:
|
||||
# Ereignisse
|
||||
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.key == pygame.K_q:
|
||||
self.flashlight_player.light_state = not self.flashlight_player.light_state
|
||||
if self.flashlight_player.render_item:
|
||||
self.flashlight_player.render_item = False
|
||||
|
||||
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.flashlight_player.update()
|
||||
|
||||
def render(self) -> None:
|
||||
# Hintergrund
|
||||
self.screen.fill((0, 0, 0, 0))
|
||||
|
||||
for y in range(ROWS):
|
||||
for x in range(COLS):
|
||||
self.screen.blit(
|
||||
self.assets["floor_01"],
|
||||
pygame.Rect(x * 64, y * 64, TILE_SIZE, TILE_SIZE),
|
||||
)
|
||||
|
||||
# 1. Zeichne 1 Tile -> []
|
||||
|
||||
# 2. Zeichne eine Reihe von Tiles -> [][][][][][]
|
||||
|
||||
# 3. Zeichne mehrere Reihen von Tiles
|
||||
# -> [][][][][][]
|
||||
# -> [][][][][][]
|
||||
# -> [][][][][][]
|
||||
|
||||
# Licht
|
||||
self.flashlight_player.render(self.screen)
|
||||
if self.flashlight_player.render_item:
|
||||
self.test_item.render(self.screen)
|
||||
|
||||
# Wände
|
||||
for wall in self.walls:
|
||||
pygame.draw.rect(self.screen, "grey", wall)
|
||||
|
||||
# Spieler
|
||||
self.player.render(self.screen)
|
||||
|
||||
# Surfaces aktualisieren
|
||||
pygame.display.update()
|
||||
|
||||
|
||||
|
||||
def run(self) -> None:
|
||||
while self.running:
|
||||
self.dt: float = self.clock.tick(60) / 1000.0
|
||||
self.update()
|
||||
self.render()
|
||||
|
||||
|
||||
class Player:
|
||||
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
|
||||
)
|
||||
|
||||
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)
|
||||
|
||||
|
||||
class Flashlight:
|
||||
def __init__(self, game, sprite):
|
||||
self.game = game
|
||||
self.sprite = sprite
|
||||
self.light_points: list[pygame.Vector2] = []
|
||||
self.light_surface: pygame.Surface = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.SRCALPHA)
|
||||
self.light_state: bool = True
|
||||
self.render_item = False
|
||||
|
||||
def create_light_points(self) -> list[pygame.Vector2]:
|
||||
points: list[pygame.Vector2] = []
|
||||
origin: Vector2 = self.sprite.pos
|
||||
base_ray = pygame.Vector2(LIGHT_RADIUS, 0)
|
||||
|
||||
for angle in range(360):
|
||||
ray_end = origin + base_ray.rotate(angle)
|
||||
for wall in self.game.walls:
|
||||
intersection = wall.clipline(origin, ray_end)
|
||||
if intersection:
|
||||
ray_end = pygame.Vector2(intersection[0])
|
||||
points.append(ray_end)
|
||||
|
||||
return points
|
||||
|
||||
def update(self):
|
||||
self.render_item = False
|
||||
if not self.light_state:
|
||||
return
|
||||
|
||||
self.light_points = self.create_light_points()
|
||||
self.light_surface.fill((0, 0, 0, 0))
|
||||
pygame.draw.polygon(self.light_surface, (243, 255, 74, 255), self.light_points)
|
||||
light_mask = pygame.mask.from_surface(self.light_surface)
|
||||
item = self.game.test_item
|
||||
self.render_item = light_mask.overlap(self.game.test_item_mask, item.rect.topleft) is not None
|
||||
|
||||
def render(self, surface):
|
||||
if self.light_state:
|
||||
surface.blit(self.light_surface, (0, 0))
|
||||
points: list[pygame.Vector2] = []
|
||||
origin: Vector2 = self.sprite.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.game.walls:
|
||||
# Überschneidung
|
||||
intersection = wall.clipline(origin, ray_end)
|
||||
if intersection:
|
||||
ray_end = pygame.Vector2(intersection[0])
|
||||
points.append(ray_end)
|
||||
|
||||
return points
|
||||
|
||||
game = Game()
|
||||
game.run()
|
||||
|
||||
|
||||
"""
|
||||
in Klasse Flashlight:
|
||||
|
||||
|
||||
render_item in Item Klasse machen
|
||||
"""
|
||||
38
game.py
38
game.py
|
|
@ -16,20 +16,29 @@ LIGHT_RADIUS = 150
|
|||
|
||||
|
||||
class Item:
|
||||
def __init__(self, x: float, y: float, width: float, height: float, color):
|
||||
def __init__(self, x: float, y: float, width: float, height: float, color, flashlight, game):
|
||||
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
|
||||
self.visible: bool = False
|
||||
self.flashlight = flashlight
|
||||
self.game = game
|
||||
|
||||
@property
|
||||
def rect(self) -> pygame.Rect:
|
||||
return pygame.Rect(self.x, self.y, self.width, self.height)
|
||||
|
||||
def update(self):
|
||||
self.visible = False
|
||||
self.visible = self.flashlight.light_mask.overlap(self.game.test_item_mask, self.rect.topleft) is not None
|
||||
|
||||
|
||||
def render(self, surface):
|
||||
pygame.draw.rect(surface, self.color, self.rect)
|
||||
if self.visible and self.flashlight.light_state:
|
||||
pygame.draw.rect(surface, self.color, self.rect)
|
||||
|
||||
|
||||
class Game:
|
||||
|
|
@ -55,7 +64,7 @@ class Game:
|
|||
self.flashlight_player = Flashlight(self, self.player)
|
||||
|
||||
# Items
|
||||
self.test_item = Item(140, 300, 20, 20, "blue")
|
||||
self.test_item = Item(140, 300, 20, 20, "blue", self.flashlight_player, self)
|
||||
self.test_item_mask = pygame.mask.Mask(self.test_item.rect.size, fill=True)
|
||||
# Level
|
||||
self.walls: list[Rect] = [
|
||||
|
|
@ -79,8 +88,7 @@ class Game:
|
|||
self.player.speed = 300
|
||||
if event.key == pygame.K_q:
|
||||
self.flashlight_player.light_state = not self.flashlight_player.light_state
|
||||
if self.flashlight_player.render_item:
|
||||
self.flashlight_player.render_item = False
|
||||
|
||||
|
||||
if event.type == pygame.KEYUP and event.key == pygame.K_LSHIFT:
|
||||
self.player.speed = 150
|
||||
|
|
@ -88,6 +96,7 @@ class Game:
|
|||
# Spielzustände aktualisieren
|
||||
self.player.update(walls=self.walls, dt=self.dt)
|
||||
self.flashlight_player.update()
|
||||
self.test_item.update()
|
||||
|
||||
def render(self) -> None:
|
||||
# Hintergrund
|
||||
|
|
@ -111,8 +120,7 @@ class Game:
|
|||
|
||||
# Licht
|
||||
self.flashlight_player.render(self.screen)
|
||||
if self.flashlight_player.render_item:
|
||||
self.test_item.render(self.screen)
|
||||
self.test_item.render(self.screen)
|
||||
|
||||
# Wände
|
||||
for wall in self.walls:
|
||||
|
|
@ -197,7 +205,7 @@ class Flashlight:
|
|||
self.light_points: list[pygame.Vector2] = []
|
||||
self.light_surface: pygame.Surface = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.SRCALPHA)
|
||||
self.light_state: bool = True
|
||||
self.render_item = False
|
||||
self.light_mask = None
|
||||
|
||||
def create_light_points(self) -> list[pygame.Vector2]:
|
||||
points: list[pygame.Vector2] = []
|
||||
|
|
@ -215,16 +223,14 @@ class Flashlight:
|
|||
return points
|
||||
|
||||
def update(self):
|
||||
self.render_item = False
|
||||
if not self.light_state:
|
||||
return
|
||||
|
||||
self.light_points = self.create_light_points()
|
||||
self.light_surface.fill((0, 0, 0, 0))
|
||||
pygame.draw.polygon(self.light_surface, (243, 255, 74, 255), self.light_points)
|
||||
light_mask = pygame.mask.from_surface(self.light_surface)
|
||||
item = self.game.test_item
|
||||
self.render_item = light_mask.overlap(self.game.test_item_mask, item.rect.topleft) is not None
|
||||
self.light_mask = pygame.mask.from_surface(self.light_surface)
|
||||
|
||||
|
||||
def render(self, surface):
|
||||
if self.light_state:
|
||||
|
|
@ -249,11 +255,3 @@ class Flashlight:
|
|||
|
||||
game = Game()
|
||||
game.run()
|
||||
|
||||
|
||||
"""
|
||||
in Klasse Flashlight:
|
||||
|
||||
|
||||
render_item in Item Klasse machen
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -1,103 +0,0 @@
|
|||
|
||||
import pygame
|
||||
|
||||
# pygame setup
|
||||
pygame.init()
|
||||
screen = pygame.display.set_mode((1280, 720))
|
||||
clock = pygame.time.Clock()
|
||||
running = True
|
||||
dt = 0
|
||||
|
||||
player_pos = pygame.Vector2(screen.get_width() / 2, screen.get_height() / 2)
|
||||
|
||||
walls = [
|
||||
pygame.Rect(980, 100, 50, 440),
|
||||
pygame.Rect(280, 650, 720, 35),
|
||||
]
|
||||
|
||||
|
||||
|
||||
|
||||
while running:
|
||||
# poll for events
|
||||
# pygame.QUIT event means the user clicked X to close your window
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
running = False
|
||||
|
||||
if event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_ESCAPE:
|
||||
running = False
|
||||
|
||||
if event.key == pygame.K_ESCAPE:
|
||||
running = False
|
||||
|
||||
# fill the screen with a color to wipe away anything from last frame
|
||||
screen.fill("black")
|
||||
|
||||
for wall in walls:
|
||||
pygame.draw.rect(screen, "grey", wall)
|
||||
|
||||
end_pos = pygame.Vector2(player_pos[0] + 300, player_pos[1])
|
||||
|
||||
# pygame.draw.line(screen, "cyan", (640, 360), vector2)
|
||||
|
||||
"""--------------------------------------- WICHTIG: Vector2.rotate() ---------------------------------------"""
|
||||
|
||||
|
||||
ray_end = pygame.Vector2()
|
||||
light_points = []
|
||||
vector = pygame.Vector2(300, 0)
|
||||
|
||||
# Lichtstrahlen berechnen
|
||||
for i in range(360):
|
||||
|
||||
rotated = vector.rotate(i)
|
||||
ray_end = player_pos + rotated
|
||||
|
||||
|
||||
# Überschneidung mit den Wänden prüfen
|
||||
for wall in walls:
|
||||
überschneidung = wall.clipline(player_pos, ray_end)
|
||||
|
||||
if überschneidung:
|
||||
schnittpunkt = pygame.Vector2(überschneidung[0])
|
||||
|
||||
ray_end = schnittpunkt
|
||||
|
||||
|
||||
light_points.append(ray_end)
|
||||
|
||||
|
||||
|
||||
|
||||
pygame.draw.polygon(
|
||||
screen,
|
||||
"yellow",
|
||||
light_points
|
||||
)
|
||||
|
||||
|
||||
|
||||
pygame.draw.circle(screen, "red", player_pos, 40)
|
||||
|
||||
keys = pygame.key.get_pressed()
|
||||
if keys[pygame.K_w]:
|
||||
player_pos.y -= 300 * dt
|
||||
if keys[pygame.K_s]:
|
||||
player_pos.y += 300 * dt
|
||||
if keys[pygame.K_a]:
|
||||
player_pos.x -= 300 * dt
|
||||
if keys[pygame.K_d]:
|
||||
player_pos.x += 300 * dt
|
||||
|
||||
# flip() the display to put your work on screen
|
||||
pygame.display.flip()
|
||||
|
||||
# limits FPS to 60
|
||||
# dt is delta time in seconds since last frame, used for framerate-
|
||||
# independent physics.
|
||||
dt = clock.tick(60) / 1000
|
||||
|
||||
|
||||
pygame.quit()
|
||||
Loading…
Reference in a new issue