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.midbottomThe player stands on start, the bottom middle of the P tile.
Challenge
Medium- Before the game loop, make
wallsandtiles, then loop over the map. For each#, pickdirt_img,grass_left_img,grass_right_imgorgrass_imgwithsolid()as in Edge Tiles, and append the Rect towallsand the pair totiles. - For
P, setstartto the tile'smidbottom. - Make
player = player_img.get_rect(midbottom=start). - 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()
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Worlds
1Maps from Text
A Level in TextRows and ColumnsLetters With MeaningsWhere the Player StartsOne Screen at a Time3Gravity and Jumping
Falling With GravityJump From the GroundShort HopsA Top Falling SpeedJump BufferPractice on your own: Pygame playground