diff options
Diffstat (limited to 'usmpgames/application.py')
-rwxr-xr-x | usmpgames/application.py | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/usmpgames/application.py b/usmpgames/application.py new file mode 100755 index 0000000..cd6b863 --- /dev/null +++ b/usmpgames/application.py @@ -0,0 +1,129 @@ +#!/usr/bin/python +# -*- coding: iso-8859-15 -*- + +import sys +import pygame + +class Application(): + + _instance = None + + @staticmethod + def instance(): + return Application._instance + + def __init__(self): + self._state_stack = [] + self._current_state = None + self._running = True + self._fps = 25 + if Application._instance is None: + Application._instance = self + + def set_screen(self, screen): + self._screen = screen + + def screen(self): + return self._screen + + def push_state(self, new_state): + # exit from current state + if self._current_state is not None: + self._current_state.exiting_state( True ) + if new_state is not None: + self._state_stack.append(self._current_state) + + # process new state + fromStack = False + if new_state is None: + if len(self._state_stack) == 0 : + self.set_running( False ) + return + else : + self._current_state = self.pop_state() + fromStack = True + else: + self._current_state = new_state + + # entering new state + if self._current_state is None: + self.set_running(False) + else: + self._current_state.set_screen(self.screen()) + self._current_state.entering_state(fromStack) + + def change_state(self, new_state): + # exit from current state + if self._current_state is not None: + self._current_state.exiting_state(False) + + # process current state + if new_state is None: + if len(self._state_stack) == 0 : + self.set_running( False ) + return + else : + self.pop_state() + return + else: + self._current_state = new_state + + # entering new state + if self._current_state is not None: + self._current_state.set_screen(self.screen()) + self._current_state.entering_state( False ) + else: + self.set_running(False) + + # entering new state + + def pop_state(self): + if self._current_state is not None: + self._current_state.exiting_state( False ) + if len(self._state_stack) >= 1: + self._current_state = self._state_stack.pop() + else: + self._current_state = None + if self._current_state is not None: + self._current_state.set_screen(self.screen()) + self._current_state.entering_state( True ) + + def current_state(self): + return self._current_state + + def running(self): + if not self._running: + return False + if self.current_state() is None: + return False + state_running = self.current_state().running() + if not state_running: + self.pop_state() + return self.running() + return True + + def set_running(self, new_value): + self._running = new_value + + def set_fps(self, new_value): + self._fps = new_value + + def fps(self): + return self._fps + + def initialize(self): + pygame.init() + + def shutdown(self): + pygame.quit() + + def runLoop(self): + self.initialize() + clock = pygame.time.Clock() + while self.running() : + ms = clock.tick( self.fps() ) + #try: + self.current_state().loop(ms) + #except: + # print "Unexpected error:", sys.exc_info()[0] + self.shutdown() |