Files
2024-10-29 17:18:30 +01:00

61 lines
2.5 KiB
Python

import pygame as pg
from Vec import Vec
class Maze:
def __init__(self, grid_cell_size):
self.grid_cell_size = grid_cell_size
self.maze = []
# jeweilige .pac Datei wird ausgelesen
def read_file(self, filename):
try:
with open(filename, "r") as file:
data = file.read()
lines = data.split("\n")
for line in lines:
inner_list = []
self.maze.append(inner_list)
for f in line:
inner_list.append(int(f))
# rotate
temp = []
for i in range(len(self.maze)):
temp.append([])
for j in range(len(self.maze)):
temp[i].append(
self.maze[j][i]) # füllt das 2d Array an richtiger Stelle mit den Werten aus der .pac Datei
self.maze = temp
except FileNotFoundError:
print("File Not Found")
# Labyrinth wird für den Spieler gezeichnet
def draw(self, screen, color1, color2):
for x, l in enumerate(self.maze):
for y, f in enumerate(l):
# wenn die Zelle begehbar ist, bekommt sie die Farbe Weiß, ansonsten grau.
color = color1 if f == 1 else color2
if f > 0:
# Zelle wird mit ihrer Farbe gezeichnet
pg.draw.rect(screen, color, (x * self.grid_cell_size,
y * self.grid_cell_size, self.grid_cell_size,
self.grid_cell_size),
border_bottom_left_radius=10 if self.is_free(Vec(x - 1, y)) and self.is_free(
Vec(x, y + 1)) else 0,
border_bottom_right_radius=10 if self.is_free(Vec(x + 1, y)) and self.is_free(
Vec(x, y + 1)) else 0,
border_top_left_radius=10 if self.is_free(Vec(x - 1, y)) and self.is_free(
Vec(x, y - 1)) else 0,
border_top_right_radius=10 if self.is_free(Vec(x + 1, y)) and self.is_free(
Vec(x, y - 1)) else 0,
)
def is_free(self, v):
if len(self.maze) > v.x >= 0 and len(self.maze[v.x]) > v.y >= 0:
return self.maze[v.x][v.y] == 0
return True