Python Forum
[PyGame] Tileset Slpiting - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Game Development (https://python-forum.io/forum-11.html)
+--- Thread: [PyGame] Tileset Slpiting (/thread-16974.html)



Tileset Slpiting - Zman350x - Mar-22-2019

I want to use a tileset in my game (the image is an example) how would I go about splitting it?
https://ibb.co/Kb4YS4s
I want it to end up something like this:
import pygame
from pygame.locals import *
pygame.init()
Tileset = pygame.image.load('tileset.png')
Crate = <the part of the image with the crate>
Grass = <The grass above the crate>
I want to be able to save different parts of the image as separate images within the code. Is this possible? (Please keep simple if possible)

EDIT: Sorry for typo in title, should be Tileset SPLITTING


RE: Tileset Slpiting - metulburr - Mar-23-2019

How you split it depends on how the images are formatted on the spritesheet. If they are uniformed, you can loop through and pull each one, but if they are random sizes or all over the place you are gong to have to get the coordinates for each image.

We have a tutorial for this process
https://python-forum.io/Thread-PyGame-Loading-images-transparency-handling-spritesheets-part-2

Here are the functions that pull them
def strip_from_sheet(sheet, start, size, columns, rows):
    frames = []
    for j in range(rows):
        for i in range(columns):
            location = (start[0]+size[0]*i, start[1]+size[1]*j)
            frames.append(sheet.subsurface(pg.Rect(location, size)))
    return frames
def strip_coords_from_sheet(sheet, coords, size):
    frames = []
    for coord in coords:
        location = (coord[0]*size[0], coord[1]*size[1])
        frames.append(sheet.subsurface(pg.Rect(location, size)))
    return frames
The image you show seems to not be uniformed. They seemed to be put them all over the place. Some almost look uniformed, but are a few pixels off even. Unfortunately it can take a bit of time figuring out the coords of each image. But once you have it, your done. I might even open it in GIMP and realign images of the same size to a uniformed spritesheet, and others to a non-uniformed one. Or i would write a piece of code to load each suspected one as i figure out the coords to make sure it aligns properly.