def __init__(self): menu_data = ( 'Options', 'New game', 'How to play', 'Quit' ) self.menu = NonBlockingPopupMenu(menu_data)
vx = .015 vy = .023 radius = .05 dt = 20 ## Menu data and functions. global menu_data global _surface menu_data = ( 'Main', 'Save', 'Quit', ) menu = NonBlockingPopupMenu(menu_data) def handle_menu(e): global menu print 'Menu event: %s.%d: %s' % (e.name, e.item_id, e.text) if e.name is None: print 'Hide menu' menu.hide() elif e.name == 'Main': print 'I am in the menu' if e.text == 'Save': print 'I hit save' stddraw.save(_surface) elif e.text == 'Quit': quit()
class GameController(object): def __init__(self): menu_data = ( 'Options', 'New game', 'How to play', 'Quit' ) self.menu = NonBlockingPopupMenu(menu_data) def handle_menu(self, event, game, gameRenderer): if event.name is None: self.menu.hide() elif event.name == 'Options': if event.text == 'Quit': exit(0) elif event.text == 'How to play': self.menu.hide() game_state = game.state() game.reset() self.run_tutorial(game, gameRenderer) game.reset(game_state) elif event.text == 'New game': self.menu.hide() game.reset() def make_move(self, hole_moved_indice, game, gameRenderer): (switch_players, bead_new_location) = game.move(hole_moved_indice) player_hand = game.player_hands[game.current_player-1] if switch_players: game.switch_players() moving_bead = None destination = None dest_hole = None while player_hand.beads: moving_bead = player_hand.beads.pop() player_hand.bead_count -= 1 index = bead_new_location.pop(0) dest_hole = game.all_holes[index] destination = (dest_hole.rect.centerx, dest_hole.rect.centery) moving_bead.pos = [player_hand.rect.centerx, player_hand.rect.centery] gameRenderer.move_bead_to(game, moving_bead, destination) dest_hole.beads.append(moving_bead) dest_hole.bead_count += 1 game.game_over = True for (i, hole) in enumerate(game.all_holes): if hole.standard: if hole.player == game.current_player and hole.bead_count > 0: game.game_over = False else: if hole.player == 1: player_1_score = hole.bead_count else: player_2_score = hole.bead_count if game.game_over: if player_1_score > player_2_score: game.winner = 1 elif player_2_score > player_1_score: game.winner = 2 else: game.winner = 0 gameRenderer.render(game, None, True) def run_tutorial(self, game, gameRenderer): gameRenderer.tutorial_render(game) title_font = pygame.font.Font(None, 24) title_text = "How to Play Kalah - (click to continue, escape to return to your game)" gameRenderer.display_text(title_text, title_font) gameRenderer.display_tutorial_text("Each player has six pits and one store") pygame.display.flip() tutorial_screens = gameRenderer.display_tutorial(game, self) while True: try: for event in pygame.event.get(): if event.type == KEYDOWN: if event.key == pygame.K_ESCAPE: return elif event.type == QUIT: exit(0) elif event.type == MOUSEBUTTONDOWN: gameRenderer.tutorial_render(game) gameRenderer.display_text(title_text, title_font) tutorial_screens.next() pygame.display.flip() #catching the end of the generative function display_tutorial except StopIteration: return def run(self, gameRenderer, game): #used to indicate when a MOUSEBUTTONUP event should not lead to displaying the menu move_just_made = False while True: gameRenderer.render(game, self.menu, True) for event in self.menu.handle_events(pygame.event.get()): if event.type == MOUSEBUTTONDOWN: move_just_made = False for i in xrange(0,14): if game.all_holes[i].is_clicked(game.current_player): self.make_move(i, game, gameRenderer) break pos = pygame.mouse.get_pos() x = pos[0] y = pos[1] #We do not want the menu to appear when a click occurs inside the board if (x >= 100 and x <= 700) and (y >= 100 and y <= 300): move_just_made = True elif event.type == QUIT: exit(0) elif event.type == MOUSEBUTTONUP and not move_just_made: self.menu.show() elif event.type == USEREVENT: if event.code == 'MENU': self.handle_menu(event, game, gameRenderer)
'Item 1', ( 'Things', 'Item 0', 'Item 1', 'Item 2', ( 'More Things', 'Item 0', 'Item 1', ), ), 'Quit', ) menu = NonBlockingPopupMenu(menu_data) def handle_menu(e): global menu print 'Menu event: %s.%d: %s' % (e.name,e.item_id,e.text) if e.name is None: print 'Hide menu' menu.hide() elif e.name == 'Main': if e.text == 'Quit': quit() elif e.name == 'Things': pass elif e.name == 'More Things': pass