class qTile(qGameObject): cord = qVec2(0, 0) tileSize = qVec2(48, 48) def pos(self): return qVec2(self.cords) * self.tileSize def getPos(self): if self.parent == None: return self.pos() return self.pos() + self.parent.getPos()
def Render(self, screen): scale = self.scale cc = screen.center self.ca = ca = cc - qVec2(self.width, self.height) * scale / 2. self.cb = cb = cc + qVec2(self.width, self.height) * scale / 2. for w in range(self.width + 1): x = ca[0] + w * scale screen.line(qCyan, (x, ca[1]), (x, cb[1])) for h in range(self.height + 1): y = ca[1] + h * scale screen.line(qCyan, (ca[0], y), (cb[0], y)) for y in range(self.height): for x in range(self.width): g = self.grid[y * self.width + x] r = ca + qVec2(x, y) * scale if g != -1: screen.rect(colours[g], (r[0] + 1, r[1] + 1, scale - 2, scale - 2))
class qTileLevel(qGameObject): size = qVec2(0, 0) def __init__(self, tileSize, size): self.tileLayers = {} self.tileSize = tileSize self.size = qVec2(size) * tileSize def add(self, child, depth=0, **kwargs): if isinstance(child, qTileLayer): if self.tileLayers.__contains__(depth) \ and self.tileLayers[depth] != None: self.tileLayers[depth].removeMe() self.tileLayers[depth] = child child.layerDepth = depth qGameObject.add(self, child, depth, **kwargs)
def readFile(self, levelFile): from quark.properties import level_folder data = json.load(open(level_folder + levelFile)) level = qTileLevel(qVec2(48, 48), data['size']) for dataObj in self.levelDataObjects.keys(): if data.__contains__(dataObj): objData = data[dataObj] if type(data[dataObj]) == list: for x in objData: self.initLevelDataObj(dataObj, x) else: self.initLevelDataObj(dataObj, objData) for lKey in filter(lambda x: x.startswith('#Layer'), data.keys()): layerIndex = int(lKey[len('#Layer'):]) tlayer = qTileLayer(level) for (j, row) in enumerate(data[lKey]): for (i, s) in enumerate(row.split(' ')): tileInstant = self.getSymbolData(str(s)) if tileInstant: tlayer.add(tileInstant, (i, j)) level.add(tlayer, layerIndex) return level
def pos(self): return qVec2(self.cords) * self.tileSize
def __init__(self, tileSize, size): self.tileLayers = {} self.tileSize = tileSize self.size = qVec2(size) * tileSize