Tutorial: Labyrinth-Spiel#
In diesem Kapitel erstellen wir Schritt für Schritt gemeinsam ein Labyrinth-Spiel.
The technique of creating a tilemap is common in many games. Once you have learned it here, you will be able to integrate this technique into your own projects.
Basierend auf:
https://github.com/electronstudio/pygame-zero-book
Lizenz: Namensnennung-NichtKommerziell-Weitergabe unter gleichen Bedingungen 4.0 International
Schritt 1: Actor aus einer Kachelkarte laden#
A tile map uses a small number of images (tiles) that are drawn multiple times to create a large game level (the map). This saves work in creating many graphics and makes it easier to make changes to the level design. In this example, we create a maze level.
We need three image files: player.png
, wall.png
. These must be stored in the mu_code/images
folder.
my_code
|
|--images
|----player.png
|----wall.png
Now we can program the framework for our game:
Create a world#
Use this framework for your game:
In Zeile 2 wird eine TiledWorld
erstellt, die die Logik für gekachelte Welten bereitstellt. In der letzten Zeile musst du unbedingt world.run()
aufrufen, um das Spiel zu starten.
from miniworlds import *
world = TiledWorld(8, 8)
world.tile_size = 64
world.add_background((0, 0, 0, 255))
# Dein Code hier
world.run()
Create Actor subclasses#
Create Actor-Subklassen für jeden Typ von Actor:
class Player(Actor):
def on_setup(self):
self.add_costume("player")
self.layer = 1
class Wall(Actor):
def on_setup(self):
self.add_costume("wall")
self.add_costume
fügt dem Actor ein Kostüm hinzu, das auf einem Bild basiert (z. B. “player”, “wall” – Dateiendungen wie .png
oder .jpeg
können weggelassen werden) oder auf einer Farbe, die als (r, g, b, a)-Tupel angegeben wird.
Create a tile map#
A tile map is a 2D list that determines the positions of the actors.
0: Leer
1: Wand
2: Spieler
tiles = [None, Wall, Player]
maze = [
[1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 1, 2, 0, 1],
[1, 0, 1, 0, 1, 1, 0, 1],
[1, 0, 1, 0, 0, 0, 0, 1],
[1, 0, 1, 0, 1, 0, 0, 1],
[1, 0, 1, 0, 1, 0, 0, 1],
[1, 0, 1, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1]
]
Create objects for each cell of the tile map#
For each cell of the tile map, an actor can be created. The class for each actor is retrieved from the tile map.
@world.register
def on_setup(self):
for row in range(len(maze)):
for column in range(len(maze[row])):
actor_cls = tiles[maze[row][column]]
if actor_cls:
actor_cls(column, row)
Step 2: Bewegung#
Move player#
Add the following code to the Player
class to move the player:
class Player(Actor):
def on_setup(self):
self.add_costume("player")
self.layer = 1
def on_key_down(self, keys):
if "UP" in keys:
self.y -= 1
elif "DOWN" in keys:
self.y += 1
elif "LEFT" in keys:
self.x -= 1
elif "RIGHT" in keys:
self.x += 1
Block movement#
Use the method move_back()
to block the player’s movement when they hit a wall:
def on_key_down(self, keys):
if "UP" in keys:
self.y -= 1
elif "DOWN" in keys:
self.y += 1
elif "LEFT" in keys:
self.x -= 1
elif "RIGHT" in keys:
self.x += 1
if self.detect_actor(Wall):
self.move_back()
Create an opponent#
Create an enemy class that hunts the player:
class Enemy(Actor):
def on_setup(self):
self.add_costume("enemy")
self.velocity = 1
self.layer = 1
def act(self):
self.y += self.velocity
if self.detect_actor(Wall):
self.move_back()
self.velocity = -self.velocity
if self.detect_actor(Player):
print("Du wurdest gefangen!")
exit()
Zusammenfassung#
In diesem Tutorial hast du ein einfaches Labyrinth-Spiel mit beweglichen Actorn und Kachelkarten erstellt. Mit dieser Grundlage kannst du weitere Funktionen wie Punkte, Levelaufstiege oder komplexere Gegner hinzufügen!