Skip to content

Commit

Permalink
fix some typos and clean whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Cotterall committed Jul 16, 2012
1 parent 9bb839b commit dca385b
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 60 deletions.
32 changes: 16 additions & 16 deletions pytmx/pytmx.py
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,6 @@
from itertools import chain, product from itertools import chain, product
from xml.etree import ElementTree from xml.etree import ElementTree
from collections import defaultdict from collections import defaultdict
from utils import decode_gid, types, parse_properties, read_points from utils import decode_gid, types, parse_properties, read_points
from constants import * from constants import *


Expand All @@ -21,7 +21,7 @@ def set_properties(self, node):
""" """


# set the attributes reserved for tiled # set the attributes reserved for tiled
[ setattr(self, k, types[str(k)](v)) for (k,v) in node.items() ] [ setattr(self, k, types[str(k)](v)) for (k,v) in node.items() ]


# set the attributes that are derived from tiled 'properties' # set the attributes that are derived from tiled 'properties'
for k,v in parse_properties(node).items(): for k,v in parse_properties(node).items():
Expand Down Expand Up @@ -58,7 +58,7 @@ def __init__(self, filename=None):
self.layernames = {} self.layernames = {}


# only used tiles are actually loaded, so there will be a difference # only used tiles are actually loaded, so there will be a difference
# between the GID's in the Tile map data (tmx) and the data in this # between the GIDs in the Tile map data (tmx) and the data in this
# class and the layers. This dictionary keeps track of that difference. # class and the layers. This dictionary keeps track of that difference.
self.gidmap = defaultdict(list) self.gidmap = defaultdict(list)


Expand Down Expand Up @@ -199,7 +199,7 @@ def getTileLocation(self, gid):
xrange(self.height), xrange(self.height),
xrange(len(self.tilelayers))) xrange(len(self.tilelayers)))


return [ (x,y,l) for (x,y,l) in p return [ (x,y,l) for (x,y,l) in p
if self.tilelayers[l].data[y][x] == gid ] if self.tilelayers[l].data[y][x] == gid ]




Expand Down Expand Up @@ -243,15 +243,15 @@ def getTilePropertiesByLayer(self, layer):
props.append((gid, self.tile_properties[gid])) props.append((gid, self.tile_properties[gid]))
except: except:
continue continue

return props return props




def registerGID(self, real_gid, flags=0): def registerGID(self, real_gid, flags=0):
""" """
used to manage the mapping of GID between the tmx data and the internal used to manage the mapping of GID between the tmx data and the internal
data. data.
number returned is gid used internally number returned is gid used internally
""" """


Expand Down Expand Up @@ -282,7 +282,7 @@ def mapGID(self, real_gid):
except KeyError: except KeyError:
return None return None
except TypeError: except TypeError:
msg = "GID's must be an integer" msg = "GIDs must be an integer"
raise TypeError, msg raise TypeError, msg




Expand Down Expand Up @@ -356,8 +356,8 @@ def getTileLayerOrder(self):
def visibleTileLayers(self): def visibleTileLayers(self):
""" """
Returns a list of TileLayer objects that are set 'visible'. Returns a list of TileLayer objects that are set 'visible'.
Layers have their visivility set in Tiled. Optionally, you can over- Layers have their visibility set in Tiled. Optionally, you can over-
ride the Tiled visibility by creating a property named 'visible'. ride the Tiled visibility by creating a property named 'visible'.
""" """


Expand Down Expand Up @@ -418,7 +418,7 @@ def parse(self, node):
else: else:
msg = "Found external tileset, but cannot handle type: {0}" msg = "Found external tileset, but cannot handle type: {0}"
raise Exception, msg.format(self.source) raise Exception, msg.format(self.source)

self.set_properties(node) self.set_properties(node)


# since tile objects [probably] don't have a lot of metadata, # since tile objects [probably] don't have a lot of metadata,
Expand Down Expand Up @@ -449,7 +449,7 @@ def __init__(self, parent, node):
self.name = None self.name = None
self.opacity = 1.0 self.opacity = 1.0
self.visible = True self.visible = True

