Pygame Platform in detail 2: Map editor 1.0

I made this little program to create the maps visually with Pygame itself, rather than doing it as a list of characters. Now you can click to add or delete walls on the screen. With this tool it is more easy and fun build the different maps of the game. If you press ‘s‘ you will save the map as a png, because we do want to use a png as map instead of simple tiles alligned on the screen. In this way, we will see how in the next episode of this serie, i will explain how to make some video-enhancements in a simple way to make the map nicer and more fun to play with.

The program in action

This is the code of this first version of the editor. Maybe, if someone is interested, I could explain how this works.

from pygame.locals import *
import pygame
import os


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"""


print(29*16, 16*16) # 462 256
def init_display():
    global screen, tile, display, WINDOW_SIZE
    WINDOW_SIZE = (464*2, 256*2)
    screen = pygame.display.set_mode(WINDOW_SIZE, 0, 32)
    display = pygame.Surface((WINDOW_SIZE[0] // 2, WINDOW_SIZE[1] // 2))
    tile = pygame.image.load("imgs\\tiles\\wall.png")

def mixer_init():
    "Initialize sounds avoiding delay"
    pygame.mixer.pre_init(44100, -16, 1, 512)
    pygame.init()
    pygame.mixer.quit()
    pygame.mixer.init(22050, -16, 2, 512)
    pygame.mixer.set_num_channels(32)

def mk_sounds_dic():
    "Loads the sounds for the game"
    sounds_list = ("jump",)
    sound_dic = {}
    for sound in sounds_list:
        sound_path = f"audio\\{sound}.wav"
        print(sound_path)
        sound_dic[sound] = pygame.mixer.Sound(sound_path)
    return sound_dic

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

def mk_tile_rects():
    "Makes the Rects for the 'physics'"
    tile_rects = []
    y = 0
    for line_of_symbols in map1:
        x = 0
        for symbol in line_of_symbols:
            if symbol == "w":
                tile_rects.append(pygame.Rect(x * 16, y * 16, 16, 15))
            x += 1
        y += 1
    return tile_rects


def map_to_list(map1):
    map1 = map1.splitlines()
    map2 = []
    for n, line in enumerate(map1):
        map2.append(list(map1[n]))
    return map2


map1 = map_to_list(map1)

pygame.init()
init_display()
mixer_init()
sounds_dic = mk_sounds_dic()
tile_rects = mk_tile_rects()
loop = 1
font = pygame.font.Font(None, 25)
x, y = pygame.mouse.get_pos()
last_posx = x
last_posy = y
letter = "spazio"
while loop:

    display.fill((0, 0, 0))
    tiles(map1)
    for event in pygame.event.get():
        if event.type == QUIT:
            loop = 0
        if event.type == pygame.KEYDOWN:
            if event.key == K_s:
                pygame.image.save(screen, "map2.png")
                os.startfile("map2.png")
        if pygame.mouse.get_pressed()[0]:
            x, y = pygame.mouse.get_pos()
            x = int(x / 32)
            y = int(y / 32)
            # Inverte da tile a vuoto
            if (last_posx, last_posy) != (x, y):
                pygame.mixer.Sound.play(sounds_dic['jump'])
                if map1[y][x] == "w":
                    map1[y][x] = " "
                    letter = "w"
                else:
                    map1[y][x] = "w"
                    letter = "-"
            last_posx = x
            last_posy = y

    pos = font.render(f"{x},{y}: {letter}", 1, pygame.Color("white"))
    display.blit(pos, (0, 0))
    screen.blit(pygame.transform.scale(display, WINDOW_SIZE), (0, 0))
    pygame.display.update()
pygame.quit()

This is an example… very basic of how you could enhance the map…

This is made using as a base the image saved with the map and then modified with a graphic program. Then we will use this as the map in the game, having the pygame.Rect as the object with their position to be used to check the collitions. In the next video we will see how to make this rectangles.

New version

You can find here the new version of this program,

Map editor 1.2 – Devlog

Devlog videos of the next versions

Version 1.1

Version 1.2


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.