Menu
CoddyTech

A Tiled Background

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

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

The starter loads assets/backgrounds/clouds.png into background.

  1. In the game loop, instead of screen.fill, blit background at every x from 0 to 576 in steps of 72, for every y from 0 to 360 in steps of 72.
  2. Draw the tiles after the background.

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()

All lessons in Worlds

Practice on your own: Pygame playground