From a0b18a395627a951fe26fc6a3bf6acabdb23c41b Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 15 Apr 2013 17:58:57 +0200 Subject: [PATCH] Advance through the levels --- data/level_list | 2 ++ data/levels/level2.txt | 20 ++++++++++++++++++ erdslangetjie/__main__.py | 36 +++++++++++++++----------------- erdslangetjie/editor/editor.py | 3 +-- erdslangetjie/level.py | 38 ++++++++++++++++++++++++++++------ 5 files changed, 72 insertions(+), 27 deletions(-) create mode 100644 data/level_list create mode 100644 data/levels/level2.txt diff --git a/data/level_list b/data/level_list new file mode 100644 index 0000000..ba33d41 --- /dev/null +++ b/data/level_list @@ -0,0 +1,2 @@ +levels/level1.txt +levels/level2.txt diff --git a/data/levels/level2.txt b/data/levels/level2.txt new file mode 100644 index 0000000..e683c76 --- /dev/null +++ b/data/levels/level2.txt @@ -0,0 +1,20 @@ +.......................... +.... . ............... +E ... ... . X +. .. . . ............. +. .... . . . ....... .. .. +. . . . ....... .. .. +. .... ..... ....... .. .. +. ... ... .. +. ... .. .. ....... ..... +. . .. ... . . . .. . +. . . .. .. .. .. . .. .. +. . . .. ... .. .. .. +. . . . . . .... . ... +. . . .. ... .. . . +. . . . .. ....... ..... +. . . ...... . .. .. +. . . ...... . . .. ... +. ... ...... ............. +. . +.......................... diff --git a/erdslangetjie/__main__.py b/erdslangetjie/__main__.py index 50ee66d..7329b44 100644 --- a/erdslangetjie/__main__.py +++ b/erdslangetjie/__main__.py @@ -1,5 +1,4 @@ import sys -import os import kivy import pygame @@ -11,8 +10,7 @@ from kivy.uix.widget import Widget 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 @@ -20,24 +18,14 @@ 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 @@ -97,16 +85,26 @@ class GameWindow(FloatLayout): 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 diff --git a/erdslangetjie/editor/editor.py b/erdslangetjie/editor/editor.py index 45545a2..08c5f6b 100644 --- a/erdslangetjie/editor/editor.py +++ b/erdslangetjie/editor/editor.py @@ -31,8 +31,7 @@ class EditorWindow(FloatLayout): self.level = level else: self.level = 'levels/' + level - self.level_obj = Level() - self.level_obj.load(level_data) + self.level_obj = Level(level_data) level_data.close() self.level_obj.load_tiles() diff --git a/erdslangetjie/level.py b/erdslangetjie/level.py index 645a62f..283588c 100644 --- a/erdslangetjie/level.py +++ b/erdslangetjie/level.py @@ -1,19 +1,16 @@ # 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) @@ -60,3 +57,32 @@ class Level(object): 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() -- 2.34.1