30 lines
1.1 KiB
Python
30 lines
1.1 KiB
Python
import pygame, json
|
|
|
|
class Tilemap:
|
|
def __init__(self, game, tile=16):
|
|
self.game = game
|
|
self.tile_size = tile
|
|
self.tilemap = {}
|
|
|
|
def save(self, path):
|
|
f = open(path, 'w')
|
|
json.dump({'tilemap': self.tilemap, 'tile_size': self.tile_size}, f)
|
|
f.close()
|
|
|
|
def load(self, path):
|
|
f = open(path, 'r')
|
|
map_data = json.load(f)
|
|
f.close()
|
|
|
|
self.tilemap = map_data['tilemap']
|
|
self.tile_size = map_data['tile_size']
|
|
|
|
|
|
def render(self, surf, offset=(0, 0)):
|
|
for x in range(offset[0] // self.tile_size, (offset[0] + surf.get_width()) // self.tile_size + 1):
|
|
for y in range(offset[1] // self.tile_size, (offset[1] + surf.get_height()) // self.tile_size + 1):
|
|
loc = str(x) + ';' + str(y)
|
|
if loc in self.tilemap:
|
|
tile = self.tilemap[loc]
|
|
surf.blit(self.game.assets[tile['type']][tile['variant']], (tile['pos'][0] * self.tile_size - offset[0], tile['pos'][1] * self.tile_size - offset[1]))
|