本文整理汇总了Python中map.Map.newLayer方法的典型用法代码示例。如果您正苦于以下问题:Python Map.newLayer方法的具体用法?Python Map.newLayer怎么用?Python Map.newLayer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类map.Map
的用法示例。
在下文中一共展示了Map.newLayer方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: loadMap
# 需要导入模块: from map import Map [as 别名]
# 或者: from map.Map import newLayer [as 别名]
def loadMap(self, id):
doc = parse(os.path.join(self.root, 'maps', id))
xmap = doc.documentElement
assert xmap.tagName == 'map', "Not a valid TMX file"
assert xmap.getAttribute('version') == '1.0', "Incompatible TMX file version"
assert xmap.getAttribute('orientation') == "orthogonal", "Only Orthogonal maps are supported"
tileSize = (int(xmap.getAttribute('tilewidth')),
int(xmap.getAttribute('tileheight')))
size = (int(xmap.getAttribute('width')),
int(xmap.getAttribute('height')))
map = Map(size, tileSize)
tilesets = []
for xtileset in xmap.getElementsByTagName('tileset'):
gap = int(xtileset.getAttribute('spacing') or 0)
margin = int(xtileset.getAttribute('margin') or 0)
imageSource = xtileset.getElementsByTagName('image')[0].getAttribute('source')
ts = self.loadTileSet(xtileset.getAttribute('name'),
os.path.basename(imageSource), tileSize,
(gap, gap), (margin, margin))
tilesets.append({
'gid': int(xtileset.getAttribute('firstgid') or 1),
'tileset': ts
})
tilesets.sort(key=lambda a: a['gid'])
for xlayer in xmap.getElementsByTagName('layer'):
layer = map.newLayer(name=xlayer.getAttribute('name'))
xdata = xlayer.getElementsByTagName('data')[0]
encoding = xdata.getAttribute('encoding') or 'csv'
compression = xdata.getAttribute('compression') or ''
assert encoding in ('base64', 'csv'), "Non-supported layer data encoding scheme"
if encoding == 'base64':
assert compression in ('', 'zlib'), "Invalid layer data compression scheme"
nodes = []
for n in xdata.childNodes:
if n.nodeType == n.TEXT_NODE:
nodes.append(n)
strdata = [node.data for node in nodes]
data = base64.decodestring(''.join(strdata))
if compression == 'zlib':
data = zlib.decompress(data)
if encoding == 'csv':
nodes = []
for n in xdata.childNodes:
if n.nodeType == n.TEXT_NODE:
nodes.append(n)
strdata = [node.data for node in nodes]
data = ''.join(strdata).replace(',', ' ').split()
index = 0
for r in range(map.rows):
for c in range(map.columns):
if encoding == 'csv':
gid = int(data[index])
index += 1
elif encoding == 'base64':
gid = struct.unpack_from('<I', data, index)[0]
index += struct.calcsize('<I')
for ts in reversed(tilesets):
if ts['gid'] <= gid:
layer.getCell(c, r).tile = ts['tileset'][gid - ts['gid']]
break
return map