self.parse(node) self.parse(node)




Expand Down Expand Up @@ -502,7 +502,7 @@ def parse(self, node):
elif compression: elif compression:
msg = "TMX compression type: {0} is not supported." msg = "TMX compression type: {0} is not supported."
raise Exception, msg.format(str(attr["compression"])) raise Exception, msg.format(str(attr["compression"]))

# if data is None, then it was not decoded or decompressed, so # if data is None, then it was not decoded or decompressed, so
# we assume here that it is going to be a bunch of tile elements # we assume here that it is going to be a bunch of tile elements
# TODO: this will probably raise an exception if there are no tiles # TODO: this will probably raise an exception if there are no tiles
Expand All @@ -514,13 +514,13 @@ def get_children(parent):
next_gid = get_children(data_node) next_gid = get_children(data_node)


elif data: elif data:
# data is a list of gid's. cast as 32-bit ints to format properly # data is a list of gids. cast as 32-bit ints to format properly
# create iterator to efficiently parse data # create iterator to efficiently parse data
next_gid=imap(lambda i:unpack("<L", "".join(i))[0], group(data, 4)) next_gid=imap(lambda i:unpack("<L", "".join(i))[0], group(data, 4))


# using bytes here limits the layer to 256 unique tiles # using bytes here limits the layer to 256 unique tiles
# may be a limitation for very detailed maps, but most maps are not # may be a limitation for very detailed maps, but most maps are not
# so detailed. # so detailed.
[ self.data.append(array.array("B")) for i in xrange(self.height) ] [ self.data.append(array.array("B")) for i in xrange(self.height) ]


for (y, x) in product(xrange(self.height), xrange(self.width)): for (y, x) in product(xrange(self.height), xrange(self.width)):
Expand Down Expand Up @@ -599,7 +599,7 @@ def parse(self, node):
if x > x2: x2 = x if x > x2: x2 = x
if y < y1: y1 = y if y < y1: y1 = y
if y > y2: y2 = y if y > y2: y2 = y
self.width = abs(x1) + abs(x2) self.width = abs(x1) + abs(x2)
self.height = abs(y1) + abs(y2) self.height = abs(y1) + abs(y2)


polyline = node.find('polyline') polyline = node.find('polyline')
Expand All @@ -612,7 +612,7 @@ def parse(self, node):
if x > x2: x2 = x if x > x2: x2 = x
if y < y1: y1 = y if y < y1: y1 = y
if y > y2: y2 = y if y > y2: y2 = y
self.width = abs(x1) + abs(x2) self.width = abs(x1) + abs(x2)
self.height = abs(y1) + abs(y2) self.height = abs(y1) + abs(y2)




10 changes: 5 additions & 5 deletions pytmx/tmxloader.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@
I have been intentionally not including a module rendering utility since I have been intentionally not including a module rendering utility since
rendering a map will have different requirements for every situation. However, rendering a map will have different requirements for every situation. However,
I can appreciate that some poeple won't understand how it works unless they see I can appreciate that some people won't understand how it works unless they see
it, so I am including a sample map and viewer. It includes a scrolling/zooming it, so I am including a sample map and viewer. It includes a scrolling/zooming
renderer. They are for demonstation purposes, and may not be suitable for all renderer. They are for demonstration purposes, and may not be suitable for all
projects. Use at your own risk. projects. Use at your own risk.
I've also included a copy of this loader that may work with python 3.x. I I've also included a copy of this loader that may work with python 3.x. I
Expand Down Expand Up @@ -117,7 +117,7 @@
When you want to draw tiles, you simply call "getTileImage": When you want to draw tiles, you simply call "getTileImage":
>>> image = tmxdata.getTileImage(x, y, layer) >>> image = tmxdata.getTileImage(x, y, layer)
>>> screen.blit(position, image) >>> screen.blit(position, image)
Expand Down Expand Up @@ -148,7 +148,7 @@
* The Tiled "properties" have reserved names. * The Tiled "properties" have reserved names.
If you use "properties" for any of the following object types, you cannot use If you use "properties" for any of the following object types, you cannot use
any of theese words as a name for your property. A ValueError will be raised any of these words as a name for your property. A ValueError will be raised
if there are any conflicts. if there are any conflicts.
As of 0.8.1, these values are: As of 0.8.1, these values are:
Expand Down Expand Up @@ -308,7 +308,7 @@ def handle_transformation(tile, flags):


