From ce05ade731b513cd2dce61e3fa06690a22de208c Mon Sep 17 00:00:00 2001 From: Neil Date: Wed, 17 Jul 2013 14:04:12 +0200 Subject: [PATCH] Rework imports to get correct screen size with kivy 1.7 --- erdslangetjie/__main__.py | 456 +--------------------------------- erdslangetjie/localwidgets.py | 454 +++++++++++++++++++++++++++++++++ 2 files changed, 455 insertions(+), 455 deletions(-) create mode 100644 erdslangetjie/localwidgets.py diff --git a/erdslangetjie/__main__.py b/erdslangetjie/__main__.py index e408ac8..1728970 100644 --- a/erdslangetjie/__main__.py +++ b/erdslangetjie/__main__.py @@ -1,466 +1,12 @@ -from erdslangetjie.constants import TILE_SIZE, LEFT, RIGHT, UP, DOWN - -from kivy.app import App -from kivy.uix.widget import Widget -from kivy.uix.relativelayout import RelativeLayout -from kivy.uix.scrollview import ScrollView -from kivy.uix.label import Label -from kivy.uix.popup import Popup -from kivy.graphics import Rectangle from kivy.utils import platform -from kivy.clock import Clock from kivy.config import Config -from erdslangetjie.level import LevelList -from erdslangetjie.data import load_image, load_sound -from erdslangetjie.player import ThePlayer, Nemesis - - if platform() != 'android': Config.set('graphics', 'width', '1026') Config.set('graphics', 'height', '760') - -class GameWindow(RelativeLayout): - - def __init__(self, level_list, app): - self.level_list = level_list - self.level_obj = self.level_list.get_current_level() - self.level_obj.load_tiles() - self.app = app - - cols, rows = self.level_obj.get_size() - - super(GameWindow, self).__init__( - size=(cols * TILE_SIZE, rows * TILE_SIZE), - size_hint=(None, None)) - - self.mouse_move = False - - self.caught = load_sound('sounds/caught.ogg') - - self.player = ThePlayer() - self.nemesis = Nemesis(self.app.config) - if not self.level_obj.enter_pos: - raise RuntimeError('No entry point') - self.player_tile = None - self.nemesis_tile = None - self.timer_set = False - self.move_counter = 0 - - self.player.pos = self.level_obj.enter_pos - self.keyboard = None - self._key_bound = False - self._background = None - - 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) - self.clear_widgets() - self._background = Widget(size=self.size, pos=(0, 0)) - tiles = self.level_obj.get_tiles() - bx, by = 0, 0 - for tile_line in tiles: - bx = 0 - for tile in tile_line: - self.draw_tile((bx, by), tile) - bx += TILE_SIZE - by += TILE_SIZE - self.add_widget(self._background) - - def draw_tile(self, pos, tile): - with self._background.canvas: - Rectangle(pos=pos, size=(TILE_SIZE, TILE_SIZE), - texture=tile.texture) - - 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) - sprite_pos = (self.player.pos[0] * TILE_SIZE, - self.player.pos[1] * TILE_SIZE) - self.player_tile = Widget(size=(TILE_SIZE, TILE_SIZE), - pos=sprite_pos) - with self.player_tile.canvas: - Rectangle(pos=sprite_pos, size=self.player_tile.size, - texture=self.player.get_texture()) - self.add_widget(self.player_tile) - for offset in [(TILE_SIZE - 1, TILE_SIZE - 1), - (-TILE_SIZE + 1, TILE_SIZE - 1), - (TILE_SIZE - 1, -TILE_SIZE + 1), - (-TILE_SIZE + 1, -TILE_SIZE + 1), - (0, 2 * TILE_SIZE - 2), - (-2 * TILE_SIZE + 2, 0), - (2 * TILE_SIZE - 2, 0), - (0, -2 * TILE_SIZE + 2), - (0, 0)]: - # Aim is to ensure a 'neighbourhood' around the player - # is visible if possible - check_point = (sprite_pos[0] + offset[0] + TILE_SIZE / 2, - sprite_pos[1] + offset[1] + TILE_SIZE / 2) - true_point = self.to_parent(*check_point) - if check_point[0] < 0: - continue - if check_point[1] < 0: - continue - if check_point[0] >= self.size[0]: - continue - if check_point[1] >= self.size[1]: - continue - while not self.included(true_point, 0): - # Scroll ourselves - if true_point[0] >= self.view.size[0]: - self.view.scroll_x += self.x_scroll_margin - true_point = self.to_parent(*check_point) - # Avoid an infinite loop that can happen we - # changing screens - if self.view.scroll_x > 0.99: - return - elif true_point[0] < 0: - self.view.scroll_x -= self.x_scroll_margin - true_point = self.to_parent(*check_point) - # See above - if self.view.scroll_x < 0.01: - return - elif true_point[1] >= self.view.size[1]: - self.view.scroll_y += self.y_scroll_margin - true_point = self.to_parent(*check_point) - # See above - if self.view.scroll_y > 0.99: - return - elif true_point[1] < 0: - self.view.scroll_y -= self.y_scroll_margin - true_point = self.to_parent(*check_point) - # See above - if self.view.scroll_y < 0.01: - return - - def included(self, point, margin): - if point[0] < margin: - return False - if point[0] >= self.view.size[0] - margin: - return False - if point[1] < margin: - return False - if point[1] >= self.view.size[1] - margin: - return False - return True - - def draw_nemesis(self): - if not self.nemesis.on_board(): - return - if self.nemesis_tile: - self.remove_widget(self.nemesis_tile) - sprite_pos = (self.nemesis.pos[0] * TILE_SIZE, - self.nemesis.pos[1] * TILE_SIZE) - self.nemesis_tile = Widget(size=(TILE_SIZE, TILE_SIZE), - pos=sprite_pos) - with self.nemesis_tile.canvas: - Rectangle(pos=sprite_pos, size=self.nemesis_tile.size, - texture=self.nemesis.get_texture()) - self.add_widget(self.nemesis_tile) - - def _closed(self): - 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): - direction = None - letter = keycode[1].lower() - if letter in UP: - direction = (0, 1) - elif letter in DOWN: - direction = (0, -1) - elif letter in LEFT: - direction = (-1, 0) - elif letter in RIGHT: - direction = (1, 0) - if direction: - self.do_move(direction) - - def do_move(self, direction): - if not self.level_obj: - return - # Do nothing on null moves - if not self.player.move(direction, self.level_obj): - return - if self.check_state(): - return - self.do_nemesis_move() - - def do_nemesis_move(self): - self.nemesis.move(self.level_obj, self.player.pos) - if self.check_state(): - return - if self.move_counter > 4: - self.move_counter = 0 - self.draw_nemesis() - self.nemesis.move(self.level_obj, self.player.pos) - if self.check_state(): - return - else: - self.move_counter += 1 - self.draw_nemesis() - self.draw_player() - self.reset_timer() - - def timed_move(self, event): - if not self.level_obj: - return - self.do_nemesis_move() - - def reset_timer(self): - self.timer_set = True - Clock.unschedule(self.timed_move) - Clock.schedule_once(self.timed_move, 3) - - 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 - self.move_counter = 0 - if self.nemesis_tile: - self.remove_widget(self.nemesis_tile) - self.nemesis.reset_pos() - - def load_level(self): - if self.level_obj: - self.level_obj.load_tiles() - self.player.pos = self.level_obj.enter_pos - if self.player_tile: - self.remove_widget(self.player_tile) - self.view.scroll_x = 0 - self.view.scroll_y = 0 - self.build() - self.draw_nemesis() - self.draw_player() - 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 True - if self.level_obj.at_exit(self.player.pos): - self.reset_level() - # Jump to next level - self.level_obj = self.level_list.advance_to_next_level() - if not self.load_level(): - self._closed() - self.app.game_over(True) - return True - elif self.check_caught(): - # Caught - if self.app.config.getdefault('bane', 'sound', '0') != '0': - self.caught.play() - self.reset_level() - self._closed() - self.app.game_over(False) - return True - elif self.level_obj.is_button(self.player.pos): - self.level_obj.trigger_button(self.player.pos) - elif self.level_obj.is_button(self.nemesis.pos): - self.level_obj.trigger_button(self.nemesis.pos) - for map_pos, new_tile in self.level_obj.get_changed_tiles(): - pos = (map_pos[0] * TILE_SIZE, map_pos[1] * TILE_SIZE) - self.draw_tile(pos, new_tile) - return False - - def _calc_mouse_pos(self, pos): - pos = self.to_local(*pos) - return (int(pos[0] / TILE_SIZE), int(pos[1] / TILE_SIZE)) - - def _near_player(self, pos): - return (abs(pos[0] - self.player.pos[0]) < 2 and - abs(pos[1] - self.player.pos[1]) < 2) - - def on_touch_down(self, touch): - pos = self._calc_mouse_pos(touch.pos) - if self._near_player(pos): - self.mouse_move = True - self.mouse_start = pos - - def on_touch_up(self, touch): - self.mouse_move = False - - def on_touch_move(self, touch): - if self.mouse_move: - pos = self._calc_mouse_pos(touch.pos) - if (pos[0] - self.mouse_start[0] != 0) or ( - pos[1] - self.mouse_start[1] != 0): - direction = (pos[0] - self.mouse_start[0], - pos[1] - self.mouse_start[1]) - self.do_move(direction) - self.mouse_start = pos - - -class Screen(Widget): - - BACKGROUND = None - START = 'Start' - - def __init__(self, app): - super(Screen, self).__init__() - self.image = load_image(self.BACKGROUND) - self.app = app - with self.canvas: - Rectangle(pos=(0, 0), size=(1026, 760), - texture=self.image.texture) - - self.stop_button = Label( - text='[ref=quit][color=ff0066]Quit[/color][/ref]', - font_size=30, - markup=True, - size=(200, 40), - pos=((1026 - 200) / 2 - 100, 100)) - self.stop_button.bind(on_ref_press=self.app.stop_app) - self.start_button = Label( - text="[ref=start][color=00ff66]%s[/color][/ref]" % self.START, - font_size=30, - markup=True, size=(200, 40), - pos=((1026 - 200) / 2 + 100, 100)) - self.start_button.bind(on_ref_press=self.app.start_game) - self.add_widget(self.stop_button) - self.add_widget(self.start_button) - - -class IntroScreen(Screen): - - BACKGROUND = 'screens/intro_screen.png' - START = 'Start the Game' - - -class WonScreen(Screen): - - BACKGROUND = 'screens/won.png' - START = 'Play again?' - - -class LostScreen(Screen): - - BACKGROUND = 'screens/lost.png' - START = 'Retry?' - - -class GameApp(App): - - title = "Bane's Befuddlement" - - def __init__(self): - super(GameApp, self).__init__() - self.levels = LevelList() - self.game = None - - def build_config(self, config): - config.setdefaults('bane', { - 'start_level': 'levels/level1.txt', - 'sound': 'True' - }) - - def build_settings(self, settings): - config_json = """[ - { "type": "title", - "title": "Bane's Befuddlement" - }, - - { "type": "options", - "title": "Start Level", - "desc": "Level to start at", - "section": "bane", - "key": "start_level", - "options": ["%s"] }, - - { "type": "bool", - "title": "Sound", - "desc": "Enable sound", - "section": "bane", - "key": "sound" - } - ]""" % '", "'.join(self.levels.get_level_names()) - settings.add_json_panel("Bane's Befuddlement", - self.config, data=config_json) - - def build(self): - root = ScrollView(size_hint=(None, None)) - 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): - from kivy.base import EventLoop - window = EventLoop.window - if platform() == 'android': - window.fullscreen = True - self.root.size = window.size - errors = self.levels.get_errors() - if errors: - popup = Popup(title='Levels excluded', - content=Label(text='\n'.join(errors)), - size_hint=(.5, .5)) - popup.open() - self.make_intro() - - def make_intro(self): - self.root.clear_widgets() - screen = IntroScreen(self) - self.root.add_widget(screen) - - def stop_app(self, label, ref): - self.stop() - - def start_game(self, label, ref): - """Start the game""" - 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 - self.game.draw_player() - self.game.draw_nemesis() - - def game_over(self, won): - if won: - screen = WonScreen(self) - self.levels.reset() - self.game.do_reload() - else: - screen = LostScreen(self) - self.game.stop_game() - self.root.clear_widgets() - self.root.add_widget(screen) - - def main(): """ Erdslangetjie, a maze game of eluding nemesis """ + from erdslangetjie.localwidgets import GameApp GameApp().run() diff --git a/erdslangetjie/localwidgets.py b/erdslangetjie/localwidgets.py new file mode 100644 index 0000000..885ac51 --- /dev/null +++ b/erdslangetjie/localwidgets.py @@ -0,0 +1,454 @@ +from erdslangetjie.constants import TILE_SIZE, LEFT, RIGHT, UP, DOWN + +from kivy.app import App +from kivy.uix.widget import Widget +from kivy.uix.relativelayout import RelativeLayout +from kivy.uix.scrollview import ScrollView +from kivy.uix.label import Label +from kivy.uix.popup import Popup +from kivy.graphics import Rectangle +from kivy.utils import platform +from kivy.clock import Clock + +from erdslangetjie.level import LevelList +from erdslangetjie.data import load_image, load_sound +from erdslangetjie.player import ThePlayer, Nemesis + + +class GameWindow(RelativeLayout): + + def __init__(self, level_list, app): + self.level_list = level_list + self.level_obj = self.level_list.get_current_level() + self.level_obj.load_tiles() + self.app = app + + cols, rows = self.level_obj.get_size() + + super(GameWindow, self).__init__( + size=(cols * TILE_SIZE, rows * TILE_SIZE), + size_hint=(None, None)) + + self.mouse_move = False + + self.caught = load_sound('sounds/caught.ogg') + + self.player = ThePlayer() + self.nemesis = Nemesis(self.app.config) + if not self.level_obj.enter_pos: + raise RuntimeError('No entry point') + self.player_tile = None + self.nemesis_tile = None + self.timer_set = False + self.move_counter = 0 + + self.player.pos = self.level_obj.enter_pos + self.keyboard = None + self._key_bound = False + self._background = None + + 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) + self.clear_widgets() + self._background = Widget(size=self.size, pos=(0, 0)) + tiles = self.level_obj.get_tiles() + bx, by = 0, 0 + for tile_line in tiles: + bx = 0 + for tile in tile_line: + self.draw_tile((bx, by), tile) + bx += TILE_SIZE + by += TILE_SIZE + self.add_widget(self._background) + + def draw_tile(self, pos, tile): + with self._background.canvas: + Rectangle(pos=pos, size=(TILE_SIZE, TILE_SIZE), + texture=tile.texture) + + 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) + sprite_pos = (self.player.pos[0] * TILE_SIZE, + self.player.pos[1] * TILE_SIZE) + self.player_tile = Widget(size=(TILE_SIZE, TILE_SIZE), + pos=sprite_pos) + with self.player_tile.canvas: + Rectangle(pos=sprite_pos, size=self.player_tile.size, + texture=self.player.get_texture()) + self.add_widget(self.player_tile) + for offset in [(TILE_SIZE - 1, TILE_SIZE - 1), + (-TILE_SIZE + 1, TILE_SIZE - 1), + (TILE_SIZE - 1, -TILE_SIZE + 1), + (-TILE_SIZE + 1, -TILE_SIZE + 1), + (0, 2 * TILE_SIZE - 2), + (-2 * TILE_SIZE + 2, 0), + (2 * TILE_SIZE - 2, 0), + (0, -2 * TILE_SIZE + 2), + (0, 0)]: + # Aim is to ensure a 'neighbourhood' around the player + # is visible if possible + check_point = (sprite_pos[0] + offset[0] + TILE_SIZE / 2, + sprite_pos[1] + offset[1] + TILE_SIZE / 2) + true_point = self.to_parent(*check_point) + if check_point[0] < 0: + continue + if check_point[1] < 0: + continue + if check_point[0] >= self.size[0]: + continue + if check_point[1] >= self.size[1]: + continue + while not self.included(true_point, 0): + # Scroll ourselves + if true_point[0] >= self.view.size[0]: + self.view.scroll_x += self.x_scroll_margin + true_point = self.to_parent(*check_point) + # Avoid an infinite loop that can happen we + # changing screens + if self.view.scroll_x > 0.99: + return + elif true_point[0] < 0: + self.view.scroll_x -= self.x_scroll_margin + true_point = self.to_parent(*check_point) + # See above + if self.view.scroll_x < 0.01: + return + elif true_point[1] >= self.view.size[1]: + self.view.scroll_y += self.y_scroll_margin + true_point = self.to_parent(*check_point) + # See above + if self.view.scroll_y > 0.99: + return + elif true_point[1] < 0: + self.view.scroll_y -= self.y_scroll_margin + true_point = self.to_parent(*check_point) + # See above + if self.view.scroll_y < 0.01: + return + + def included(self, point, margin): + if point[0] < margin: + return False + if point[0] >= self.view.size[0] - margin: + return False + if point[1] < margin: + return False + if point[1] >= self.view.size[1] - margin: + return False + return True + + def draw_nemesis(self): + if not self.nemesis.on_board(): + return + if self.nemesis_tile: + self.remove_widget(self.nemesis_tile) + sprite_pos = (self.nemesis.pos[0] * TILE_SIZE, + self.nemesis.pos[1] * TILE_SIZE) + self.nemesis_tile = Widget(size=(TILE_SIZE, TILE_SIZE), + pos=sprite_pos) + with self.nemesis_tile.canvas: + Rectangle(pos=sprite_pos, size=self.nemesis_tile.size, + texture=self.nemesis.get_texture()) + self.add_widget(self.nemesis_tile) + + def _closed(self): + 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): + direction = None + letter = keycode[1].lower() + if letter in UP: + direction = (0, 1) + elif letter in DOWN: + direction = (0, -1) + elif letter in LEFT: + direction = (-1, 0) + elif letter in RIGHT: + direction = (1, 0) + if direction: + self.do_move(direction) + + def do_move(self, direction): + if not self.level_obj: + return + # Do nothing on null moves + if not self.player.move(direction, self.level_obj): + return + if self.check_state(): + return + self.do_nemesis_move() + + def do_nemesis_move(self): + self.nemesis.move(self.level_obj, self.player.pos) + if self.check_state(): + return + if self.move_counter > 4: + self.move_counter = 0 + self.draw_nemesis() + self.nemesis.move(self.level_obj, self.player.pos) + if self.check_state(): + return + else: + self.move_counter += 1 + self.draw_nemesis() + self.draw_player() + self.reset_timer() + + def timed_move(self, event): + if not self.level_obj: + return + self.do_nemesis_move() + + def reset_timer(self): + self.timer_set = True + Clock.unschedule(self.timed_move) + Clock.schedule_once(self.timed_move, 3) + + 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 + self.move_counter = 0 + if self.nemesis_tile: + self.remove_widget(self.nemesis_tile) + self.nemesis.reset_pos() + + def load_level(self): + if self.level_obj: + self.level_obj.load_tiles() + self.player.pos = self.level_obj.enter_pos + if self.player_tile: + self.remove_widget(self.player_tile) + self.view.scroll_x = 0 + self.view.scroll_y = 0 + self.build() + self.draw_nemesis() + self.draw_player() + 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 True + if self.level_obj.at_exit(self.player.pos): + self.reset_level() + # Jump to next level + self.level_obj = self.level_list.advance_to_next_level() + if not self.load_level(): + self._closed() + self.app.game_over(True) + return True + elif self.check_caught(): + # Caught + if self.app.config.getdefault('bane', 'sound', '0') != '0': + self.caught.play() + self.reset_level() + self._closed() + self.app.game_over(False) + return True + elif self.level_obj.is_button(self.player.pos): + self.level_obj.trigger_button(self.player.pos) + elif self.level_obj.is_button(self.nemesis.pos): + self.level_obj.trigger_button(self.nemesis.pos) + for map_pos, new_tile in self.level_obj.get_changed_tiles(): + pos = (map_pos[0] * TILE_SIZE, map_pos[1] * TILE_SIZE) + self.draw_tile(pos, new_tile) + return False + + def _calc_mouse_pos(self, pos): + pos = self.to_local(*pos) + return (int(pos[0] / TILE_SIZE), int(pos[1] / TILE_SIZE)) + + def _near_player(self, pos): + return (abs(pos[0] - self.player.pos[0]) < 2 and + abs(pos[1] - self.player.pos[1]) < 2) + + def on_touch_down(self, touch): + pos = self._calc_mouse_pos(touch.pos) + if self._near_player(pos): + self.mouse_move = True + self.mouse_start = pos + + def on_touch_up(self, touch): + self.mouse_move = False + + def on_touch_move(self, touch): + if self.mouse_move: + pos = self._calc_mouse_pos(touch.pos) + if (pos[0] - self.mouse_start[0] != 0) or ( + pos[1] - self.mouse_start[1] != 0): + direction = (pos[0] - self.mouse_start[0], + pos[1] - self.mouse_start[1]) + self.do_move(direction) + self.mouse_start = pos + + +class Screen(Widget): + + BACKGROUND = None + START = 'Start' + + def __init__(self, app): + super(Screen, self).__init__() + self.image = load_image(self.BACKGROUND) + self.app = app + with self.canvas: + Rectangle(pos=(0, 0), size=(1026, 760), + texture=self.image.texture) + + self.stop_button = Label( + text='[ref=quit][color=ff0066]Quit[/color][/ref]', + font_size=30, + markup=True, + size=(200, 40), + pos=((1026 - 200) / 2 - 100, 100)) + self.stop_button.bind(on_ref_press=self.app.stop_app) + self.start_button = Label( + text="[ref=start][color=00ff66]%s[/color][/ref]" % self.START, + font_size=30, + markup=True, size=(200, 40), + pos=((1026 - 200) / 2 + 100, 100)) + self.start_button.bind(on_ref_press=self.app.start_game) + self.add_widget(self.stop_button) + self.add_widget(self.start_button) + + +class IntroScreen(Screen): + + BACKGROUND = 'screens/intro_screen.png' + START = 'Start the Game' + + +class WonScreen(Screen): + + BACKGROUND = 'screens/won.png' + START = 'Play again?' + + +class LostScreen(Screen): + + BACKGROUND = 'screens/lost.png' + START = 'Retry?' + + +class GameApp(App): + + title = "Bane's Befuddlement" + + def __init__(self): + super(GameApp, self).__init__() + self.levels = LevelList() + self.game = None + + def build_config(self, config): + config.setdefaults('bane', { + 'start_level': 'levels/level1.txt', + 'sound': 'True' + }) + + def build_settings(self, settings): + config_json = """[ + { "type": "title", + "title": "Bane's Befuddlement" + }, + + { "type": "options", + "title": "Start Level", + "desc": "Level to start at", + "section": "bane", + "key": "start_level", + "options": ["%s"] }, + + { "type": "bool", + "title": "Sound", + "desc": "Enable sound", + "section": "bane", + "key": "sound" + } + ]""" % '", "'.join(self.levels.get_level_names()) + settings.add_json_panel("Bane's Befuddlement", + self.config, data=config_json) + + def build(self): + root = ScrollView(size_hint=(None, None)) + 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): + from kivy.base import EventLoop + window = EventLoop.window + if platform() == 'android': + window.fullscreen = True + self.root.size = window.size + errors = self.levels.get_errors() + if errors: + popup = Popup(title='Levels excluded', + content=Label(text='\n'.join(errors)), + size_hint=(.5, .5)) + popup.open() + self.make_intro() + + def make_intro(self): + self.root.clear_widgets() + screen = IntroScreen(self) + self.root.add_widget(screen) + + def stop_app(self, label, ref): + self.stop() + + def start_game(self, label, ref): + """Start the game""" + 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 + self.game.draw_player() + self.game.draw_nemesis() + + def game_over(self, won): + if won: + screen = WonScreen(self) + self.levels.reset() + self.game.do_reload() + else: + screen = LostScreen(self) + self.game.stop_game() + self.root.clear_widgets() + self.root.add_widget(screen) -- 2.34.1