def bind_keys(self): import input game = self.game def stop(event): self.stop() game.quit(event) game.inputMgr.bind(input.key_down_type('q'), stop) # game.inputMgr.bind(input.key_down_type('q'), game.quit) game.inputMgr.bind(input.key_down_type('z'), lambda e: exit(0))
def run(self, isRun): if not (isRun or RUN_ALL): return game = self.game = Game() game.inputMgr.bind(input.key_down_type('q'), game.quit) game.inputMgr.bind(input.key_down_type('z'), lambda e: exit(0)) game.inputMgr.bind(input.key_down_type('p'), game.pause) self.display = Display() game.setup_stage(self.configData, self.display) self.extra_config() game.start() game.mainloop()
def __init__(self, game, display): self.game = game game.rootMenu = self self.display = display # setup menu stage self.setup_empty_stage() self.image = pygame.Surface((config.SCREEN_W, config.SCREEN_H)).convert_alpha() self.rect = (0, 0) K = input.key def record_on(ctx): config.RECORD = 1 def record_off(ctx): config.RECORD = 0 def play_human_vs_AI(ctx): game.setup_stage({ 'world-size':(16, 16), 'snakes': [ ((8, 8), Directions.RIGHT, 5), ((7, 7), Directions.RIGHT, 5), ], 'rule': rule[0], }, display) game.join_human_player("Player", [K('w'), K('s'), K('a'), K('d')]) game.join_player(AIPlayer("AI Player")) game.start() def play_human_vs_human(ctx): game.setup_stage({ 'world-size':(16, 16), 'snakes': [ ((8, 8), Directions.RIGHT, 5), ((7, 7), Directions.RIGHT, 5), ], 'rule': rule[0], }, display) game.join_human_player("Foo", [K('w'), K('s'), K('a'), K('d')]) game.join_human_player("Bar", [K('i'), K('k'), K('j'), K('l')]) game.start() play_4_AI = play_AI_vs_AI = play_human_vs_human # def play_AI_vs_AI(ctx): # pass # def play_4_AI(ctx): # pass rule = [(gamerule.DeathModeRule, ())] def mode_set(mode, args): def setter(ctx): rule[0] = (mode, args) return setter super(RootMenu, self).__init__('$>', [ Menu('play', [ Menu('1: human v.s. AI', [], callback=play_human_vs_AI), Menu('2: human v.s. human', [], callback=play_human_vs_human), Menu('3: AI v.s. AI', [], callback=play_AI_vs_AI), Menu('4: 4 AI', [], callback=play_4_AI), ]), Choices('game rule', [ Option('death mode', callback=mode_set(gamerule.DeathModeRule, ())), Option('scoring mode', callback=mode_set(gamerule.ScoringModeRule, (30,))), Option('fixed round mode', callback=mode_set(gamerule.FixedRoundModeRule, (128,))), ]), Help('help'), Choices('set record', [ Option('record on', callback=record_on), Option('record off', callback=record_off), ]), InputBox('record'), ReplayRecord('replays'), Choices('quit', [ Option('confirm', callback=lambda ctx: exit(0)), ]) ], cancel=False) self.ctx = Context(self) # bind keys inputMgr = InputManager.get_instance() def handle_key(event): if game.state == game.ST_MENU: self.ctx.handle_key(event) inputMgr.bind(input.key_down_type(), handle_key) def escape(event): if game.state != game.ST_MENU: game.quit() else: exit(0) inputMgr.bind(input.key_down_type('ESCAPE'), escape) def back_to_menu(event): self.setup_empty_stage() inputMgr.bind(input.key_down_type('HOME'), back_to_menu)