Menu
CoddyTech

Build the Caves

Part of the Worlds section of Coddy's Pygame journey. Lesson 28 of 35.

Moss Caves is a platformer of your own: run through a mossy cave, collect coins, jump spikes and pits, stomp blobs, find the key, open the door and reach the flag. Each step adds one part, built from what you made in this section.

The whole level is the map in the starter, 64 columns wide. # is ground, P the start, and the other letters are things you will add: C coins, ^ spikes, E blobs, K the key, D the door, F the flag. Until a step reads a letter, its tile stays empty.

First, build the ground. One pass over the map fills walls with Rects for collisions and tiles with (image, Rect) pairs for drawing, choosing grass, dirt or an edge tile with solid(), as in the Tiles chapter:

rect = pygame.Rect(col * TILE, row * TILE, TILE, TILE)
if char == "#":
    # pick image with solid(), as before
    walls.append(rect)
    tiles.append((image, rect))
elif char == "P":
    start = rect.midbottom

The player stands on start, the bottom middle of the P tile.

challenge icon

Challenge

Medium
  1. Before the game loop, make walls and tiles, then loop over the map. For each #, pick dirt_img, grass_left_img, grass_right_img or grass_img with solid() as in Edge Tiles, and append the Rect to walls and the pair to tiles.
  2. For P, set start to the tile's midbottom.
  3. Make player = player_img.get_rect(midbottom=start).
  4. In the game loop, after the background, blit every tile at its Rect, then the player.

Try it yourself

import pygame

TILE = 36
WIDTH, HEIGHT = 576, 360

LEVEL = [
    "................................................................",
    "................................................................",
    "................................................................",
    "................................................................",
    "#..............................................................#",
    "#....................C.........................#####...........#",
    "#......CC...C................CC.....CC.........#####.....CC....#",
    "#....................#.........................D...............#",
    "#.P.........#...E....#...^.......K......E......D.C....^......F.#",
    "#######..####################..##########################..#####",
]

pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Moss Caves")
clock = pygame.time.Clock()


def load(name):
    return pygame.image.load("assets/images/" + name).convert_alpha()


background = pygame.image.load("assets/backgrounds/forest.png").convert_alpha()
grass_img = load("grass.png")
grass_left_img = load("grass_left.png")
grass_right_img = load("grass_right.png")
dirt_img = load("dirt.png")
player_img = load("player.png")


def solid(row, col):
    if row < 0 or row >= len(LEVEL) or col < 0 or col >= len(LEVEL[0]):
        return False
    return LEVEL[row][col] == "#"


running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    for y in range(0, HEIGHT, 72):
        for x in range(0, WIDTH, 72):
            screen.blit(background, (x, y))
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Worlds

Practice on your own: Pygame playground