Compare commits
No commits in common. "99dc8c8a98aa1fdcacd73db797f6d8a2e53acbad" and "c8494dcab1661c098629c73b05d62750947a2170" have entirely different histories.
99dc8c8a98
...
c8494dcab1
131
backup/game.py
|
|
@ -1,131 +0,0 @@
|
||||||
import pygame
|
|
||||||
|
|
||||||
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
|
|
||||||
from scripts.wall import Wall
|
|
||||||
|
|
||||||
|
|
||||||
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
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
# Assets
|
|
||||||
self.assets = {
|
|
||||||
"floor_01": load_image("level/ground/floor_1.png"),
|
|
||||||
"plant_01": load_image("items/furniture/plants/plant.png"),
|
|
||||||
"wall": load_image("level/walls/wall_0.png"),
|
|
||||||
|
|
||||||
"player": load_image("entity/player/player.png"),
|
|
||||||
}
|
|
||||||
self.player = Player(self.player_pos, width=PLAYER_SIZE, height=PLAYER_SIZE, image=self.assets["player"])
|
|
||||||
self.flashlight_player = Flashlight(self, self.player)
|
|
||||||
|
|
||||||
self.level = Level(
|
|
||||||
game=self,
|
|
||||||
items=[],
|
|
||||||
walls=[
|
|
||||||
Wall(self, 0, 0, SCREEN_WIDTH//TILE_SIZE, 1),
|
|
||||||
Wall(self, 0, TILE_SIZE, 1, SCREEN_HEIGHT//TILE_SIZE),
|
|
||||||
Wall(
|
|
||||||
self, TILE_SIZE, SCREEN_HEIGHT - TILE_SIZE, SCREEN_WIDTH//TILE_SIZE, 1
|
|
||||||
),
|
|
||||||
Wall(self, SCREEN_WIDTH - TILE_SIZE, 0, 1, SCREEN_HEIGHT//TILE_SIZE),
|
|
||||||
Wall(self, 500, 500, 2, 2)
|
|
||||||
],
|
|
||||||
)
|
|
||||||
|
|
||||||
self.level_zustand: int = 0
|
|
||||||
|
|
||||||
@property
|
|
||||||
def walls(self):
|
|
||||||
return self.level.walls
|
|
||||||
|
|
||||||
@property
|
|
||||||
def items(self):
|
|
||||||
return self.level.items
|
|
||||||
|
|
||||||
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 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()
|
|
||||||
|
|
||||||
for item in self.items:
|
|
||||||
item.update()
|
|
||||||
|
|
||||||
def render(self) -> None:
|
|
||||||
# Hintergrund
|
|
||||||
self.screen.fill((0, 0, 0, 0))
|
|
||||||
|
|
||||||
self.level.render(self.screen)
|
|
||||||
|
|
||||||
# 1. Zeichne 1 Tile -> []
|
|
||||||
|
|
||||||
# 2. Zeichne eine Reihe von Tiles -> [][][][][][]
|
|
||||||
|
|
||||||
# 3. Zeichne mehrere Reihen von Tiles
|
|
||||||
# -> [][][][][][]
|
|
||||||
# -> [][][][][][]
|
|
||||||
# -> [][][][][][]
|
|
||||||
|
|
||||||
# Licht
|
|
||||||
self.flashlight_player.render(self.screen)
|
|
||||||
|
|
||||||
for item in self.items:
|
|
||||||
item.render(self.screen)
|
|
||||||
|
|
||||||
# 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()
|
|
||||||
|
|
||||||
|
|
||||||
game = Game()
|
|
||||||
game.run()
|
|
||||||
|
|
||||||
|
|
||||||
"""
|
|
||||||
Wie funktioniert Kollision mit Bildern?????????????????????????!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
||||||
(Günter hat ein Tutorial)
|
|
||||||
"""
|
|
||||||
|
|
@ -1,76 +0,0 @@
|
||||||
import pygame
|
|
||||||
from pygame.math import Vector2
|
|
||||||
|
|
||||||
from scripts.constants import *
|
|
||||||
|
|
||||||
|
|
||||||
class Player:
|
|
||||||
def __init__(self, pos: pygame.Vector2, width, height, image: pygame.Surface=None):
|
|
||||||
self.pos: Vector2 = pos
|
|
||||||
self.width: float = width
|
|
||||||
self.height: float = height
|
|
||||||
self.speed = 150
|
|
||||||
self.image = image
|
|
||||||
self.mask = pygame.mask.from_surface(image)
|
|
||||||
self.rect = image.get_frect(center=(64, 64))
|
|
||||||
# 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:
|
|
||||||
wall_rect = wall.get_rect()
|
|
||||||
if self.mask.overlap(pygame.mask.from_surface(wall.image), (0, 0)):
|
|
||||||
if movement.x > 0:
|
|
||||||
self.rect.right = wall_rect.left
|
|
||||||
elif movement.x < 0:
|
|
||||||
self.rect.left = wall_rect.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:
|
|
||||||
wall_rect = wall.get_rect()
|
|
||||||
offset = (int(self.rect.x) - wall_rect.x, int(self.rect.y) - wall_rect.y)
|
|
||||||
|
|
||||||
if self.mask.overlap(pygame.mask.from_surface(wall.image), offset):
|
|
||||||
if movement.y > 0:
|
|
||||||
self.rect.bottom = wall_rect.top
|
|
||||||
elif movement.y < 0:
|
|
||||||
self.rect.top = wall_rect.bottom
|
|
||||||
self.pos.y = self.rect.centery
|
|
||||||
|
|
||||||
|
|
||||||
def render(self, surface: pygame.Surface):
|
|
||||||
if not self.image:
|
|
||||||
pygame.draw.rect(surface, self.color, self.rect)
|
|
||||||
else:
|
|
||||||
surface.blit(
|
|
||||||
self.image,
|
|
||||||
(self.pos[0]-TILE_SIZE/2, self.pos[1]-TILE_SIZE/2-10, self.image.get_width(), self.image.get_height()),
|
|
||||||
)
|
|
||||||
|
|
@ -1,35 +0,0 @@
|
||||||
import pygame
|
|
||||||
|
|
||||||
from scripts.constants import *
|
|
||||||
|
|
||||||
|
|
||||||
class Wall:
|
|
||||||
def __init__(self, game, x: float, y: float, cols: int, rows: int):
|
|
||||||
self.game = game
|
|
||||||
self.x = x
|
|
||||||
self.y = y
|
|
||||||
self.cols = cols
|
|
||||||
self.rows = rows
|
|
||||||
self.rect = pygame.Rect(x, y, cols*TILE_SIZE, rows*TILE_SIZE)
|
|
||||||
self.drawed_rect = None
|
|
||||||
self.grid: list[list[pygame.Rect]] = []
|
|
||||||
self.image = self.game.assets["wall"]
|
|
||||||
|
|
||||||
for row in range(self.rows): # rows
|
|
||||||
current_row = []
|
|
||||||
for col in range(self.cols): # cols
|
|
||||||
current_row.append(pygame.Rect(self.x+col*TILE_SIZE, self.y+row*TILE_SIZE, col*TILE_SIZE, row*TILE_SIZE))
|
|
||||||
|
|
||||||
self.grid.append(current_row)
|
|
||||||
|
|
||||||
def update(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def render(self, surface: pygame.Surface):
|
|
||||||
for row in self.grid:
|
|
||||||
for wall_rect in row:
|
|
||||||
surface.blit(self.image, wall_rect)
|
|
||||||
|
|
||||||
|
|
||||||
def get_rect(self) -> pygame.Rect:
|
|
||||||
return self.rect
|
|
||||||
BIN
data/textures/Assets/A4.png
Normal file
|
After Width: | Height: | Size: 68 KiB |
BIN
data/textures/Assets/A5.png
Normal file
|
After Width: | Height: | Size: 64 KiB |
BIN
data/textures/Assets/Inside_C.png
Normal file
|
After Width: | Height: | Size: 143 KiB |
BIN
data/textures/Assets/Inside_D.png
Normal file
|
After Width: | Height: | Size: 78 KiB |
BIN
data/textures/Assets/Inside_E.png
Normal file
|
After Width: | Height: | Size: 97 KiB |
|
Before Width: | Height: | Size: 53 KiB |
|
Before Width: | Height: | Size: 135 KiB |
|
Before Width: | Height: | Size: 144 KiB |
|
Before Width: | Height: | Size: 3.5 KiB |
|
Before Width: | Height: | Size: 3.8 KiB |
|
Before Width: | Height: | Size: 3.9 KiB |
|
Before Width: | Height: | Size: 3.6 KiB |
|
Before Width: | Height: | Size: 3.9 KiB |
|
Before Width: | Height: | Size: 3.7 KiB |
|
Before Width: | Height: | Size: 3.6 KiB |
|
Before Width: | Height: | Size: 3.5 KiB |
|
Before Width: | Height: | Size: 4.3 KiB |
|
Before Width: | Height: | Size: 4.5 KiB |
|
Before Width: | Height: | Size: 4.3 KiB |
|
Before Width: | Height: | Size: 4.2 KiB |
|
Before Width: | Height: | Size: 4.3 KiB |
|
Before Width: | Height: | Size: 4.1 KiB |
|
Before Width: | Height: | Size: 3.7 KiB |
|
Before Width: | Height: | Size: 3.5 KiB |
|
Before Width: | Height: | Size: 3.5 KiB |
|
Before Width: | Height: | Size: 3.8 KiB |
|
Before Width: | Height: | Size: 3.9 KiB |
|
Before Width: | Height: | Size: 3.8 KiB |
|
Before Width: | Height: | Size: 3.8 KiB |
|
Before Width: | Height: | Size: 3.7 KiB |
|
Before Width: | Height: | Size: 3.3 KiB |
|
Before Width: | Height: | Size: 3.6 KiB |
|
Before Width: | Height: | Size: 3.6 KiB |
|
Before Width: | Height: | Size: 3.4 KiB |
|
Before Width: | Height: | Size: 3.4 KiB |
|
Before Width: | Height: | Size: 3.4 KiB |
|
Before Width: | Height: | Size: 3.8 KiB |
|
Before Width: | Height: | Size: 3.8 KiB |
|
Before Width: | Height: | Size: 3.8 KiB |
|
Before Width: | Height: | Size: 3.7 KiB |
|
Before Width: | Height: | Size: 3.7 KiB |
|
Before Width: | Height: | Size: 3.6 KiB |
|
Before Width: | Height: | Size: 3.6 KiB |
|
Before Width: | Height: | Size: 3.6 KiB |
|
Before Width: | Height: | Size: 3.3 KiB |
|
Before Width: | Height: | Size: 3.3 KiB |
|
Before Width: | Height: | Size: 3.2 KiB |
|
Before Width: | Height: | Size: 3.3 KiB |
|
Before Width: | Height: | Size: 4.1 KiB |
|
Before Width: | Height: | Size: 4 KiB |
|
Before Width: | Height: | Size: 4.2 KiB |
|
Before Width: | Height: | Size: 3.8 KiB |
|
Before Width: | Height: | Size: 3.6 KiB |
|
Before Width: | Height: | Size: 4 KiB |
|
Before Width: | Height: | Size: 3.9 KiB |
|
Before Width: | Height: | Size: 4 KiB |
|
Before Width: | Height: | Size: 3.8 KiB |
|
Before Width: | Height: | Size: 3.9 KiB |
|
Before Width: | Height: | Size: 3.8 KiB |
|
Before Width: | Height: | Size: 3.8 KiB |
|
Before Width: | Height: | Size: 4.3 KiB |
|
Before Width: | Height: | Size: 4.1 KiB |
|
Before Width: | Height: | Size: 4 KiB |
|
Before Width: | Height: | Size: 3.7 KiB |
|
Before Width: | Height: | Size: 3.5 KiB |
|
Before Width: | Height: | Size: 3.8 KiB |
|
Before Width: | Height: | Size: 3.7 KiB |
|
Before Width: | Height: | Size: 3.7 KiB |
|
Before Width: | Height: | Size: 3.3 KiB |
|
Before Width: | Height: | Size: 3.3 KiB |
|
Before Width: | Height: | Size: 2.9 KiB |
|
Before Width: | Height: | Size: 3.2 KiB |
|
Before Width: | Height: | Size: 3.8 KiB |
|
Before Width: | Height: | Size: 3.5 KiB |
|
Before Width: | Height: | Size: 3.5 KiB |
|
Before Width: | Height: | Size: 3.7 KiB |
|
Before Width: | Height: | Size: 3.8 KiB |
|
Before Width: | Height: | Size: 3.3 KiB |
|
Before Width: | Height: | Size: 3.9 KiB |
|
Before Width: | Height: | Size: 3.7 KiB |
|
Before Width: | Height: | Size: 3.6 KiB |
|
Before Width: | Height: | Size: 3.6 KiB |
|
Before Width: | Height: | Size: 3.5 KiB |
|
Before Width: | Height: | Size: 2.9 KiB |
|
Before Width: | Height: | Size: 4.2 KiB |
|
Before Width: | Height: | Size: 3.8 KiB |
|
Before Width: | Height: | Size: 3.6 KiB |
|
Before Width: | Height: | Size: 3.8 KiB |
|
Before Width: | Height: | Size: 3.8 KiB |
|
Before Width: | Height: | Size: 3.7 KiB |
|
Before Width: | Height: | Size: 4.1 KiB |
|
Before Width: | Height: | Size: 3.3 KiB |
|
Before Width: | Height: | Size: 3.2 KiB |
|
Before Width: | Height: | Size: 3.2 KiB |
|
Before Width: | Height: | Size: 3.5 KiB |
|
Before Width: | Height: | Size: 3.4 KiB |
|
Before Width: | Height: | Size: 4.4 KiB |