-import sys
import kivy
import pygame
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()
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):
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)
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"
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
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