Merge branch 'feat/map' into 'main'
feat: implement basic map structure See merge request swd25-bootcamp/silent-fh!4
This commit is contained in:
+149
@@ -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",
|
||||
"WWWWWWWWWWWWWWWWWWWHHHHHHWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
|
||||
"WWRRRRRRRRRRRRRRRWWHHHHHHWWRRRRRRRRRRRRRRRWWRRRRRRRRRRRRRRRWW",
|
||||
"WWRRRRRRRRRRRRRRRWWHHHHHHWWRRRRRRRRRRRRRRRWWRRRRRRRRRRRRRRRWW",
|
||||
"WWRRRRRRRRRRRRRRRWWHHHHHHWWRRRRRRRRRRRRRRRWWRRRRRRRRRRRRRRRWW",
|
||||
"WWRRRRRRRRRRRRRRRWWHHHHHHWWRRRRRRRRRRRRRRRWWRRRRRRRRRRRRRRRWW",
|
||||
"WWRRRRRRRRRRRRRRRWWHHHHHHWWRRRRRRRRRRRRRRRWWRRRRRRRRRRRRRRRWW",
|
||||
"WWRRRRRRRRRRRRRRRWWHHHHHHWWRRRRRRRRRRRRRRRWWRRRR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ibung)
|
||||
# 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)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user