Merge branch 'main' into feat/text_input

This commit is contained in:
Kevin Baumgartner
2026-05-07 21:22:43 +02:00
3 changed files with 173 additions and 6 deletions
+14 -6
View File
@@ -1,5 +1,13 @@
import pygame
from src.settings import (
DOOR_WIDTH,
DOOR_HEIGHT,
DOOR_BORDER_WIDTH,
COLOR_DOOR_BEIGE,
COLOR_DOOR_BORDER
)
# using class to be able to reuse it
# as we will have multiple rooms to go in
class Door:
@@ -12,15 +20,15 @@ class Door:
self.current_hp = hp_value
self.is_closed = False
self.door_color = (245, 245, 220)
self.door_border_color = (100, 90, 80)
self.door_color = COLOR_DOOR_BEIGE
self.door_border_color = COLOR_DOOR_BORDER
def draw(self, screen):
# main space of the door with 20x100 px
pygame.draw.rect(screen, self.door_color, (self.x, self.y, 20, 100))
# door
pygame.draw.rect(screen, self.door_color, (self.x, self.y, DOOR_WIDTH, DOOR_HEIGHT))
# doorframe 3px
pygame.draw.rect(screen, self.door_border_color, (self.x, self.y, 20, 100), 3)
# doorframe
pygame.draw.rect(screen, self.door_border_color, (self.x, self.y, DOOR_WIDTH, DOOR_HEIGHT), DOOR_BORDER_WIDTH)
# when attacked and gets dmg reduces the health (if closed), door "breaks" when health = 0
def take_damage(self, amount):
+149
View File
@@ -0,0 +1,149 @@
import pygame
# Fenstergröße und Tile Größe (Felder)
tile_size = 30
screen_w = 1900
screen_h = 900
# Farben für die einzelnen Tiles
color_wall = (30, 20, 40) # black like
color_room = (75, 40, 55) # dark red-violet
color_hall = (35, 65, 45) # dark green
color_door = (40, 90, 110) # dark blue
color_void = (10, 10, 15) # a little more black
color_text = (220, 180, 210) # rose
color_border = (55, 35, 70)
color_start = (255, 0, 0)# dark violet
class Map:
def __init__(self):
# Map wird mit "Tiles" (30x30 Feldern aufgebaut
# W=Wall, R=Room, S=Start, H=Hall, D=Door
self.map = [
"WWWWWWWWWWWWWWWWWWWHHSSHHWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
"WWWWWWWWWWWWWWWWWWWHHHH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HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHDD",
"WWHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHWW",
"WWHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHWW",
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
]
# Raumbeschreibung in (Spalte, Zeile, Beschreibung)
# gibt an wo der Raumname auf dem Bildschirm angezeigt wird
self.room_decription = [
(7, 13, "Room 102"),
(32, 13, "Room 103"),
(49, 13, "Room 105")
]
def get_tile(self, column: int, row: int):
# ein check, ob die Position innerhalb der Map liegt
if 0 <= row < len(self.map) and 0 <= column < len(self.map[row]):
# gibt den Buchstaben der Position zurück
return self.map[row][column]
# falls Außerhalb der Map, ein return als Wand
return "W"
def is_accessible(self, column: int, row: int) -> bool:
# gibt True zurück, wenn der Spieler das Tile betreten darf
return self.get_tile(column, row) in ("R", "D,", "H", "S")
# Funktion, um die Map zu erstellen
def draw_map(self, surface: pygame.Surface, font: pygame.font.Font, cam_x: int = 0, cam_y: int = 0) -> None:
# Dict, dass jedem Buchstaben eine Farbe zuordnet
color_for = {
"W": color_wall,
"R": color_room,
"H": color_hall,
"D": color_door,
"S": color_start,
}
# äußere Schleife Zeile für Zeile
for row_nr in range(len(self.map)):
# innerer Loop für jedes Zeichen in der Zeile
for column_nr in range(len(self.map[row_nr])):
# holt sich den Buchstaben und speichert ihn in der Variable mark
mark = self.map[row_nr][column_nr]
# sucht die Farbe für den Buchstaben - falls unbekannt -> color_void
color = color_for.get(mark, color_void)
# errechnet die Position des Tiles auf dem Bildschirm mit "column_nr * tile_size", "- cam_x" verschiebt alles mit der Kamera
rect = pygame.Rect(
column_nr * tile_size - cam_x,
row_nr * tile_size - cam_y,
# Tile-Breite
tile_size,
# Tile-Höhe
tile_size,
)
# das Rechteck ind er Farbe zeichnen
pygame.draw.rect(surface, color, rect)
# die Wände sollen einen zusätzlichen Rand haben mit 1 = Wandbreite in Pixel
if mark == "W":
pygame.draw.rect(surface, color_border, rect, 1)
# geht die Beschriftungen durch
for i in range(len(self.room_decription)):
# entpackt das Tupel in 3 Variablen: column, row, text
column, row, text = self.room_decription[i]
# die Tile-Koordinaten in Pixel umrechnen und addieren von 2, damit der Text nicht ganz am Rand klebt
px = column * tile_size - cam_x + 2
py = row * tile_size - cam_y + 2
# Text-Bild mit fon.render()
# True gibt eine glattere Schrift
# blit() fixiert den Text auf dem Bildschirm
surface.blit(font.render(text, True, color_text), (px, py))
if __name__ == "__main__":
pygame.init()
screen = pygame.display.set_mode((screen_w, screen_h))
font = pygame.font.SysFont("monospace", 26, bold=True)
clock = pygame.time.Clock()
my_map = Map()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
screen.fill(color_void)
my_map.draw_map(screen, font)
pygame.display.flip()
clock.tick(60)
+10
View File
@@ -2,3 +2,13 @@
DESK_WIDTH = 70
DESK_HEIGHT = 40
# --- DOOR ---
DOOR_WIDTH = 20
DOOR_HEIGHT = 100
DOOR_BORDER_WIDTH = 3
COLOR_DOOR_BEIGE = (245, 245, 220)
COLOR_DOOR_BORDER = (100, 90, 80)
COLOR_HP_BAR_GREEN = (0, 255, 0)
COLOR_HP_BAR_BG = (50, 50, 50)