Platform game in detail: part.1

If we want to understand the platform game Climber, we better destructurate it, starting from the map drawing. This game uses the pygame libraries and started from an example of a tutorial by dafluffypotato. Then I changed many stuffs in the code. Let’s see what.

The map is made using characters that are then substituted by an image. The character is the “w” that correspond to some bricks of a wall.

This is the map in characters and the actual map one next to another.

You can see how the first resemble the second, so it is pretty easy to create it, when you got just one tile, of course. As you add more tiles, it would become more hard, and we should find an alternative method, maybe.

I used a multiline string for the scheme of the map, so that is very easy to see how the map will be. To use a multiline string, simply use “”” at the start and “”” at the end (or ”’ and ”’). Then I use splitlines so that very line becomes a string included in a list of string.

I can now iterate the list with the map divided in single line string to show it.

for line in map1:
    print(line)

>>> [output]:
wwwww  www   www  ww   ww  ww
w             w             w
w                           w
w    wwwwww    wwwww      www
www    w                  w w
w      w          w         w
w   wwwww     wwwwwww      ww
w      wwwwww     w     w   w
w    w      w   www  wwww   w
w      w          w     w   w
w   wwwwww www wwww     w   w
w     w              ww w   w
w                           w
wwwwwwwwwwwwwwwwwwwwwwwwwwwww

 

from pygame.locals import *
import pygame
import glob


map1 ="""                             
                             
wwwww  www   www  ww   ww  ww
w             w             w
w                           w
w    wwwwww    wwwww      www
www    w                  w w
w      w          w         w
w   wwwww     wwwwwww      ww
w      wwwwww     w     w   w
w    w      w   www  wwww   w
w      w          w     w   w
w   wwwwww www wwww     w   w
w     w              ww w   w
w                           w
wwwwwwwwwwwwwwwwwwwwwwwwwwwww"""


def init_display():
    global screen, tile
    screen = pygame.display.set_mode((462, 256))
    tile = pygame.image.load("imgs\\tiles\\wall.png")


def tiles(map1):
    global tile
    for y, line in enumerate(map1):
        for x, c in enumerate(line):
            if c == "w":
                screen.blit(tile, (x * 16, y * 16))


map1 = map1.splitlines()
pygame.init()
init_display()
loop = 1
while loop:

    # screen.fill((0, 0, 0))
    tiles(map1)
    for event in pygame.event.get():
        if event.type == QUIT:
            loop = 0

    pygame.display.update()
pygame.quit()

Map to explain the code

This is the output

Video explanation of how to make a map in Pygame

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.