colorkey = None colorkey = None
if t.trans: if t.trans:
colorkey = pygame.Color("#{0}".format(t.trans)) colorkey = pygame.Color("#{0}".format(t.trans))


# i dont agree with margins and spacing, but i'll support it anyway # i dont agree with margins and spacing, but i'll support it anyway
# such is life. okay.jpg # such is life. okay.jpg
Expand Down
34 changes: 17 additions & 17 deletions pytmx/tmxloader3.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
Pygame: test colorkey transparency Pygame: test colorkey transparency
Optimized for maps that do not make heavy use of tile Optimized for maps that do not make heavy use of tile
properties. If i find that it is used a lot then i can rework properties. If I find that it is used a lot then I can rework
it for better performance. it for better performance.
====================================================================== ======================================================================
Expand All @@ -40,7 +40,7 @@
When you want to draw tiles, you simply call "get_tile_image": When you want to draw tiles, you simply call "get_tile_image":
>>> image = tiledmap.get_tile_image(x, y, layer) >>> image = tiledmap.get_tile_image(x, y, layer)
>>> screen.blit(position, image) >>> screen.blit(position, image)
Expand Down Expand Up @@ -102,7 +102,7 @@ def __init__(self):
self.width = 0 self.width = 0
self.height = 0 self.height = 0
self.tilewidth = 0 self.tilewidth = 0
self.tileheight = 0 self.tileheight = 0


def get_tile_image(self, x, y, layer): def get_tile_image(self, x, y, layer):
""" """
Expand Down Expand Up @@ -153,8 +153,8 @@ def getTileImages(self, r, layer):
return a group of tiles in an area return a group of tiles in an area
expects a pygame rect or rect-like list/tuple expects a pygame rect or rect-like list/tuple
usefull if you don't want to repeatedly call get_tile_image useful if you don't want to repeatedly call get_tile_image
probably not the most effecient way of doing this, but oh well. probably not the most efficient way of doing this, but oh well.
""" """


raise NotImplementedError raise NotImplementedError
Expand Down Expand Up @@ -217,7 +217,7 @@ def __init__(self):
self.name = None self.name = None
self.opacity = 1.0 self.opacity = 1.0
self.visible = 1 self.visible = 1

class TiledObjectGroup(TiledElement): class TiledObjectGroup(TiledElement):
def __init__(self): def __init__(self):
TiledElement.__init__(self) TiledElement.__init__(self)
Expand Down Expand Up @@ -252,7 +252,7 @@ def load_tmx(filename):


from xml.dom.minidom import parse from xml.dom.minidom import parse
from itertools import tee, islice, chain from itertools import tee, islice, chain
from collections import defaultdict from collections import defaultdict
from struct import unpack from struct import unpack
import array, os import array, os


Expand Down Expand Up @@ -283,7 +283,7 @@ def load_tmx(filename):
"y": int, "y": int,
"value": str, "value": str,
} }

def pairwise(iterable): def pairwise(iterable):
# return a list as a sequence of pairs # return a list as a sequence of pairs
a, b = tee(iterable) a, b = tee(iterable)
Expand Down Expand Up @@ -324,7 +324,7 @@ def get_properties(node):


# get vlues of the properties element, if any # get vlues of the properties element, if any
d.update(parse_properties(node)) d.update(parse_properties(node))

return d return d


def set_properties(obj, node): def set_properties(obj, node):
Expand All @@ -349,7 +349,7 @@ def get_attributes(node):
return d return d


