Menu
CoddyTech

One Screen at a Time

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

A level can be wider than the window. This map has 32 columns, two windows of 16. The walls keep their level positions: a wall in column 20 is at x = 720, past the window's right edge at 576.

The level's width comes from the map: len(LEVEL[0]) * TILE. Keep the player inside the level, not inside the window:

level_width = len(LEVEL[0]) * TILE
level = pygame.Rect(0, 0, level_width, 360)
# in the loop
player.clamp_ip(level)

To show the part of the level the player is in, split it into screens of 576 pixels. player.centerx // 576 is the screen number, 0 or 1. Draw everything moved left by that many window widths:

offset = player.centerx // 576 * 576
pygame.draw.rect(screen, WALL, wall.move(-offset, 0))

wall.move(-offset, 0) is a copy of the Rect moved left; the wall itself stays where it is in the level. Walk past the edge and the window shows the next screen. The player still passes through walls: stopping at them comes in Platform Collisions.

challenge icon

Challenge

Medium
  1. Before the game loop, make level_width = len(LEVEL[0]) * TILE and level = pygame.Rect(0, 0, level_width, 360).
  2. Keep the player inside level instead of screen.get_rect().
  3. Each frame, set offset = player.centerx // 576 * 576.
  4. Draw every wall, coin and the player moved by -offset on x.

Try it yourself

import pygame

TILE = 36
WALL = (120, 90, 60)
COIN = (255, 196, 0)
PLAYER = (80, 200, 120)

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

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

walls = []
coins = []
for row, line in enumerate(LEVEL):
    for col, char in enumerate(line):
        if char == "#":
            walls.append(pygame.Rect(col * TILE, row * TILE, TILE, TILE))
        elif char == "C":
            coins.append(pygame.Rect(col * TILE + 10, row * TILE + 10, 16, 16))
        elif char == "P":
            player = pygame.Rect(col * TILE + 4, row * TILE + 4, 28, 28)

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

    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        player.x -= 4
    if keys[pygame.K_RIGHT]:
        player.x += 4
    if keys[pygame.K_UP]:
        player.y -= 4
    if keys[pygame.K_DOWN]:
        player.y += 4
    player.clamp_ip(screen.get_rect())

    screen.fill((30, 30, 46))
    for wall in walls:
        pygame.draw.rect(screen, WALL, wall)
    for coin in coins:
        pygame.draw.rect(screen, COIN, coin)
    pygame.draw.rect(screen, PLAYER, player)
    pygame.display.flip()
    clock.tick(60)

pygame.quit()

All lessons in Worlds

Practice on your own: Pygame playground