def run_wrapped(stdscr): curses.use_default_colors() curses.curs_set(0) stdscr.nodelay(1) curses.init_pair(1, curses.COLOR_BLUE, -1) curses.init_pair(2, curses.COLOR_RED, -1) curses.init_pair(3, curses.COLOR_GREEN, -1) curses.init_pair(4, curses.COLOR_YELLOW, -1) while not self.tron.game_over: self.status = 'Turn %d.' % self.tron.turn if self.human: self.human.action = player.GO_FORWARD now = util.milliseconds() diff = now - self.last_update if diff < self.human_delay: curses.napms(self.human_delay - diff) self.last_update = util.milliseconds() key = stdscr.getch() while key != -1: if key == ord('q'): sys.exit(0) elif self.human: if key in [ord(','), curses.KEY_LEFT]: self.human.action = player.TURN_LEFT elif key in [ord('.'), curses.KEY_RIGHT]: self.human.action = player.TURN_RIGHT key = stdscr.getch() self.tron.do_turn() self.draw(stdscr) if self.tron.winner >= 0: self.status = 'Game over: %s won' % self.tron.players[self.tron.winner].name else: self.status = 'Game over.' self.draw(stdscr) for num, p in enumerate(self.tron.players): stdscr.addstr(ROWS + num, 4, '%s survived %d turns.' % (p.name, p.survived)) stdscr.nodelay(0) stdscr.getch()
def __init__(self, args): players = [] self.human = None self.human_delay = 150 self.last_update = util.milliseconds() for arg in args: if arg == 'human': if self.human: raise Exception('Only one human player allowed') self.human = player.Player() players.append(self.human) else: players.append(ai.AIPlayer(arg)) self.tron = game.Tron((ROWS - 3, COLUMNS - 2), players)