def decode_gid(raw_gid): def decode_gid(raw_gid):
# gid's are encoded with extra information # gids are encoded with extra information
# as of 0.7.0 it determines if the tile should be flipped when rendered # as of 0.7.0 it determines if the tile should be flipped when rendered


flags = 0 flags = 0
Expand Down Expand Up @@ -401,7 +401,7 @@ def parse_tileset(node, firstgid=None):
tileset.firstgid = firstgid tileset.firstgid = firstgid


# since tile objects probably don't have a lot of metadata, # since tile objects probably don't have a lot of metadata,
# we store it seperately from the class itself # we store it separately from the class itself
for child in node.childNodes: for child in node.childNodes:
if child.nodeName == "tile": if child.nodeName == "tile":
p = get_properties(child) p = get_properties(child)
Expand Down Expand Up @@ -439,7 +439,7 @@ def parse_tileset(node, firstgid=None):
y, r = divmod(attr["height"], tileset.tileheight) y, r = divmod(attr["height"], tileset.tileheight)


tileset.lastgid = tileset.firstgid + x + y tileset.lastgid = tileset.firstgid + x + y

return tileset, tiles return tileset, tiles




Expand Down Expand Up @@ -470,7 +470,7 @@ def parse_layer(tilesets, node):


elif not attr["encoding"] is None: elif not attr["encoding"] is None:
raise Exception("TMX encoding type: " + str(attr["encoding"]) + " is not supported.") raise Exception("TMX encoding type: " + str(attr["encoding"]) + " is not supported.")

if attr["compression"] == "gzip": if attr["compression"] == "gzip":
from io import BytesIO from io import BytesIO
import gzip import gzip
Expand All @@ -479,7 +479,7 @@ def parse_layer(tilesets, node):


elif not attr["compression"] is None: elif not attr["compression"] is None:
raise Exception("TMX compression type: " + str(attr["compression"]) + " is not supported.") raise Exception("TMX compression type: " + str(attr["compression"]) + " is not supported.")

# if data is None, then it was not decoded or decompressed, so # if data is None, then it was not decoded or decompressed, so
# we assume here that it is going to be a bunch of tile elements # we assume here that it is going to be a bunch of tile elements
if attr["encoding"] == next_gid is None: if attr["encoding"] == next_gid is None:
Expand All @@ -496,7 +496,7 @@ def u(i): return unpack("<L", bytes(i))[0]


gids = [ i.firstgid for i in tilesets ] gids = [ i.firstgid for i in tilesets ]


# fill up our 2D array of gid's. # fill up our 2D array of gids.
for y in range(layer.height): for y in range(layer.height):


# store as 16-bit ints, since we will never use enough tiles to fill a 32-bit int # store as 16-bit ints, since we will never use enough tiles to fill a 32-bit int
Expand Down Expand Up @@ -557,7 +557,7 @@ def load_pygame(filename):
tile_size = (t.tilewidth, t.tileheight) tile_size = (t.tilewidth, t.tileheight)


# some tileset images may be slightly larger than the tiles area # some tileset images may be slightly larger than the tiles area
# ie: may include a banner, copyright, ect. this compensates for that # ie: may include a banner, copyright, etc. this compensates for that
for y in range(0, int(h / t.tileheight) * t.tileheight, t.tileheight): for y in range(0, int(h / t.tileheight) * t.tileheight, t.tileheight):
for x in range(0, int(w / t.tilewidth) * t.tilewidth, t.tilewidth): for x in range(0, int(w / t.tilewidth) * t.tilewidth, t.tilewidth):


Expand All @@ -572,7 +572,7 @@ def load_pygame(filename):


# make a unique id for this image, not sure if this is the best way, but it works # make a unique id for this image, not sure if this is the best way, but it works
key = pygame.image.tostring(tile, "RGBA") key = pygame.image.tostring(tile, "RGBA")

# make sure we don't have a duplicate tile # make sure we don't have a duplicate tile
try: try:
tile = cache[key] tile = cache[key]
Expand Down
Loading

0 comments on commit dca385b

Please sign in to comment.