Added tilemap. /JL
This commit is contained in:
@@ -2,13 +2,13 @@
|
||||
|
||||
# Snake
|
||||
|
||||
Dette er mit bud på det klassiske spil Snake, lavet i PyGame.
|
||||
Dette er mit bud på det klassiske spil Snake, skrevet i Python, ved hjælp af PyGame.
|
||||
|
||||
## Spillets forløb
|
||||
Slangen bevæger sig med en konstant fart, i én retning. Der er vilkårligt anbragte forhindringer på banen.
|
||||
|
||||
|
||||
Slangen skal styres udenom forhindringerne samt vægge. Du dør hvis du ramme en væg, en forhindring eller dig selv.
|
||||
Slangen skal styres udenom forhindringerne samt vægge. Du dør hvis du rammer en væg, en forhindring eller dig selv.
|
||||
|
||||
Der bliver også lagt føde ud, vilkårlige steder. Føden forsvinder igen, efter 5 sekunder.
|
||||
|
||||
|
0
src/snake.py
Normal file
0
src/snake.py
Normal file
0
src/tilemap/__init__.py
Normal file
0
src/tilemap/__init__.py
Normal file
90
src/tilemap/tilemap.py
Normal file
90
src/tilemap/tilemap.py
Normal file
@@ -0,0 +1,90 @@
|
||||
"""
|
||||
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"]
|
||||
]
|
||||
|
||||
# room_width, room_height = 20, 15 # 20 tiles wide and 15 tiles high
|
||||
|
||||
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 = []
|
||||
self.create_room(geometry)
|
||||
|
||||
def create_room(self, geometry) -> None:
|
||||
self.rooms.append(Room(geometry))
|
||||
|
||||
def print_room(self, room) -> None:
|
||||
pass
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
pg = PlayGround(geometry = (20, 5))
|
||||
|
||||
line = ""
|
||||
for ro in pg.rooms:
|
||||
for r in ro.room:
|
||||
for c in r:
|
||||
line = line + html.unescape(c)
|
||||
line = line + "\n"
|
||||
print(line)
|
Reference in New Issue
Block a user