Merge pull request '0.0.1' (#1) from 0.0.1 into main

Reviewed-on: #1
This commit is contained in:
2025-03-20 06:31:27 +01:00
25 changed files with 131 additions and 1 deletions

View File

@@ -1,2 +1,29 @@
# PyGame-Snake
![PyGame](./pygame_logo.png)
# 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>&#8594;</kbd>
Venstre - <kbd>a</kbd> eller <kbd>&#8592;</kbd>
Op - <kbd>w</kbd> eller <kbd>&#8593;</kbd>
Ned - <kbd>s</kbd> eller <kbd>&#8595;</kbd>
Alternativt, kan et joystick :joystick: eller gamepad :video_game: bruges.

BIN
gfx/LoseScreen.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 MiB

BIN
gfx/StartScreen.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 MiB

BIN
gfx/Textures.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 146 KiB

BIN
gfx/Vignette.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 MiB

BIN
gfx/WinScreen.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 MiB

5
main.py Normal file
View 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

Binary file not shown.

After

Width:  |  Height:  |  Size: 129 KiB

1
tilemap/__init__.py Normal file
View File

@@ -0,0 +1 @@
from . import playground

97
tilemap/playground.py Normal file
View 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": "&#9556;", "WALL_HORIZONTAL": "&#9552;",
"WALL_NE": "&#9559;", "WALL_VERTICAL": "&#9553;",
"WALL_SW": "&#9562;", "WALL_SE": "&#9565;",
"WALL_TOP_LEFT_OPENING": "&#9563;", "WALL_TOP_RIGHT_OPENING": "&#9560;",
"WALL_BOTTOM_LEFT_OPENING": "&#9557;", "WALL_BOTTOM_RIGHT_OPENING": "&#9554;",
"WALL_LEFT_UPPER_OPENING": "&#9564;", "WALL_LEFT_LOWER_OPENING": "&#9558;",
"WALL_RIGHT_UPPER_OPENING": "&#9561;", "WALL_RIGHT_LOWER_OPENING": "&#9555;"}
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

Binary file not shown.

After

Width:  |  Height:  |  Size: 632 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 448 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 436 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 165 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 423 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 461 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 171 B

BIN
tilemap/tiles/head_down.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 680 B

BIN
tilemap/tiles/head_left.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 711 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 671 B

BIN
tilemap/tiles/head_up.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 688 B

BIN
tilemap/tiles/tail_down.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 337 B

BIN
tilemap/tiles/tail_left.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 359 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 338 B

BIN
tilemap/tiles/tail_up.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 342 B