def do_tick( self, indev ): if self.ingame_menu is None and not self.is_paused: if self.game_data.is_single_player() or \ self.game_data.is_single_random(): SingleSwitch.feed_keys( indev ) # in singleplayer, all joystick buttons are keypress for joy in indev.joys: if joy.any_went_down(): indev.key.feed_down( K_SPACE ) if joy.any_went_up(): indev.key.feed_up( K_SPACE ) if self.state == MonorailGame.STATE_INTRO: if self.hud.is_ready(): # or self.game_data.is_single_player(): self.hud.end_info() self.state = MonorailGame.STATE_BEGIN self.music_man.play() elif self.state == MonorailGame.STATE_BEGIN: if self.begin_timeout % 50 == 0: random_spawn = not self.game_data.is_single_player(); spawns_left = self.playfield.spawn_next_goldcar( random_spawn ) if spawns_left: self.begin_timeout += 50 self.controller.game_tick( indev ) self.playfield.game_tick() # Start right away in single player if self.game_data.is_single_player(): self.scenario.game_tick() self.begin_timeout -= 1 if self.begin_timeout <= 0: self.state = MonorailGame.STATE_GAME if indev.mouse.has_moved(): self.mouse_timeout = MonorailGame.MOUSE_TIMEOUT else: self.mouse_timeout -= 1 elif self.state == MonorailGame.STATE_GAME: self.controller.game_tick( indev ) self.playfield.game_tick() self.scenario.game_tick() if self.scenario.is_finished(): if not self.game_data.is_single_player(): self.hud.start_end_screen() else: self.game_data.get_quest().save_score( self.scenario ) skill = self.game_data.get_quest().get_skill( self.scenario ) self.game_data.skill_level.update( skill ) self.game_data.save_single_player_progress() if self.scenario.has_won(): self.hud.start_win_screen() else: self.hud.start_lose_screen() self.state = MonorailGame.STATE_STATS self.mouse_timeout = MonorailGame.MOUSE_TIMEOUT self.music_man.stop() if indev.mouse.has_moved(): self.mouse_timeout = MonorailGame.MOUSE_TIMEOUT else: self.mouse_timeout -= 1 elif self.state == MonorailGame.STATE_STATS: if self.hud.is_ready(): if not self.game_data.is_single_player(): self.game_data.add_total_scores( self.playfield ) self.hud.start_total_screen() self.state = MonorailGame.STATE_TOTAL else: if self.scenario.has_won(): self.state = MonorailGame.STATE_DONE else: self.restart( self.game_data ) return elif self.state == MonorailGame.STATE_TOTAL: if self.hud.is_ready(): self.state = MonorailGame.STATE_DONE elif self.state == MonorailGame.STATE_MENU: pass self.hud.game_tick( indev ) self.music_man.game_tick() SingleSwitch.tick( indev, None ) if indev.key.went_down( K_ESCAPE ) or \ self.hud.menu_btn.went_down() or \ SingleSwitch.esc_went_down: resman.get("gui.paper_sound").play() self.ingame_menu = IngameMenu(self.game_data.is_single_player(), self.game_data) elif self.ingame_menu is not None: # Ingame Menu SingleSwitch.feed_keys( indev ) self.gui_state.update( indev, self.ingame_menu ) self.ingame_menu.tick( indev, self.gui_state ) if self.ingame_menu.is_done(): if self.ingame_menu.to_menu: self.music_man.stop() self.state = MonorailGame.STATE_MENU elif self.ingame_menu.should_quit: self.music_man.stop() self.state = MonorailGame.STATE_QUIT elif self.ingame_menu.to_next_level: self.music_man.stop() self.state = MonorailGame.STATE_DONE self.ingame_menu = None self.mouse_timeout = MonorailGame.MOUSE_TIMEOUT # if indev.key.went_down( K_p ): # self.is_paused = not self.is_paused event.Event.update() # for debugging if self.is_paused: self.controller.game_tick( indev )
def do_tick(self, indev): if self.ingame_menu is None and not self.is_paused: if self.game_data.is_single_player() or \ self.game_data.is_single_random(): SingleSwitch.feed_keys(indev) # in singleplayer, all joystick buttons are keypress for joy in indev.joys: if joy.any_went_down(): indev.key.feed_down(K_SPACE) if joy.any_went_up(): indev.key.feed_up(K_SPACE) if self.state == MonorailGame.STATE_INTRO: if self.hud.is_ready( ): # or self.game_data.is_single_player(): self.hud.end_info() self.state = MonorailGame.STATE_BEGIN self.music_man.play() elif self.state == MonorailGame.STATE_BEGIN: if self.begin_timeout % 50 == 0: random_spawn = not self.game_data.is_single_player() spawns_left = self.playfield.spawn_next_goldcar( random_spawn) if spawns_left: self.begin_timeout += 50 self.controller.game_tick(indev) self.playfield.game_tick() # Start right away in single player if self.game_data.is_single_player(): self.scenario.game_tick() self.begin_timeout -= 1 if self.begin_timeout <= 0: self.state = MonorailGame.STATE_GAME if indev.mouse.has_moved(): self.mouse_timeout = MonorailGame.MOUSE_TIMEOUT else: self.mouse_timeout -= 1 elif self.state == MonorailGame.STATE_GAME: self.controller.game_tick(indev) self.playfield.game_tick() self.scenario.game_tick() if self.scenario.is_finished(): if not self.game_data.is_single_player(): self.hud.start_end_screen() else: self.game_data.get_quest().save_score(self.scenario) skill = self.game_data.get_quest().get_skill( self.scenario) self.game_data.skill_level.update(skill) self.game_data.save_single_player_progress() if self.scenario.has_won(): self.hud.start_win_screen() else: self.hud.start_lose_screen() self.state = MonorailGame.STATE_STATS self.mouse_timeout = MonorailGame.MOUSE_TIMEOUT self.music_man.stop() if indev.mouse.has_moved(): self.mouse_timeout = MonorailGame.MOUSE_TIMEOUT else: self.mouse_timeout -= 1 elif self.state == MonorailGame.STATE_STATS: if self.hud.is_ready(): if not self.game_data.is_single_player(): self.game_data.add_total_scores(self.playfield) self.hud.start_total_screen() self.state = MonorailGame.STATE_TOTAL else: if self.scenario.has_won(): self.state = MonorailGame.STATE_DONE else: self.restart(self.game_data) return elif self.state == MonorailGame.STATE_TOTAL: if self.hud.is_ready(): self.state = MonorailGame.STATE_DONE elif self.state == MonorailGame.STATE_MENU: pass self.hud.game_tick(indev) self.music_man.game_tick() SingleSwitch.tick(indev, None) if indev.key.went_down( K_ESCAPE ) or \ self.hud.menu_btn.went_down() or \ SingleSwitch.esc_went_down or \ indev.joys.any_went_down( Joystick.BTN_BACK ): resman.get("gui.paper_sound").play() self.ingame_menu = IngameMenu( self.game_data.is_single_player(), self.game_data) elif self.ingame_menu is not None: # Ingame Menu SingleSwitch.feed_keys(indev) self.gui_state.update(indev, self.ingame_menu) self.ingame_menu.tick(indev, self.gui_state) if self.ingame_menu.is_done(): if self.ingame_menu.to_menu: self.music_man.stop() self.state = MonorailGame.STATE_MENU elif self.ingame_menu.should_quit: self.music_man.stop() self.state = MonorailGame.STATE_QUIT elif self.ingame_menu.to_next_level: self.music_man.stop() self.state = MonorailGame.STATE_DONE self.ingame_menu = None self.mouse_timeout = MonorailGame.MOUSE_TIMEOUT # if indev.key.went_down( K_p ): # self.is_paused = not self.is_paused event.Event.update() # for debugging if self.is_paused: self.controller.game_tick(indev)
class MonorailGame: STATE_INTRO, STATE_BEGIN, STATE_GAME, STATE_MENU, STATE_QUIT, STATE_STATS, STATE_TOTAL,\ STATE_DONE = range( 8 ) MOUSE_TIMEOUT = 25 * 3 def __init__( self, game_data ): self.restart( game_data ) self.music_man = MusicManager() # preload clock sounds and big explosion graphic resman.get("game.clock_sound") resman.get("game.clockring_sound") resman.get("game.explosion_sprite") def restart( self, game_data ): """Start a new game with the current game_data""" self.game_data = game_data self.state = MonorailGame.STATE_INTRO self.scenario = self.game_data.get_quest().create_scenario(self.game_data.skill_level.value) self.playfield = self.scenario.playfield self.controller = ctrl.GroundControl( self.playfield ) self.init_goldcars() self.hud = Hud( self.scenario, self.controller, self.game_data ) self.hud.start_intro_screen() self.begin_timeout = 25 * 3 self.ingame_menu = None self.gui_state = GuiState() self.mouse_timeout = MonorailGame.MOUSE_TIMEOUT self.is_paused = False def init_goldcars( self ): goldcar_names = [] controllers = [] for name, controller in self.game_data.goldcars: goldcar_names.append( name ) controllers.append( controller ) for iq in self.game_data.get_quest().get_opponent_iqs(): goldcar_names.append( "" ) controllers.append( ctrl.AiController( None, iq ) ) self.playfield.add_goldcars( goldcar_names ) self.controller.add_controllers( controllers ) def do_tick( self, indev ): if self.ingame_menu is None and not self.is_paused: if self.game_data.is_single_player() or \ self.game_data.is_single_random(): SingleSwitch.feed_keys( indev ) # in singleplayer, all joystick buttons are keypress for joy in indev.joys: if joy.any_went_down(): indev.key.feed_down( K_SPACE ) if joy.any_went_up(): indev.key.feed_up( K_SPACE ) if self.state == MonorailGame.STATE_INTRO: if self.hud.is_ready(): # or self.game_data.is_single_player(): self.hud.end_info() self.state = MonorailGame.STATE_BEGIN self.music_man.play() elif self.state == MonorailGame.STATE_BEGIN: if self.begin_timeout % 50 == 0: random_spawn = not self.game_data.is_single_player(); spawns_left = self.playfield.spawn_next_goldcar( random_spawn ) if spawns_left: self.begin_timeout += 50 self.controller.game_tick( indev ) self.playfield.game_tick() # Start right away in single player if self.game_data.is_single_player(): self.scenario.game_tick() self.begin_timeout -= 1 if self.begin_timeout <= 0: self.state = MonorailGame.STATE_GAME if indev.mouse.has_moved(): self.mouse_timeout = MonorailGame.MOUSE_TIMEOUT else: self.mouse_timeout -= 1 elif self.state == MonorailGame.STATE_GAME: self.controller.game_tick( indev ) self.playfield.game_tick() self.scenario.game_tick() if self.scenario.is_finished(): if not self.game_data.is_single_player(): self.hud.start_end_screen() else: self.game_data.get_quest().save_score( self.scenario ) skill = self.game_data.get_quest().get_skill( self.scenario ) self.game_data.skill_level.update( skill ) self.game_data.save_single_player_progress() if self.scenario.has_won(): self.hud.start_win_screen() else: self.hud.start_lose_screen() self.state = MonorailGame.STATE_STATS self.mouse_timeout = MonorailGame.MOUSE_TIMEOUT self.music_man.stop() if indev.mouse.has_moved(): self.mouse_timeout = MonorailGame.MOUSE_TIMEOUT else: self.mouse_timeout -= 1 elif self.state == MonorailGame.STATE_STATS: if self.hud.is_ready(): if not self.game_data.is_single_player(): self.game_data.add_total_scores( self.playfield ) self.hud.start_total_screen() self.state = MonorailGame.STATE_TOTAL else: if self.scenario.has_won(): self.state = MonorailGame.STATE_DONE else: self.restart( self.game_data ) return elif self.state == MonorailGame.STATE_TOTAL: if self.hud.is_ready(): self.state = MonorailGame.STATE_DONE elif self.state == MonorailGame.STATE_MENU: pass self.hud.game_tick( indev ) self.music_man.game_tick() SingleSwitch.tick( indev, None ) if indev.key.went_down( K_ESCAPE ) or \ self.hud.menu_btn.went_down() or \ SingleSwitch.esc_went_down: resman.get("gui.paper_sound").play() self.ingame_menu = IngameMenu(self.game_data.is_single_player(), self.game_data) elif self.ingame_menu is not None: # Ingame Menu SingleSwitch.feed_keys( indev ) self.gui_state.update( indev, self.ingame_menu ) self.ingame_menu.tick( indev, self.gui_state ) if self.ingame_menu.is_done(): if self.ingame_menu.to_menu: self.music_man.stop() self.state = MonorailGame.STATE_MENU elif self.ingame_menu.should_quit: self.music_man.stop() self.state = MonorailGame.STATE_QUIT elif self.ingame_menu.to_next_level: self.music_man.stop() self.state = MonorailGame.STATE_DONE self.ingame_menu = None self.mouse_timeout = MonorailGame.MOUSE_TIMEOUT # if indev.key.went_down( K_p ): # self.is_paused = not self.is_paused event.Event.update() # for debugging if self.is_paused: self.controller.game_tick( indev ) def draw( self, surface, interpol, time_sec ): #surface.fill( (0,0,0) ) frame = Frame( surface, time_sec, interpol ) if self.ingame_menu is not None or self.is_paused or\ self.state not in [MonorailGame.STATE_BEGIN, MonorailGame.STATE_GAME]: frame.interpol = 0.0 frame.draw( self.playfield ) frame.draw( self.controller ) self.hud.draw( frame ) if self.ingame_menu is not None: self.ingame_menu.draw( surface ) frame.draw( event.Event.instance ) def draw_mouse( self, surface, interpol, time_sec ): if self.mouse_timeout > 0: x, y = pygame.mouse.get_pos() resman.get("gui_surf").draw( surface, Vec2D(x, y), (0,0,32,32) ) def mouse_down( self, button ): pass def is_done( self ): return self.state == MonorailGame.STATE_DONE \ or self.state == MonorailGame.STATE_MENU \ or self.state == MonorailGame.STATE_QUIT
class MonorailGame: STATE_INTRO, STATE_BEGIN, STATE_GAME, STATE_MENU, STATE_QUIT, STATE_STATS, STATE_TOTAL,\ STATE_DONE = range( 8 ) MOUSE_TIMEOUT = 25 * 3 def __init__(self, game_data): self.restart(game_data) self.music_man = MusicManager() # preload clock sounds and big explosion graphic resman.get("game.clock_sound") resman.get("game.clockring_sound") resman.get("game.explosion_sprite") def restart(self, game_data): """Start a new game with the current game_data""" self.game_data = game_data self.state = MonorailGame.STATE_INTRO self.scenario = self.game_data.get_quest().create_scenario( self.game_data.skill_level.value) self.playfield = self.scenario.playfield self.controller = ctrl.GroundControl(self.playfield) self.init_goldcars() self.hud = Hud(self.scenario, self.controller, self.game_data) self.hud.start_intro_screen() self.begin_timeout = 25 * 3 self.ingame_menu = None self.gui_state = GuiState() self.mouse_timeout = MonorailGame.MOUSE_TIMEOUT self.is_paused = False def init_goldcars(self): goldcar_names = [] controllers = [] for name, controller in self.game_data.goldcars: goldcar_names.append(name) controllers.append(controller) for iq in self.game_data.get_quest().get_opponent_iqs(): goldcar_names.append("") controllers.append(ctrl.AiController(None, iq)) self.playfield.add_goldcars(goldcar_names) self.controller.add_controllers(controllers) def do_tick(self, indev): if self.ingame_menu is None and not self.is_paused: if self.game_data.is_single_player() or \ self.game_data.is_single_random(): SingleSwitch.feed_keys(indev) # in singleplayer, all joystick buttons are keypress for joy in indev.joys: if joy.any_went_down(): indev.key.feed_down(K_SPACE) if joy.any_went_up(): indev.key.feed_up(K_SPACE) if self.state == MonorailGame.STATE_INTRO: if self.hud.is_ready( ): # or self.game_data.is_single_player(): self.hud.end_info() self.state = MonorailGame.STATE_BEGIN self.music_man.play() elif self.state == MonorailGame.STATE_BEGIN: if self.begin_timeout % 50 == 0: random_spawn = not self.game_data.is_single_player() spawns_left = self.playfield.spawn_next_goldcar( random_spawn) if spawns_left: self.begin_timeout += 50 self.controller.game_tick(indev) self.playfield.game_tick() # Start right away in single player if self.game_data.is_single_player(): self.scenario.game_tick() self.begin_timeout -= 1 if self.begin_timeout <= 0: self.state = MonorailGame.STATE_GAME if indev.mouse.has_moved(): self.mouse_timeout = MonorailGame.MOUSE_TIMEOUT else: self.mouse_timeout -= 1 elif self.state == MonorailGame.STATE_GAME: self.controller.game_tick(indev) self.playfield.game_tick() self.scenario.game_tick() if self.scenario.is_finished(): if not self.game_data.is_single_player(): self.hud.start_end_screen() else: self.game_data.get_quest().save_score(self.scenario) skill = self.game_data.get_quest().get_skill( self.scenario) self.game_data.skill_level.update(skill) self.game_data.save_single_player_progress() if self.scenario.has_won(): self.hud.start_win_screen() else: self.hud.start_lose_screen() self.state = MonorailGame.STATE_STATS self.mouse_timeout = MonorailGame.MOUSE_TIMEOUT self.music_man.stop() if indev.mouse.has_moved(): self.mouse_timeout = MonorailGame.MOUSE_TIMEOUT else: self.mouse_timeout -= 1 elif self.state == MonorailGame.STATE_STATS: if self.hud.is_ready(): if not self.game_data.is_single_player(): self.game_data.add_total_scores(self.playfield) self.hud.start_total_screen() self.state = MonorailGame.STATE_TOTAL else: if self.scenario.has_won(): self.state = MonorailGame.STATE_DONE else: self.restart(self.game_data) return elif self.state == MonorailGame.STATE_TOTAL: if self.hud.is_ready(): self.state = MonorailGame.STATE_DONE elif self.state == MonorailGame.STATE_MENU: pass self.hud.game_tick(indev) self.music_man.game_tick() SingleSwitch.tick(indev, None) if indev.key.went_down( K_ESCAPE ) or \ self.hud.menu_btn.went_down() or \ SingleSwitch.esc_went_down or \ indev.joys.any_went_down( Joystick.BTN_BACK ): resman.get("gui.paper_sound").play() self.ingame_menu = IngameMenu( self.game_data.is_single_player(), self.game_data) elif self.ingame_menu is not None: # Ingame Menu SingleSwitch.feed_keys(indev) self.gui_state.update(indev, self.ingame_menu) self.ingame_menu.tick(indev, self.gui_state) if self.ingame_menu.is_done(): if self.ingame_menu.to_menu: self.music_man.stop() self.state = MonorailGame.STATE_MENU elif self.ingame_menu.should_quit: self.music_man.stop() self.state = MonorailGame.STATE_QUIT elif self.ingame_menu.to_next_level: self.music_man.stop() self.state = MonorailGame.STATE_DONE self.ingame_menu = None self.mouse_timeout = MonorailGame.MOUSE_TIMEOUT # if indev.key.went_down( K_p ): # self.is_paused = not self.is_paused event.Event.update() # for debugging if self.is_paused: self.controller.game_tick(indev) def draw(self, surface, interpol, time_sec): #surface.fill( (0,0,0) ) frame = Frame(surface, time_sec, interpol) if self.ingame_menu is not None or self.is_paused or\ self.state not in [MonorailGame.STATE_BEGIN, MonorailGame.STATE_GAME]: frame.interpol = 0.0 frame.draw(self.playfield) frame.draw(self.controller) self.hud.draw(frame) if self.ingame_menu is not None: self.ingame_menu.draw(surface) frame.draw(event.Event.instance) def draw_mouse(self, surface, interpol, time_sec): if self.mouse_timeout > 0: x, y = pygame.mouse.get_pos() resman.get("gui_surf").draw(surface, Vec2D(x, y), (0, 0, 32, 32)) def mouse_down(self, button): pass def is_done(self): return self.state == MonorailGame.STATE_DONE \ or self.state == MonorailGame.STATE_MENU \ or self.state == MonorailGame.STATE_QUIT