Pygame Editor 1.4: use the map in a real game (1)

The new version 1.4 of the Pygame map editor comes with the first attempt to use it into an actual game called flash. Let’s see what is this about.

We will use the map stored into a game… isn’t it cool?

The video of the map maker and the game that uses the map

The code to get the map created into the game

The game is not yet ready, but with this code we take a pickle file created with the map editor and show the map in the future game. Pratically we:

  • load the tiles from imgs2 folder (load_images())
  • load the pickle file with the map
  • show the map with the tiles on the screen with show_map that iterates the list of letters and substitute each letter with a tiles in the posizion x * 16, y * 16, being each tile of 16×16 pixels (not all of them, but the most part)

 

# theflash
import pygame
from pygame.locals import *
import pickle
import os
import sys
from glob import glob


pygame.init()
WSIZE = (928, 512)
screen = pygame.display.set_mode((WSIZE))
W, H = WSIZE
display = pygame.Surface((W // 2, H // 2))
clock = pygame.time.Clock()



def load_images(folder: str) -> list:
    "Load tiles from a folder... with a number at the end"
    listtiles2 = [x for x in glob(folder + "2\\*.png")]
    tile2 = [pygame.image.load(x) for x in listtiles2]
    return tile2


def load_map(filename: str, mp1: list):
    "Resume a list with the data (letter) for the tiles to be displayed on display surface"
    if filename in os.listdir():
        with open(filename, "rb") as file:
            mp = pickle.load(file)
    else:
        mp = mp1
    return mp


def showmap(mp1):
    "Take the map list with letters and blit them as tiles on the display surface"
    for y, line in enumerate(mp1):
        for x, c in enumerate(line):
            for n, l in enumerate(letters):
                if c == l:
                    display.blit(tile[n], (x * 16, y * 16))


# calls the function to load tiles from imgs2/
tile = load_images("imgs")
# create a list of all letters corrisponding to the tiles with images from imgs2 folder
alphab = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm.,:;@#°^[]<>()&%$£€ABC1234567890òàèéù+-ì={}§!?/|"
letters = [x for x in alphab[0:len(tile)]]
map2 = []
# Stores a list with letters = tiles from pkl file (created with pygame map editor)
map2 = load_map("last_map2.pkl", map2)


loop = 1
# This blit all the tiles on the display surface
showmap(map2)
while loop:
    for event in pygame.event.get():
        if event.type == QUIT:
            loop = 0
        if event.type == pygame.KEYDOWN:
            if event.key == K_ESCAPE:
                loop = 0
    # the display surface is scaled as the screen (being the half of screen size)
    screen.blit(pygame.transform.scale(display, WSIZE), (0, 0))
    pygame.display.update()
    clock.tick(60)

pygame.quit()
sys.exit()

 


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

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.