comparison gamelib/engine.py @ 151:082868bea873

Refactor UI so that only a single gui.App is used. Pass all UI events via main_app. Change Toolbar table to use .td() everywhere. Move toolbar to top.
author Simon Cross <hodgestar@gmail.com>
date Thu, 03 Sep 2009 20:32:56 +0000
parents 1597081e5a84
children 210fc1ea0516
comparison
equal deleted inserted replaced
150:89d2360d4350 151:082868bea873
12 12
13 class Engine(Game): 13 class Engine(Game):
14 def __init__(self, main_app): 14 def __init__(self, main_app):
15 self.main_app = main_app 15 self.main_app = main_app
16 self.clock = pygame.time.Clock() 16 self.clock = pygame.time.Clock()
17 self.main_menu = mainmenu.make_main_menu()
18 self._open_window = None
17 19
18 def tick(self): 20 def tick(self):
19 """Tic toc.""" 21 """Tic toc."""
20 pygame.time.wait(10) 22 pygame.time.wait(10)
21 23
24 def open_window(self, window):
25 """Open a widget as the main window."""
26 if self._open_window is not None:
27 self.main_app.close(self._open_window)
28 self.main_app.open(window)
29 self._open_window = window
30
22 def create_game_board(self): 31 def create_game_board(self):
23 self.gameboard = gameboard.GameBoard() 32 """Create and open a gameboard window."""
33 self.gameboard = gameboard.GameBoard(self.main_app)
34 self.open_window(self.gameboard.get_top_widget())
24 35
25 def set_main_menu(self): 36 def set_main_menu(self):
26 """Create the main menu""" 37 """Open the main menu"""
27 mainmenu.add_main_menu(self.main_app) 38 self.open_window(self.main_menu)
28 39
29 def generate_score(self): 40 def create_game_over(self):
30 """Create the Game Over state""" 41 """Create and open the Game Over window"""
31 gameover.add_game_over(self.main_app, self.gameboard) 42 game_over = gameover.create_game_over(self.gameboard)
43 self.open_window(game_over)
32 44
33 class MainMenuState(State): 45 class MainMenuState(State):
34 def init(self): 46 def init(self):
35 self.game.set_main_menu() 47 self.game.set_main_menu()
36 48
72 84
73 def event(self, e): 85 def event(self, e):
74 if events_equal(e, START_NIGHT): 86 if events_equal(e, START_NIGHT):
75 return NightState(self.game) 87 return NightState(self.game)
76 elif e.type is KEYDOWN and e.key == K_ESCAPE: 88 elif e.type is KEYDOWN and e.key == K_ESCAPE:
77 return MainMenuState(self.game) 89 return GameOver(self.game)
78 elif e.type is KEYDOWN and e.key == K_n: 90 elif e.type is KEYDOWN and e.key == K_n:
79 return pygame.event.post(START_NIGHT) 91 return pygame.event.post(START_NIGHT)
80 elif events_equal(e, GO_MAIN_MENU): 92 elif events_equal(e, GO_MAIN_MENU):
81 return MainMenuState(self.game) 93 return MainMenuState(self.game)
82 elif e.type is not QUIT: 94 elif e.type is not QUIT:
83 self.game.gameboard.event(e) 95 self.game.main_app.event(e)
84 96
85 def paint(self, screen): 97 def paint(self, screen):
86 self.game.gameboard.paint(screen) 98 self.game.main_app.paint(screen)
87 pygame.display.flip() 99 pygame.display.flip()
88 100
89 def update(self, screen): 101 def update(self, screen):
90 update = self.game.gameboard.update(screen) 102 self.game.gameboard.update()
103 update = self.game.main_app.update(screen)
91 pygame.display.update(update) 104 pygame.display.update(update)
92 105
93 def loop(self): 106 def loop(self):
94 self.game.gameboard.loop() 107 self.game.gameboard.loop()
95 108
114 return GameOver(self.game) 127 return GameOver(self.game)
115 return DayState(self.game) 128 return DayState(self.game)
116 elif e.type is KEYDOWN and e.key == K_d: 129 elif e.type is KEYDOWN and e.key == K_d:
117 return pygame.event.post(START_DAY) 130 return pygame.event.post(START_DAY)
118 elif e.type is KEYDOWN and e.key == K_ESCAPE: 131 elif e.type is KEYDOWN and e.key == K_ESCAPE:
119 return MainMenuState(self.game) 132 return GameOver(self.game)
120 elif e.type is MOVE_FOX_ID: 133 elif e.type is MOVE_FOX_ID:
121 self.cycle_count += 1 134 self.cycle_count += 1
122 if self.cycle_count > constants.NIGHT_LENGTH: 135 if self.cycle_count > constants.NIGHT_LENGTH:
123 return pygame.event.post(START_DAY) 136 return pygame.event.post(START_DAY)
124 if self.game.gameboard.move_foxes(): 137 if self.game.gameboard.move_foxes():
125 # All foxes are gone/safe, so dawn happens 138 # All foxes are gone/safe, so dawn happens
126 return pygame.event.post(START_DAY) 139 return pygame.event.post(START_DAY)
127 elif e.type is not QUIT: 140 elif e.type is not QUIT:
128 self.game.gameboard.event(e) 141 self.game.main_app.event(e)
129 142
130 def loop(self): 143 def loop(self):
131 self.game.gameboard.loop() 144 self.game.gameboard.loop()
132 145
133 def paint(self, screen): 146 def paint(self, screen):
134 self.game.gameboard.paint(screen) 147 self.game.main_app.paint(screen)
135 pygame.display.flip() 148 pygame.display.flip()
136 149
137 def update(self, screen): 150 def update(self, screen):
138 update = self.game.gameboard.update(screen) 151 self.game.gameboard.update()
152 update = self.game.main_app.update(screen)
139 pygame.display.update(update) 153 pygame.display.update(update)
140 154
141 class GameOver(State): 155 class GameOver(State):
142 def init(self): 156 def init(self):
143 """Setup everything""" 157 """Setup everything"""
144 self.game.generate_score() 158 self.game.create_game_over()
145 pygame.time.set_timer(MOVE_FOX_ID, 0) 159 pygame.time.set_timer(MOVE_FOX_ID, 0)
146 160
147 def event(self, e): 161 def event(self, e):
148 if e.type is KEYDOWN: 162 if e.type is KEYDOWN:
149 if e.key == K_ESCAPE: 163 if e.key == K_ESCAPE: