blob: cd6b863217998d57b649319aa1e127327cd80dae (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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()
|