Files
PyGame-Snake/tilemap/playground.py
Jan Lerking 537006167a Cleanup. /JL
2025-03-19 15:30:44 +01:00

97 lines
3.5 KiB
Python

"""
This is the part responsible for generating the playing area.
The setup is a simple tilemap, which consists of a variety of
tiles of 32x32 pixels.
There's a series of wall_tiles, ground_tiles and obstacle_tiles.
"""
import html
wall_tiles = {"WALL_NW": "╔", "WALL_HORIZONTAL": "═",
"WALL_NE": "╗", "WALL_VERTICAL": "║",
"WALL_SW": "╚", "WALL_SE": "╝",
"WALL_TOP_LEFT_OPENING": "╛", "WALL_TOP_RIGHT_OPENING": "╘",
"WALL_BOTTOM_LEFT_OPENING": "╕", "WALL_BOTTOM_RIGHT_OPENING": "╒",
"WALL_LEFT_UPPER_OPENING": "╜", "WALL_LEFT_LOWER_OPENING": "╖",
"WALL_RIGHT_UPPER_OPENING": "╙", "WALL_RIGHT_LOWER_OPENING": "╓"}
walls = [
["WALL_NW", "WALL_HORIZONTAL", "WALL_HORIZONTAL", "WALL_HORIZONTAL", "WALL_NE"],
["WALL_VERTICAL", " ", " ", " ", "WALL_VERTICAL"],
["WALL_VERTICAL", " ", " ", " ", "WALL_VERTICAL"],
["WALL_VERTICAL", " ", " ", " ", "WALL_VERTICAL"],
["WALL_SW", "WALL_HORIZONTAL", "WALL_HORIZONTAL", "WALL_HORIZONTAL", "WALL_SE"]
]
# Playground geometry matrix of rooms can range from 1x1 to 3x3.
geometries = [(1, 1), (2, 1), (3, 1),
(1, 2), (2, 2), (3, 2),
(1, 3), (2, 3), (3, 3)]
class Room:
def __init__(self, geometry: tuple):
self.room: list = []
self.create_random(geometry)
def create_random(self, geometry) -> None:
top: int = 0 # top = row 0
left: int = 0 # left = char 0 of row
right: int = geometry[0] - 1 # right = room_width-1
bottom: int = geometry[1] - 1 # bottom = room_height-1
room: list = []
for r in range(geometry[1]):
row: list = []
for c in range(geometry[0]):
if r == top and c == left:
row.append(wall_tiles["WALL_NW"])
continue
elif r == top and c == right:
row.append(wall_tiles["WALL_NE"])
continue
elif r == bottom and c ==left:
row.append(wall_tiles["WALL_SW"])
continue
elif r == bottom and c == right:
row.append(wall_tiles["WALL_SE"])
continue
if r == top or r == bottom:
if c > left and c < right:
row.append(wall_tiles["WALL_HORIZONTAL"])
continue
if r > top and r < bottom:
if c == left or c == right:
row.append(wall_tiles["WALL_VERTICAL"])
continue
row.append(" ")
room.append(row)
self.room = room
def get_room(self) -> list:
return self.list
class PlayGround:
def __init__(self, geometry: tuple):
self.rooms: list = []
for r in range(geometry[0]):
row: list = []
for c in range(geometry[1]):
row.append(self.create_room(geometry = (20, 10)))
self.rooms.append(row)
def create_room(self, geometry) -> list:
return Room(geometry).room
def print_room(self, room) -> None:
pass
if __name__ == "__main__":
pg = PlayGround(geometry = (1, 3))
line: str = ""
for r in pg.rooms[0]:
for c in r:
for b in c:
line = line + html.unescape(b)
line = line + "\n"
print(line)