Crudely hook up screens
authorNeil <neil@dip.sun.ac.za>
Wed, 17 Apr 2013 11:31:06 +0000 (13:31 +0200)
committerNeil <neil@dip.sun.ac.za>
Wed, 17 Apr 2013 11:31:06 +0000 (13:31 +0200)
erdslangetjie/__main__.py

index 95fdf1be5d3423a6e41fc20c63dd412af89e7d16..b01c9ef8db1bbbf07d70add1dfe78e12cef67153 100644 (file)
@@ -1,4 +1,3 @@
-import sys
 import kivy
 import pygame
 
@@ -9,22 +8,25 @@ from kivy.uix.widget import Widget
 from kivy.logger import Logger, LoggerHistory
 from kivy.uix.relativelayout import RelativeLayout
 from kivy.uix.scrollview import ScrollView
+from kivy.uix.button import Button
 from kivy.graphics import Color, Rectangle
 from kivy.utils import platform
 
 from erdslangetjie.level import LevelList
+from erdslangetjie.data import load_image
 from erdslangetjie.player import ThePlayer, Nemesis
 from erdslangetjie.constants import TILE_SIZE, QUIET
 
 
 class GameWindow(RelativeLayout):
 
-    def __init__(self, level_list, view):
+    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.tiles = {}
-        self.view = view
+        self.view = app.root
+        self.app = app
 
         cols, rows = self.level_obj.get_size()
 
@@ -176,6 +178,16 @@ class GameWindow(RelativeLayout):
         self.player.move(direction, self.level_obj)
         self.draw_player()
         self.check_state()
+        self.reset_timer()
+
+    def timed_move(self):
+        self.nemesis.move(self.level_obj)
+        self.draw_nemesis()
+        self.check_state()
+        self.reset_timer()
+
+    def reset_timer(self):
+        pass
 
     def check_state(self):
         if self.level_obj.at_exit(self.player.pos):
@@ -193,12 +205,10 @@ class GameWindow(RelativeLayout):
                 self.draw_nemesis()
                 self.draw_player()
             else:
-                print 'You won!'
-                sys.exit(1)
+                self.app.game_over(True)
         elif self.nemesis.pos == self.player.pos:
             # Caught
-            print 'You lost!'
-            sys.exit(1)
+            self.app.game_over(False)
 
     def _calc_mouse_pos(self, pos):
         pos = self.to_local(*pos)
@@ -224,6 +234,49 @@ class GameWindow(RelativeLayout):
                 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 = Button(
+                text='Quit', size=(100, 50),
+                pos=((1026 - 100) / 2, 650))
+        self.stop_button.bind(on_press=self.app.stop_app)
+        self.start_button = Button(
+                text=self.START, size=(500, 50),
+                pos=((1026 - 250) / 2, 600))
+        self.start_button.bind(on_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 = "Peter's thread snake"
@@ -242,8 +295,21 @@ class GameApp(App):
         if platform() == 'android':
             window.fullscreen = True
         self.root.size = window.size
-        game = GameWindow(self.levels, self.root)
+        self.make_intro()
+
+    def make_intro(self):
+        self.root.clear_widgets()
+        screen = IntroScreen(self)
+        self.root.add_widget(screen)
+
+    def stop_app(self, button):
+        self.stop()
+
+    def start_game(self, button):
+        """Start the game"""
+        game = GameWindow(self.levels, self)
         game.build()
+        self.root.clear_widgets()
         self.root.add_widget(game)
         # Ensure the player is visible
         self.root.scroll_x = 0
@@ -251,6 +317,15 @@ class GameApp(App):
         game.draw_player()
         game.draw_nemesis()
 
+    def game_over(self, won):
+        if won:
+            screen = WonScreen(self)
+            self.levels.reset()
+        else:
+            screen = LostScreen(self)
+        self.root.clear_widgets()
+        self.root.add_widget(screen)
+
 
 def main():
     """ Erdslangetjie, a maze game of eluding nemesis