Pygame Map editor update 1.1

One of the fundamental things to develop a game is the map editor. If you do not have something to speed up your work, it would be very hard to make thing nice and fast. Here is the project for a simple map editor. To be continued… this is version 1.1 (6/1/2020).

I made some changes to the editor for a platform game map version 1.0 with python and pygame:

  • create wall with left mouse button
  • delete wall with right mouse button

Now the map building is much faster.

Some images for the tiles

Here are some attempts to make nice bricks for the walls tiles.

You can, as before, continuosly build walls or deleting them by dragging the mouse.

from pygame.locals import *
import pygame
import os


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 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 map_to_list():
    start = "w"*29
    map1 = "w" + " " * 27 + "w\n"
    map1 = start + map1 * 14 + start
    map1 = map1.splitlines()
    map2 = []
    for n, line in enumerate(map1):
        map2.append(list(map1[n]))
    return map2


map1 = map_to_list()

pygame.init()
init_display()
loop = 1
last_pos = x, y = pygame.mouse.get_pos()
letter = "spazio"
num_file = len(os.listdir())
map_name = f"map{num_file}.png"
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, map_name)
                os.startfile(map_name)
            if event.key == K_d:
                map1 = map_to_list()
        if pygame.mouse.get_pressed()[0]:
            x, y = pygame.mouse.get_pos()
            # You divide by 32 for its scaled (pygame.transform.scale)
            x, y = int(x / 32), int(y / 32)
            # x and y are col and row (the opposite)
            row, col = y, x
            map1[row][col] = "w"
        elif pygame.mouse.get_pressed()[2]:
            x, y = pygame.mouse.get_pos()
            # You divide by 32 for its scaled (pygame.transform.scale)
            x, y = int(x / 32), int(y / 32)
            # x and y are col and row (the opposite)
            row, col = y, x
            map1[row][col] = " "

    screen.blit(pygame.transform.scale(display, WINDOW_SIZE), (0, 0))
    pygame.display.update()
pygame.quit()

The video of version 1.1

The video of version 1.0

Here are some maps

pygame map editor for platform game or to edit a background of a stage

Map editor 1.2 – Devlog

Repository https://github.com/formazione/map_editor

video v. 1.2

 

Previous video v. 1.1

 

First video v. 1.0

 

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.