0% found this document useful (0 votes)
11 views2 pages

Editor 1

This document is a Python script that implements a simple sprite game editor using Pygame. It allows users to load sprites from a folder, place them on a grid, and save or load the grid layout to and from a JSON file. The editor features a sprite palette at the top and a grid for arranging sprites below it.

Uploaded by

Gregory Serwadda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Editor 1

This document is a Python script that implements a simple sprite game editor using Pygame. It allows users to load sprites from a folder, place them on a grid, and save or load the grid layout to and from a JSON file. The editor features a sprite palette at the top and a grid for arranging sprites below it.

Uploaded by

Gregory Serwadda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import pygame

import sys
import json
import os

# Constants
WIDTH, HEIGHT = 800, 600
TILE_SIZE = 64
TILE_PALETTE_HEIGHT = TILE_SIZE

# Initialize Pygame
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Sprite Game Editor")

# Load sprites
sprite_folder = "sprites" # Folder where sprites are located
sprites = []
sprite_names = []

# Load sprites from the sprites folder


for filename in os.listdir(sprite_folder):
if filename.endswith('.png'):
sprite_names.append(filename)
sprites.append(pygame.image.load(os.path.join(sprite_folder,
filename)).convert_alpha())

selected_sprite = 0

# Create a grid
grid_width = WIDTH // TILE_SIZE
grid_height = (HEIGHT - TILE_PALETTE_HEIGHT) // TILE_SIZE
grid = [[-1 for _ in range(grid_width)] for _ in range(grid_height)]

# Function to draw the sprite palette


def draw_palette():
for i, sprite in enumerate(sprites):
screen.blit(sprite, (i * TILE_SIZE, 0))
pygame.draw.rect(screen, (255, 255, 255), (i * TILE_SIZE, 0, TILE_SIZE,
TILE_SIZE), 1)

# Function to draw the grid


def draw_grid():
for y in range(grid_height):
for x in range(grid_width):
sprite_index = grid[y][x]
if sprite_index != -1:
screen.blit(sprites[sprite_index], (x * TILE_SIZE, y * TILE_SIZE +
TILE_PALETTE_HEIGHT))
pygame.draw.rect(screen, (200, 200, 200), (x * TILE_SIZE, y * TILE_SIZE
+ TILE_PALETTE_HEIGHT, TILE_SIZE, TILE_SIZE), 1)

# Function to save the level to a file


def save_level(filename):
with open(filename, 'w') as f:
json.dump(grid, f)

# Function to load the level from a file


def load_level(filename):
global grid
with open(filename, 'r') as f:
grid = json.load(f)

# Main loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: # Left mouse button
x, y = event.pos
if y < TILE_PALETTE_HEIGHT: # Clicked in the palette
selected_sprite = x // TILE_SIZE
else: # Clicked on the grid
grid[(y - TILE_PALETTE_HEIGHT) // TILE_SIZE][x // TILE_SIZE] =
selected_sprite
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_s: # Save level on 'S' key press
save_level('level.json')
elif event.key == pygame.K_l: # Load level on 'L' key press
load_level('level.json')

screen.fill((255, 255, 255)) # Clear screen with white


draw_palette() # Draw sprite palette
draw_grid() # Draw grid with placed sprites
pygame.display.flip() # Update display

You might also like