Assets und Klassen für das Spiel hinzugefügt: neue Texturen, Level- und Item-Klassen implementiert, sowie die Flashlight-Logik erstellt.
This commit is contained in:
parent
f41f872243
commit
01e925902a
10 changed files with 254 additions and 179 deletions
Binary file not shown.
|
Before Width: | Height: | Size: 1.5 KiB |
BIN
data/textures/items/furniture/plants/plant.png
Normal file
BIN
data/textures/items/furniture/plants/plant.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.5 KiB |
BIN
data/textures/level/walls/wall_0.png
Normal file
BIN
data/textures/level/walls/wall_0.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 4.5 KiB |
216
game.py
216
game.py
|
|
@ -1,54 +1,11 @@
|
|||
import pygame
|
||||
from pygame.math import Vector2
|
||||
from pygame.rect import Rect
|
||||
from pygame.typing import SequenceLike
|
||||
|
||||
from scripts.constants import *
|
||||
from scripts.flashlight import Flashlight
|
||||
from scripts.level import Level
|
||||
from scripts.player import Player
|
||||
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, flashlight, game, image: pygame.Surface = None):
|
||||
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
|
||||
self.image = image
|
||||
self.mask = pygame.mask.Mask(self.rect.size, fill=True)
|
||||
|
||||
@property
|
||||
def rect(self) -> pygame.Rect:
|
||||
if not self.image:
|
||||
return pygame.Rect(self.x, self.y, self.width, self.height)
|
||||
|
||||
else:
|
||||
return pygame.Rect(self.x, self.y, self.image.get_width(), self.image.get_height())
|
||||
|
||||
def update(self):
|
||||
self.visible = False
|
||||
self.visible = self.flashlight.light_mask.overlap(self.mask, self.rect.topleft) is not None
|
||||
|
||||
|
||||
def render(self, surface: pygame.Surface):
|
||||
if self.visible and self.flashlight.light_state:
|
||||
if not self.image:
|
||||
pygame.draw.rect(surface, self.color, self.rect)
|
||||
else:
|
||||
surface.blit(self.image, (self.x, self.y, self.image.get_width(), self.image.get_height()))
|
||||
|
||||
|
||||
class Game:
|
||||
def __init__(self):
|
||||
|
|
@ -75,24 +32,32 @@ class Game:
|
|||
# Assets
|
||||
self.assets = {
|
||||
"floor_01": load_image("level/ground/floor_1.png"),
|
||||
"plant_01": load_image("items/furniture/plant.png"),
|
||||
"plant_01": load_image("items/furniture/plants/plant.png"),
|
||||
"wall": load_image("level/walls/wall_0.png"),
|
||||
}
|
||||
|
||||
# Items
|
||||
self.items = [
|
||||
Item(140, 300, 20, 20, "blue", self.flashlight_player, self, self.assets["plant_01"]),
|
||||
Item(140, 340, 20, 20, "red", self.flashlight_player, self),
|
||||
]
|
||||
self.level = Level(
|
||||
game=self,
|
||||
items=[],
|
||||
walls=[
|
||||
pygame.Rect(0, 0, SCREEN_WIDTH, TILE_SIZE),
|
||||
pygame.Rect(0, TILE_SIZE, TILE_SIZE, SCREEN_HEIGHT),
|
||||
pygame.Rect(
|
||||
TILE_SIZE, SCREEN_HEIGHT - TILE_SIZE, SCREEN_WIDTH, TILE_SIZE
|
||||
),
|
||||
pygame.Rect(SCREEN_WIDTH - TILE_SIZE, 0, SCREEN_WIDTH, SCREEN_HEIGHT),
|
||||
],
|
||||
)
|
||||
|
||||
self.level_zustand: int = 0
|
||||
|
||||
# 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),
|
||||
]
|
||||
|
||||
@property
|
||||
def walls(self):
|
||||
return self.level.walls
|
||||
|
||||
@property
|
||||
def items(self):
|
||||
return self.level.items
|
||||
|
||||
def update(self) -> None:
|
||||
# Ereignisse
|
||||
|
|
@ -105,8 +70,9 @@ class Game:
|
|||
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
|
||||
|
||||
self.flashlight_player.light_state = (
|
||||
not self.flashlight_player.light_state
|
||||
)
|
||||
|
||||
if event.type == pygame.KEYUP and event.key == pygame.K_LSHIFT:
|
||||
self.player.speed = 150
|
||||
|
|
@ -122,12 +88,7 @@ class Game:
|
|||
# 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),
|
||||
)
|
||||
self.level.render(self.screen)
|
||||
|
||||
# 1. Zeichne 1 Tile -> []
|
||||
|
||||
|
|
@ -144,18 +105,12 @@ class Game:
|
|||
for item in self.items:
|
||||
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
|
||||
|
|
@ -163,123 +118,28 @@ class Game:
|
|||
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):
|
||||
|
||||
class Wall:
|
||||
def __init__(self, game, x: float, y: float, width: float, height: float):
|
||||
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.light_mask = None
|
||||
|
||||
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
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.width = width
|
||||
self.height = height
|
||||
|
||||
def update(self):
|
||||
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)
|
||||
self.light_mask = pygame.mask.from_surface(self.light_surface)
|
||||
|
||||
pass
|
||||
|
||||
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)
|
||||
pass
|
||||
|
||||
# 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()
|
||||
|
||||
|
||||
|
||||
"""
|
||||
Ein paar Assets schonmal zurechtschneiden.
|
||||
"""
|
||||
9
scripts/constants.py
Normal file
9
scripts/constants.py
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
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
|
||||
WALL_COLOR = "grey"
|
||||
62
scripts/flashlight.py
Normal file
62
scripts/flashlight.py
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
import pygame
|
||||
from pygame.math import Vector2
|
||||
|
||||
from scripts.constants import *
|
||||
|
||||
|
||||
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.light_mask = None
|
||||
|
||||
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):
|
||||
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)
|
||||
self.light_mask = pygame.mask.from_surface(self.light_surface)
|
||||
|
||||
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
|
||||
|
||||
53
scripts/item.py
Normal file
53
scripts/item.py
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
import pygame
|
||||
from pygame.typing import SequenceLike
|
||||
|
||||
|
||||
class Item:
|
||||
def __init__(
|
||||
self,
|
||||
x: float,
|
||||
y: float,
|
||||
width: float,
|
||||
height: float,
|
||||
color,
|
||||
flashlight,
|
||||
game,
|
||||
image: pygame.Surface = None,
|
||||
):
|
||||
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
|
||||
self.image = image
|
||||
self.mask = pygame.mask.Mask(self.rect.size, fill=True)
|
||||
|
||||
@property
|
||||
def rect(self) -> pygame.Rect:
|
||||
if not self.image:
|
||||
return pygame.Rect(self.x, self.y, self.width, self.height)
|
||||
|
||||
else:
|
||||
return pygame.Rect(
|
||||
self.x, self.y, self.image.get_width(), self.image.get_height()
|
||||
)
|
||||
|
||||
def update(self):
|
||||
self.visible = False
|
||||
self.visible = (
|
||||
self.flashlight.light_mask.overlap(self.mask, self.rect.topleft) is not None
|
||||
)
|
||||
|
||||
def render(self, surface: pygame.Surface):
|
||||
if self.visible and self.flashlight.light_state:
|
||||
if not self.image:
|
||||
pygame.draw.rect(surface, self.color, self.rect)
|
||||
else:
|
||||
surface.blit(
|
||||
self.image,
|
||||
(self.x, self.y, self.image.get_width(), self.image.get_height()),
|
||||
)
|
||||
30
scripts/level.py
Normal file
30
scripts/level.py
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import pygame
|
||||
|
||||
from scripts.constants import *
|
||||
from scripts.item import Item
|
||||
|
||||
|
||||
class Level:
|
||||
def __init__(self, game, items: list[Item], walls: list[pygame.Rect]):
|
||||
self.game = game
|
||||
|
||||
# Items
|
||||
self.items = items
|
||||
|
||||
# Level
|
||||
self.walls = walls
|
||||
|
||||
def render(self, surface: pygame.Surface):
|
||||
for y in range(ROWS):
|
||||
for x in range(COLS):
|
||||
surface.blit(
|
||||
self.game.assets["floor_01"],
|
||||
pygame.Rect(x * 64, y * 64, TILE_SIZE, TILE_SIZE),
|
||||
)
|
||||
|
||||
# Wände
|
||||
for wall in self.walls:
|
||||
surface.blit(self.game.assets["wall"], wall)
|
||||
|
||||
def draw_walls(self, surface: pygame.Surface):
|
||||
pass
|
||||
61
scripts/player.py
Normal file
61
scripts/player.py
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
import pygame
|
||||
from pygame.math import Vector2
|
||||
|
||||
from scripts.constants import *
|
||||
|
||||
|
||||
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)
|
||||
0
scripts/wall.py
Normal file
0
scripts/wall.py
Normal file
Loading…
Reference in a new issue