import itertools
from pytmx import TiledMap, TiledTileset
from kivy.core.image import Image
from kivy.uix.gridlayout import GridLayout
from kivy.properties import StringProperty
from kivy.app import App
class KivyTiledMap(TiledMap):
"""Loads Kivy images. Make sure that there is an active OpenGL context
(Kivy Window) before trying to load a map.
"""
def __init__(self, *args, **kwargs):
super(KivyTiledMap, self).__init__(*args, **kwargs)
# call load tile images for each tileset
for tileset in self.tilesets:
self.loadTileImages(tileset)
def loadTileImages(self, ts):
"""Loads the images in filename into Kivy Images.
:type ts: TiledTileset
"""
texture = Image(ts.source).texture
ts.width, ts.height = texture.size
# initialize the image array
self.images = [0] * self.maxgid
p = itertools.product(
xrange(ts.margin, ts.height, ts.tileheight + ts.margin),
xrange(ts.margin, ts.width, ts.tilewidth + ts.margin)
)
for real_gid, (y, x) in enumerate(p, ts.firstgid):
if x + ts.tilewidth - ts.spacing > ts.width:
continue
gids = self.map_gid(real_gid)
if gids:
x = x - ts.spacing
# convert the y coordinate to opengl (0 at bottom of texture)
y = ts.height - y - ts.tileheight + ts.spacing
tile = texture.get_region(x, y, ts.tilewidth, ts.tileheight)
for gid, flags in gids:
self.images[gid] = tile
def find_tile_with_property(self, property_name, layer_name='Meta'):
layer = self.getTileLayerByName(layer_name)
index = self.tilelayers.index(layer)
for tile in layer:
try:
properties = self.getTileProperties((tile[0], tile[1], index))
if properties.has_key(property_name):
return tile[0], tile[1]
except:
pass
return None
def tile_has_property(self, x, y, property_name, layer_name='Meta'):
"""Check if the tile coordinates passed in represent a collision.
:return: Boolean representing whether or not there was a collision.
"""
layer = self.getTileLayerByName(layer_name)
index = self.tilelayers.index(layer)
try:
properties = self.getTileProperties((x, y, index))
return properties.has_key(property_name)
except:
return False
class TileGrid(GridLayout):
"""Creates a Kivy grid and puts the tiles in a KivyTiledMap in it."""
map_file = StringProperty('map.tmx')
def __init__(self, **kwargs):
print("Hello world")
self.map = KivyTiledMap(self.map_file)
super(TileGrid, self).__init__(
rows=self.map.height, cols=self.map.width,
row_force_default=True,
row_default_height=self.map.tileheight,
col_force_default=True,
col_default_width=self.map.tilewidth,
**kwargs
)
tilelayer_index = 0
for tile in self.map.getTileLayerByName('Ground'):
texture = self.map.getTileImage(tile[0], tile[1], 0)
self.add_widget(Image(texture=texture, size=texture.size))
tilelayer_index += 1
def get_tile_position(self, x, y):
# invert the x and y to account for gridlayout's children
x = self.map.width - x - 1
y = self.map.height - y - 1
# calculate the position in the array
child_index = x + y * self.map.width
child = self.children[child_index]
return child.pos
def valid_move(self, x, y):
if x < 0 or x > self.map.width - 1 or y < 0 or y > self.map.height - 1:
Logger.debug('TileGrid: Move {},{} is out of bounds'.format(x, y))
return False
if self.map.tile_has_property(x, y, 'Collidable'):
Logger.debug('TileGrid: Move {},{} collides with something'.format(x, y))
return False
return True
class MapApp(App):
def build(self):
print("J")
return TileGrid()Is my code. When executed, it brings this error:
Fatal Python error: (pygame parachute) Segmentation Fault
Aborted (core dumped)All the above is only sample code taken from the wiki page for loading a map from Tiled. Here is my log file from running this:
[INFO ] Logger: Record log in /home/user/.kivy/logs/kivy_14-04-26_33.txt
[ [1;32mINFO [0m ] Kivy v1.7.1
[DEBUG ] Cache: register <kv.image> with limit=None, timeout=60s
[DEBUG ] Cache: register <kv.atlas> with limit=None, timeout=Nones
[INFO ] Image: Providers: img_tex, img_dds, img_pygame, img_pil, img_gif
[DEBUG ] Cache: register <kv.texture> with limit=1000, timeout=60s
[DEBUG ] Cache: register <kv.shader> with limit=1000, timeout=3600s
[INFO ] Factory: 144 symbols loaded
[DEBUG ] Cache: register <kv.lang> with limit=None, timeout=Nones
[DEBUG ] App: Loading kv <./map.kv>
[DEBUG ] ImagePygame: Load <free_tileset_version_10.png>And map.kv is an empty kv file in the same directory(it told me the file was missing, though no where in my code do I make mention of it.) After plenty of print statements I narrowed the problem down to this line:
texture = Image(ts.source).textureAlthough I still have no clue how to fix it. I'm running Fedora 20 using the Fedora18 repos from the instruction page.