Reputation: 73
I created a map using tiled and exported as a CSV to my Pygame app. I used a tileset I downloaded from online. So I've loaded in the data as such:
world_data = []
for row in range(ROWS+1):
r = [-1] * (COLS+1)
world_data.append(r)
#LOAD IN LEVEL DATA AND CREATE WORLD
with open("stickquestmap{}.csv".format(level), newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for x, row in enumerate(reader):
for y, tile in enumerate(row):
world_data[x][y] = int(tile)
Now I'm currently making a process_data
method that iterates through the world_data
and blits the individual tiles, but I don't have the tiles saved individually. I just have a PNG containing all the tiles. I'm lost how to solve this issue.
Upvotes: 0
Views: 1039
Reputation: 1874
You can either separate your tiles into separate images, using an image editor like Paint.net or GIMP, or you can do it within pygame itself.
One easy way to "crop" out rectangular parts of a Surface is to use surface.subsurface(Rect)
. See https://www.pygame.org/docs/ref/surface.html#pygame.Surface.subsurface.
Example:
tileset = pygame.image.load("tileset.png").convert()
grass_tile = tileset.subsurface([0,0,32,32])
dirt_tile = tileset.subsurface([32,0,32,32])
Upvotes: 2