self.level_obj = self.level_list.get_current_level()
self.level_obj.load_tiles()
self.tiles = {}
- self.view = app.root
self.app = app
cols, rows = self.level_obj.get_size()
size=(cols * TILE_SIZE, rows * TILE_SIZE),
size_hint=(None, None))
- self.x_scroll_margin = float(TILE_SIZE) / self.view.size[0]
- self.y_scroll_margin = float(TILE_SIZE) / self.view.size[1]
-
self.mouse_move = False
self.caught = load_sound('sounds/caught.ogg')
self.move_counter = 0
self.player.pos = self.level_obj.enter_pos
- if platform() != 'android':
+ self.keyboard = None
+ self._key_bound = False
+
+ def build(self):
+ if platform() != 'android' and not self.keyboard:
# Very hack'ish
# We need to delay this import until after the window creation by
# the app, else our size config doesn't work
from kivy.core.window import Window
self.keyboard = Window.request_keyboard(self._closed, self)
+ if self.keyboard and not self._key_bound:
+ # We remove this binding when we're the not top level widget,
+ # so re-add it here
+ self._key_bound = True
self.keyboard.bind(on_key_down=self._on_key_down)
-
- def build(self):
self.clear_widgets()
self.tiles = {}
tiles = self.level_obj.get_tiles()
texture=tile.texture)
self.tiles[pos] = node
+ def fix_scroll_margins(self):
+ # We need to call this after app.root is set
+ self.view = self.app.root
+ self.x_scroll_margin = float(TILE_SIZE) / self.view.size[0]
+ self.y_scroll_margin = float(TILE_SIZE) / self.view.size[1]
+
def draw_player(self):
if self.player_tile:
self.remove_widget(self.player_tile)
self.add_widget(self.nemesis_tile)
def _closed(self):
- if platform() != 'android':
+ if self.keyboard:
+ self._key_bound = False
self.keyboard.unbind(on_key_down=self._on_key_down)
def _on_key_down(self, keyboard, keycode, text, modifiers):
def check_caught(self):
return self.nemesis.pos == self.player.pos
+ def stop_game(self):
+ Clock.unschedule(self.timed_move)
+ if self.nemesis_tile:
+ self.remove_widget(self.nemesis_tile)
+ self.nemesis.reset_pos()
+
def reset_level(self):
Clock.unschedule(self.timed_move)
self.timer_set = False
if self.level_obj:
self.level_obj.load_tiles()
self.player.pos = self.level_obj.enter_pos
- self.remove_widget(self.player_tile)
+ if self.player_tile:
+ self.remove_widget(self.player_tile)
self.view.scroll_x = 0
self.view.scroll_y = 0
self.build()
return True
return False
+ def do_reload(self):
+ self.level_obj = self.level_list.get_current_level()
+
def check_state(self):
if not self.level_obj:
return
# Jump to next level
self.level_obj = self.level_list.advance_to_next_level()
if not self.load_level():
- app = self.app
- self.app = None
self._closed()
- app.game_over(True)
+ self.app.game_over(True)
return
elif self.check_caught():
# Caught
if self.app.config.getdefault('bane', 'sound', '0') != '0':
self.caught.play()
self.reset_level()
- app = self.app
- self.app = None
self._closed()
- app.game_over(False)
+ self.app.game_over(False)
return
elif self.level_obj.is_button(self.player.pos):
self.level_obj.trigger_button(self.player.pos)
level_name = self.config.getdefault('bane', 'start_level', None)
if level_name:
self.levels.set_level_to(level_name)
+ self.game = GameWindow(self.levels, self)
return root
def on_start(self):
def start_game(self, label, ref):
"""Start the game"""
- self.game = GameWindow(self.levels, self)
- self.game.build()
self.root.clear_widgets()
self.root.add_widget(self.game)
+ self.game.fix_scroll_margins()
+ self.game.reset_level()
+ self.game.load_level()
# Ensure the player is visible
self.root.scroll_x = 0
self.root.scroll_y = 0
if won:
screen = WonScreen(self)
self.levels.reset()
+ self.game.do_reload()
else:
screen = LostScreen(self)
- del self.game
- self.game = None
+ self.game.stop_game()
self.root.clear_widgets()
self.root.add_widget(screen)