nj0/gui/map.py

41 lines
1.2 KiB
Python
Raw Permalink Normal View History

2024-04-03 22:20:31 +00:00
import pygame as pg
import numpy as np
import xml.etree.ElementTree as ET
class Map:
def __init__(self, world):
self.world = world
self.cam = np.array([0.0, 0.0])
self.data = self.load()
self.tileset = pg.image.load('data/tilemap.png')
def draw(self, screen):
# MAP
for i in range(0, 64):
for j in range(0, 64):
k = i * 64 + j
index = int(self.data[k]) - 1
self.draw_tile(screen, (j*32, i*32), index)
# PLAYER
player = self.world.get_player()
self.cam = (player.pos[0] - screen.get_width()/2,
player.pos[1] - screen.get_height()/2)
pg.draw.rect(screen, 'white', (
player.pos[0] - player.size[0]/2.0 - self.cam[0],
player.pos[1] - player.size[1]/2.0 - self.cam[1],
player.size[0],
player.size[1]
))
def draw_tile(self, screen, pos, index):
p = (pos[0] - self.cam[0], pos[1] - self.cam[1])
screen.blit(self.tileset, p, (index * 32, 0, 32, 32))
def load(self):
tree = ET.parse('data/map.tmx')
root = tree.getroot()
csv = root[1][0]
return csv.text.split(',')