--- /dev/null
+..........................
+.... . ...............
+E ... ... . X
+. .. . . .............
+. .... . . . ....... .. ..
+. . . . ....... .. ..
+. .... ..... ....... .. ..
+. ... ... ..
+. ... .. .. ....... .....
+. . .. ... . . . .. .
+. . . .. .. .. .. . .. ..
+. . . .. ... .. .. ..
+. . . . . . .... . ...
+. . . .. ... .. . .
+. . . . .. ....... .....
+. . . ...... . .. ..
+. . . ...... . . .. ...
+. ... ...... .............
+. .
+..........................
import sys
-import os
import kivy
import pygame
from kivy.uix.floatlayout import FloatLayout
from kivy.graphics import Color, Rectangle
-from erdslangetjie.data import filepath, load
-from erdslangetjie.level import Level
+from erdslangetjie.level import LevelList
from erdslangetjie.player import ThePlayer, Nemesis
TILE_SIZE = 24
class GameWindow(FloatLayout):
- def __init__(self, level):
+ def __init__(self, level_list):
super(GameWindow, self).__init__(size=(648, 480))
- if os.path.exists(filepath(level)):
- level_data = load(level)
- self.level = level
- elif os.path.exists(filepath('levels/' + level)):
- level_data = load('levels/' + level)
- self.level = 'levels/' + level
- else:
- print 'Unable to find %s - treating this as a new level' % level
- sys.exit(1)
- self.level_obj = Level()
- self.level_obj.load(level_data)
- level_data.close()
+ self.level_list = level_list
+ self.level_obj = self.level_list.get_current_level()
+ self.level_obj.load_tiles()
self.tiles = {}
self.player = ThePlayer()
- self.level_obj.load_tiles()
if not self.level_obj.enter_pos:
raise RuntimeError('No entry point')
self.player_tile = None
if self.player.can_move(direction, self.level_obj):
self.player.move(direction)
self.draw_player()
+ if self.level_obj.at_exit(self.player.pos):
+ self.level_obj = self.level_list.advance_to_next_level()
+ if self.level_obj:
+ self.level_obj.load_tiles()
+ self.player.pos = self.level_obj.enter_pos
+ self.remove_widget(self.player_tile)
+ self.build()
+ else:
+ print 'You won!'
+ sys.exit(1)
class GameApp(App):
def __init__(self):
- self.level = 'level1.txt'
+ self.levels = LevelList()
super(GameApp, self).__init__()
def build(self):
- game = GameWindow(self.level)
+ game = GameWindow(self.levels)
game.build()
return game
# The level object
-from data import load_image
+import os
+from data import load_image, load, filepath
class Level(object):
- def __init__(self):
+ def __init__(self, levelfile):
self.data = []
self.exit_pos = []
self.enter_pos = None
self.tiles = []
-
- def load(self, levelfile):
- """Load the level"""
- self.data = []
# Because of how kivy's coordinate system works,
# we reverse the lines so things match up between
# the file and the display (top of file == top of display)
if tile == '.':
return True
return False
+
+
+class LevelList(object):
+
+ LEVELS = 'level_list'
+
+ def __init__(self):
+ self.levels = []
+ level_list = load(self.LEVELS)
+ for line in level_list:
+ line = line.strip()
+ if os.path.exists(filepath(line)):
+ level_file = load(line)
+ self.levels.append(Level(level_file))
+ level_file.close()
+ else:
+ print 'Level list includes non-existant level %s' % line
+ level_list.close()
+ self._cur_level = 0
+
+ def get_current_level(self):
+ if self._cur_level < len(self.levels):
+ return self.levels[self._cur_level]
+ else:
+ return None
+
+ def advance_to_next_level(self):
+ self._cur_level += 1
+ return self.get_current_level()