def draw_map to display the map

This commit is contained in:
Lukas Kammerhofer
2026-05-07 20:37:08 +02:00
parent 72684f8e5f
commit 5e7a3197c0
+57 -1
View File
@@ -1,9 +1,11 @@
import pygame import pygame
# Fenstergröße und Tile Größe (Felder)
tile_size = 30 tile_size = 30
screen_w = 1800 screen_w = 1800
screen_h = 900 screen_h = 900
# Farben für die einzelnen Tiles
color_wall = (30, 20, 40) # black like color_wall = (30, 20, 40) # black like
color_room = (75, 40, 55) # dark red-violet color_room = (75, 40, 55) # dark red-violet
color_hall = (35, 65, 45) # dark green color_hall = (35, 65, 45) # dark green
@@ -15,8 +17,11 @@ color_border = (55, 35, 70) # dark violet
class Map: class Map:
def __init__(self): def __init__(self):
# Map wird mit "Tiles" (30x30 Feldern aufgebaut
# W=Wall, R=Room, S=Start, H=Hall, D=Door
self.map = [ self.map = [
"WWWWWWWWWWWWWWWWWHHWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW" "WWWWWWWWWWWWWWWWWSSWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW"
"WRRRRRRRRRRRRRRRWHHWRRRRRRRRRRRRRRRWRRRRRRRRRRRRRRRW" "WRRRRRRRRRRRRRRRWHHWRRRRRRRRRRRRRRRWRRRRRRRRRRRRRRRW"
"WRRRRRRRRRRRRRRRWHHWRRRRRRRRRRRRRRRWRRRRRRRRRRRRRRRW" "WRRRRRRRRRRRRRRRWHHWRRRRRRRRRRRRRRRWRRRRRRRRRRRRRRRW"
"WRRRRRRRRRRRRRRRWHHWRRRRRRRRRRRRRRRWRRRRRRRRRRRRRRRW" "WRRRRRRRRRRRRRRRWHHWRRRRRRRRRRRRRRRWRRRRRRRRRRRRRRRW"
@@ -36,19 +41,70 @@ class Map:
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW" "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW"
] ]
# Raumbeschreibung in (Spalte, Zeile, Beschreibung)
# gibt an wo der Raumname auf dem Bildschirm angezeigt wird
self.room_decription = [ self.room_decription = [
(8, 3, "Room 102"), (8, 3, "Room 102"),
(27, 3, "Room 103"), (27, 3, "Room 103"),
(43, 3, "Room 105") (43, 3, "Room 105")
] ]
def get_tile(self, column: int, row: int): 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]): 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] return self.map[row][column]
# falls Außerhalb der Map, ein return als Wand
return "W" return "W"
def is_accessible(self, column: int, row: int) -> bool: 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") return self.get_tile(column, row) in ("R", "D,", "H", "S")
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_hall,
}
# ä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)