29
README.md
@@ -1,2 +1,29 @@
|
||||
# PyGame-Snake
|
||||

|
||||
|
||||
# Snake
|
||||
|
||||
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 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.
|
||||
|
||||
Hvis du spiser føden, forlænges slangen.
|
||||
|
||||
Målet er, at overleve så længe som muligt, samt at få så mange point som muligt.
|
||||
|
||||
Én strategi kunne være, at undgå at spise, og derved forblive samme længde, men så får du kun point for tiden.
|
||||
|
||||
Hvis du derimod spiser, fordobles dine tidspoint, for hver gang du spiser. Derved kan du opnå mange flere point end ved ikke at spise.
|
||||
|
||||
## Kontrol
|
||||
Højre - <kbd>d</kbd> eller <kbd>→</kbd>
|
||||
Venstre - <kbd>a</kbd> eller <kbd>←</kbd>
|
||||
Op - <kbd>w</kbd> eller <kbd>↑</kbd>
|
||||
Ned - <kbd>s</kbd> eller <kbd>↓</kbd>
|
||||
|
||||
Alternativt, kan et joystick :joystick: eller gamepad :video_game: bruges.
|
BIN
gfx/LoseScreen.png
Normal file
After Width: | Height: | Size: 1.6 MiB |
BIN
gfx/StartScreen.png
Normal file
After Width: | Height: | Size: 2.1 MiB |
BIN
gfx/Textures.png
Normal file
After Width: | Height: | Size: 146 KiB |
BIN
gfx/Vignette.png
Normal file
After Width: | Height: | Size: 1.2 MiB |
BIN
gfx/WinScreen.png
Normal file
After Width: | Height: | Size: 1.7 MiB |
5
main.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from tilemap.playground import PlayGround
|
||||
|
||||
if __name__ == "__main__":
|
||||
pg = PlayGround(geometry = (1, 3))
|
||||
pass
|
BIN
pygame_logo.png
Normal file
After Width: | Height: | Size: 129 KiB |
1
tilemap/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from . import playground
|
97
tilemap/playground.py
Normal file
@@ -0,0 +1,97 @@
|
||||
"""
|
||||
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)
|
BIN
tilemap/tiles/apple.png
Normal file
After Width: | Height: | Size: 632 B |
BIN
tilemap/tiles/body_bottomleft.png
Normal file
After Width: | Height: | Size: 448 B |
BIN
tilemap/tiles/body_bottomright.png
Normal file
After Width: | Height: | Size: 436 B |
BIN
tilemap/tiles/body_horizontal.png
Normal file
After Width: | Height: | Size: 165 B |
BIN
tilemap/tiles/body_topleft.png
Normal file
After Width: | Height: | Size: 423 B |
BIN
tilemap/tiles/body_topright.png
Normal file
After Width: | Height: | Size: 461 B |
BIN
tilemap/tiles/body_vertical.png
Normal file
After Width: | Height: | Size: 171 B |
BIN
tilemap/tiles/head_down.png
Normal file
After Width: | Height: | Size: 680 B |
BIN
tilemap/tiles/head_left.png
Normal file
After Width: | Height: | Size: 711 B |
BIN
tilemap/tiles/head_right.png
Normal file
After Width: | Height: | Size: 671 B |
BIN
tilemap/tiles/head_up.png
Normal file
After Width: | Height: | Size: 688 B |
BIN
tilemap/tiles/tail_down.png
Normal file
After Width: | Height: | Size: 337 B |
BIN
tilemap/tiles/tail_left.png
Normal file
After Width: | Height: | Size: 359 B |
BIN
tilemap/tiles/tail_right.png
Normal file
After Width: | Height: | Size: 338 B |
BIN
tilemap/tiles/tail_up.png
Normal file
After Width: | Height: | Size: 342 B |