Mercurial > touhou
comparison eosd @ 460:ec327e58b477
Add a context manager to initialize and shut down SDL outside of Window.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Fri, 06 Sep 2013 23:29:19 +0200 |
parents | a502887557ac |
children | 5f5955635d2c |
comparison
equal
deleted
inserted
replaced
459:6e733ed817bd | 460:ec327e58b477 |
---|---|
51 | 51 |
52 | 52 |
53 import sys | 53 import sys |
54 import logging | 54 import logging |
55 | 55 |
56 from pytouhou.lib.sdl import SDL | |
56 from pytouhou.ui.window import Window | 57 from pytouhou.ui.window import Window |
57 from pytouhou.resource.loader import Loader | 58 from pytouhou.resource.loader import Loader |
58 from pytouhou.ui.gamerunner import GameRunner | 59 from pytouhou.ui.gamerunner import GameRunner |
59 from pytouhou.game.player import PlayerState, GameOver | 60 from pytouhou.game.player import PlayerState, GameOver |
60 from pytouhou.formats.t6rp import T6RP, Level | 61 from pytouhou.formats.t6rp import T6RP, Level |
106 resource_loader.scan_archives(data) | 107 resource_loader.scan_archives(data) |
107 except IOError: | 108 except IOError: |
108 sys.stderr.write('Some data files were not found, did you forget the -p option?\n') | 109 sys.stderr.write('Some data files were not found, did you forget the -p option?\n') |
109 exit(1) | 110 exit(1) |
110 | 111 |
111 window = Window(double_buffer=(not single_buffer), fps_limit=fps_limit, fixed_pipeline=fixed_pipeline) | 112 with SDL(): |
112 | 113 window = Window(double_buffer=(not single_buffer), fps_limit=fps_limit, fixed_pipeline=fixed_pipeline) |
113 if stage_num is None: | 114 |
114 story = True | 115 if stage_num is None: |
115 stage_num = 1 | 116 story = True |
116 continues = 3 | 117 stage_num = 1 |
117 else: | 118 continues = 3 |
118 story = False | 119 else: |
119 continues = 0 | 120 story = False |
120 | 121 continues = 0 |
121 if debug: | 122 |
122 if not verbosity: | 123 if debug: |
123 verbosity = 'DEBUG' | 124 if not verbosity: |
124 continues = float('inf') | 125 verbosity = 'DEBUG' |
125 | 126 continues = float('inf') |
126 if verbosity: | 127 |
127 logging.basicConfig(level=logging.__getattribute__(verbosity)) | 128 if verbosity: |
128 | 129 logging.basicConfig(level=logging.__getattribute__(verbosity)) |
129 if replay: | 130 |
130 with open(replay, 'rb') as file: | |
131 replay = T6RP.read(file) | |
132 rank = replay.rank | |
133 character = replay.character | |
134 | |
135 save_keystates = None | |
136 if save_filename: | |
137 save_replay = T6RP() | |
138 save_replay.rank = rank | |
139 save_replay.character = character | |
140 | |
141 if hints: | |
142 with open(hints, 'rb') as file: | |
143 hints = Hint.read(file) | |
144 | |
145 difficulty = 16 | |
146 default_power = [0, 64, 128, 128, 128, 128, 0][stage_num - 1] | |
147 states = [PlayerState(character=character, power=default_power)] | |
148 | |
149 game_class = GameBossRush if boss_rush else Game | |
150 | |
151 common = Common(resource_loader) | |
152 runner = GameRunner(window, resource_loader, skip=skip_replay) | |
153 while True: | |
154 if replay: | 131 if replay: |
155 level = replay.levels[stage_num - 1] | 132 with open(replay, 'rb') as file: |
156 if not level: | 133 replay = T6RP.read(file) |
157 raise Exception | 134 rank = replay.rank |
158 | 135 character = replay.character |
159 prng = Random(level.random_seed) | 136 |
160 | 137 save_keystates = None |
161 #TODO: apply the replay to the other players. | |
162 #TODO: see if the stored score is used or if it’s the one from the previous stage. | |
163 if stage_num != 1 and stage_num - 2 in replay.levels: | |
164 previous_level = replay.levels[stage_num - 1] | |
165 states[0].score = previous_level.score | |
166 states[0].effective_score = previous_level.score | |
167 states[0].points = level.point_items | |
168 states[0].power = level.power | |
169 states[0].lives = level.lives | |
170 states[0].bombs = level.bombs | |
171 difficulty = level.difficulty | |
172 else: | |
173 prng = Random() | |
174 | |
175 if save_filename: | 138 if save_filename: |
176 if not replay: | 139 save_replay = T6RP() |
177 save_replay.levels[stage_num - 1] = level = Level() | 140 save_replay.rank = rank |
178 level.score = states[0].score | 141 save_replay.character = character |
179 level.random_seed = prng.seed | 142 |
180 level.point_items = states[0].points | 143 if hints: |
181 level.power = states[0].power | 144 with open(hints, 'rb') as file: |
182 level.lives = states[0].lives | 145 hints = Hint.read(file) |
183 level.bombs = states[0].bombs | 146 |
184 level.difficulty = difficulty | 147 difficulty = 16 |
185 save_keystates = [] | 148 default_power = [0, 64, 128, 128, 128, 128, 0][stage_num - 1] |
186 | 149 states = [PlayerState(character=character, power=default_power)] |
187 hints_stage = hints.stages[stage_num - 1] if hints else None | 150 |
188 | 151 game_class = GameBossRush if boss_rush else Game |
189 game = game_class(resource_loader, states, stage_num, rank, difficulty, common, prng=prng, continues=continues, hints=hints_stage) | 152 |
190 | 153 common = Common(resource_loader) |
191 if not enable_particles: | 154 runner = GameRunner(window, resource_loader, skip=skip_replay) |
192 def new_particle(pos, anim, amp, number=1, reverse=False, duration=24): | 155 while True: |
193 pass | 156 if replay: |
194 game.new_particle = new_particle | 157 level = replay.levels[stage_num - 1] |
195 | 158 if not level: |
196 background = game.background if enable_background else None | 159 raise Exception |
197 bgms = game.std.bgms if enable_music else None | 160 |
198 | 161 prng = Random(level.random_seed) |
199 # Main loop | 162 |
200 runner.load_game(game, background, bgms, replay, save_keystates) | 163 #TODO: apply the replay to the other players. |
201 window.set_runner(runner) | 164 #TODO: see if the stored score is used or if it’s the one from the previous stage. |
202 try: | 165 if stage_num != 1 and stage_num - 2 in replay.levels: |
203 window.run() | 166 previous_level = replay.levels[stage_num - 1] |
204 break | 167 states[0].score = previous_level.score |
205 except NextStage: | 168 states[0].effective_score = previous_level.score |
206 if not story or stage_num == (7 if boss_rush else 6 if rank > 0 else 5): | 169 states[0].points = level.point_items |
170 states[0].power = level.power | |
171 states[0].lives = level.lives | |
172 states[0].bombs = level.bombs | |
173 difficulty = level.difficulty | |
174 else: | |
175 prng = Random() | |
176 | |
177 if save_filename: | |
178 if not replay: | |
179 save_replay.levels[stage_num - 1] = level = Level() | |
180 level.score = states[0].score | |
181 level.random_seed = prng.seed | |
182 level.point_items = states[0].points | |
183 level.power = states[0].power | |
184 level.lives = states[0].lives | |
185 level.bombs = states[0].bombs | |
186 level.difficulty = difficulty | |
187 save_keystates = [] | |
188 | |
189 hints_stage = hints.stages[stage_num - 1] if hints else None | |
190 | |
191 game = game_class(resource_loader, states, stage_num, rank, difficulty, common, prng=prng, continues=continues, hints=hints_stage) | |
192 | |
193 if not enable_particles: | |
194 def new_particle(pos, anim, amp, number=1, reverse=False, duration=24): | |
195 pass | |
196 game.new_particle = new_particle | |
197 | |
198 background = game.background if enable_background else None | |
199 bgms = game.std.bgms if enable_music else None | |
200 | |
201 # Main loop | |
202 runner.load_game(game, background, bgms, replay, save_keystates) | |
203 window.set_runner(runner) | |
204 try: | |
205 window.run() | |
207 break | 206 break |
208 stage_num += 1 | 207 except NextStage: |
209 states = [player.state.copy() for player in game.players] # if player.state.lives >= 0] | 208 if not story or stage_num == (7 if boss_rush else 6 if rank > 0 else 5): |
210 except GameOver: | 209 break |
211 print('Game over') | 210 stage_num += 1 |
212 break | 211 states = [player.state.copy() for player in game.players] # if player.state.lives >= 0] |
213 finally: | 212 except GameOver: |
214 if save_filename: | 213 print('Game over') |
215 last_key = -1 | 214 break |
216 for time, key in enumerate(save_keystates): | 215 finally: |
217 if key != last_key: | 216 if save_filename: |
218 level.keys.append((time, key, 0)) | 217 last_key = -1 |
219 last_key = key | 218 for time, key in enumerate(save_keystates): |
219 if key != last_key: | |
220 level.keys.append((time, key, 0)) | |
221 last_key = key | |
220 | 222 |
221 if save_filename: | 223 if save_filename: |
222 with open(save_filename, 'wb+') as file: | 224 with open(save_filename, 'wb+') as file: |
223 save_replay.write(file) | 225 save_replay.write(file) |
224 | 226 |