'''
import os
+from kivy.core.image import ImageLoader
data_py = os.path.abspath(os.path.dirname(__file__))
data_dir = os.path.normpath(os.path.join(data_py, '..', 'data'))
+loader = ImageLoader()
def filepath(filename):
"mode" is passed as the second arg to open().
'''
return open(filepath(filename), mode)
+
+
+def load_image(filename):
+ '''Load an image into a kivy texture. We rely on kivy's caching
+ infrastructure'''
+ return loader.load(filepath(filename))
import os
import kivy
from erdslangetjie.data import filepath, load
+from erdslangetjie.level import Level
kivy.require('1.6.0')
from kivy.logger import Logger, LoggerHistory
from kivy.uix.widget import Widget
+from kivy.uix.floatlayout import FloatLayout
+from kivy.uix.scatter import Scatter
+from kivy.graphics import Color, Rectangle
-class EditorWindow(Widget):
+class EditorWindow(FloatLayout):
+
def __init__(self, level):
+ super(EditorWindow, self).__init__()
if os.path.exists(filepath(level)):
- self.level_data = load(level)
+ level_data = load(level)
self.level = level
elif os.path.exists(filepath('levels/' + level)):
- self.level_data = load('levels/' + level)
+ level_data = load('levels/' + level)
self.level = 'levels/' + level
else:
print 'Unable to find %s - treating this as a new level' % level
- self.level_data = load('levels/blank.txt')
+ level_data = load('levels/blank.txt')
if 'levels/' in level:
self.level = level
else:
self.level = 'levels/' + level
- super(EditorWindow, self).__init__()
+ self.level_obj = Level()
+ self.level_obj.load(level_data)
+ level_data.close()
+
+ def build(self):
+ self.clear_widgets()
+ tiles = self.level_obj.get_tiles()
+ tile_size = 24
+ bx, by = 0, 0
+ for tile_line in tiles:
+ bx = 0
+ for tile in tile_line:
+ node = Scatter(pos=(bx, by), size=(tile_size, tile_size))
+ with node.canvas:
+ Color(1, 1, 1)
+ Rectangle(size=node.size, texture=tile.texture)
+ self.add_widget(node)
+ bx += tile_size
+ by += tile_size
class EditorApp(App):
super(EditorApp, self).__init__()
def build(self):
- return EditorWindow(self.level)
+ editor = EditorWindow(self.level)
+ editor.build()
+ return editor
def main():
--- /dev/null
+# The level object
+
+from data import load_image
+
+
+class Level(object):
+
+ def __init__(self):
+ self.data = []
+
+ def load(self, levelfile):
+ """Load the level"""
+ self.data = []
+ for line in levelfile.readlines():
+ self.data.append(list(line))
+
+ def get_tiles(self):
+ """Return a list of tiles for the level"""
+ tiles = []
+ for j, line in enumerate(self.data):
+ tile_line = []
+ for i, c in enumerate(line):
+ if c == ' ':
+ tile_line.append(load_image('tiles/floor.bmp'))
+ elif c == '.':
+ tile_line.append(load_image('tiles/wall.bmp'))
+ elif c == 'E' or c == 'X':
+ tile_line.append(load_image('tiles/door.bmp'))
+ tiles.append(tile_line)
+ return tiles