Menu
CoddyTech

A Tiled Background

Part of the Worlds section of Coddy's Pygame journey. Lesson 9 of 36.

A plain colour behind the level looks empty. assets/backgrounds/clouds.png is a 72 by 72 picture made to repeat: its left edge matches its right edge, and its top matches its bottom. Blit it in a grid that covers the window:

for y in range(0, 360, 72):
    for x in range(0, 576, 72):
        screen.blit(background, (x, y))

range(0, 576, 72) gives 0, 72, 144 and so on up to 504, the last start that still shows. 576 is a multiple of 72, so the last copy ends at the window's edge. The background replaces screen.fill: it covers every pixel.

Order matters. Whatever you blit later covers what is there, so draw the background first, then the tiles on top of it.

challenge icon

Challenge

Easy

Fill the sky with a repeating cloud background.

  1. Instead of screen.fill, blit background (loaded in the starter) at every x from 0 to 576 and every y from 0 to 360, in steps of 72.
  2. Draw the tiles after the background.

Press Play: clouds fill the window behind the level. Then Check.

Try it yourself

import pygame

TILE = 36

LEVEL = [
    "###.............",
    "###.............",
    "..........C.....",
    ".........####...",
    "..C.............",
    ".####......C....",
    "..........###...",
    "#....C.........#",
    "###..##...###.##",
    "######..########",
]

pygame.init()
screen = pygame.display.set_mode((576, 360))
pygame.display.set_caption("Tiles")
clock = pygame.time.Clock()

grass_img = pygame.image.load("assets/images/grass.png").convert_alpha()
grass_left_img = pygame.image.load("assets/images/grass_left.png").convert_alpha()
grass_right_img = pygame.image.load("assets/images/grass_right.png").convert_alpha()
dirt_img = pygame.image.load("assets/images/dirt.png").convert_alpha()
coin_img = pygame.image.load("assets/images/coin.png").convert_alpha()
background = pygame.image.load("assets/backgrounds/clouds.png").convert_alpha()


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] == "#"


tiles = []
for row, line in enumerate(LEVEL):
    for col, char in enumerate(line):
        x, y = col * TILE, row * TILE
        if char == "#":
            if solid(row - 1, col):
                image = dirt_img
            elif not solid(row, col - 1) and solid(row, col + 1):
                image = grass_left_img
            elif solid(row, col - 1) and not solid(row, col + 1):
                image = grass_right_img
            else:
                image = grass_img
            tiles.append((image, pygame.Rect(x, y, TILE, TILE)))
        elif char == "C":
            tiles.append((coin_img, pygame.Rect(x, y, TILE, TILE)))

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill((140, 200, 240))
    for image, rect in tiles:
        screen.blit(image, rect)
    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