class NoArmsDarts(object): def __init__(self, screen, log_level="Warn"): if not os.path.exists("log"): os.mkdir("log") self._logger = Logger(log_level, os.path.join("log", "NoArmsDarts.log")) self._logger.start() self._logger.log("Info", "Started Logging.") # self.screen_size = (640, 480) # self.screen_depth = screen.get_bitsize() self._screen = screen # self.screen = pygame.Surface(self.screen_size, 0, self.screen_depth) self._state = ST_INTRO self._game = None # menus self._menu = None self._menu_bg_image = None self._menu_font = None # backgrounds # self._connected_bg = pygame.image.load(os.path.join('namm', 'images', 'naDarts', 'images', # 'hireswallpaperboardgreenpredator.png')).convert() self._connected_bg = pygame.image.load(os.path.join('namm', 'images', 'naDarts', 'images', 'GreenBackground.png')).convert() self._disconnected_bg = pygame.image.load(os.path.join('namm', 'images', 'naDarts', 'images', 'OrangeBackground.png')).convert() self._logger.log("Debug", "Instantiating Dartboarduino...") self._dartboard = dartboarduino.Dartboarduino(log_level, autoconnect=False) self._logger.log("Info", "Pygame display set.") def run(self): try: self._logger.log("Debug", "Entering run()") self._logger.log("Debug", "State: " + self._state) self._menu_bg_image = pygame.transform.scale(self._disconnected_bg, (self._screen.get_rect().width, self._screen.get_rect().height)) self._menu_font = os.path.join('namm', 'fonts', 'FAKERECE.TTF') # self.menuFont = os.path.join('namm', 'fonts','256BYTES.ttf') self._build_menu() while self._state != ST_QUIT: if self._state == ST_INTRO: self._show_intro() self._state = ST_CONNECTING elif self._state == ST_CONNECTING: self._menu_bg_image = pygame.transform.scale(self._disconnected_bg, (self._screen.get_rect().width, self._screen.get_rect().height) ) self._menu.background(self._menu_bg_image) self._menu.page_id = ST_CONNECTING self._menu.get_action(menu.PAGE_ACTION_NO_ACTION) if self._dartboard.reconnect(): self._dartboard.connect() if self._dartboard.state == "Stopped": self._menu_bg_image = pygame.transform.scale(self._connected_bg, (self._screen.get_rect().width, self._screen.get_rect().height)) self._menu.background(self._menu_bg_image) self._menu.page_id = ST_CONNECTED self._menu.get_action(menu.PAGE_ACTION_NO_ACTION) time.sleep(2) self._state = ST_MAIN if self._state == ST_CONNECTING: time.sleep(2) self._menu.page_id = ST_ERROR_CONNECTING if self._menu.get_action() == menu.PAGE_ACTION_MAIN: self._state = ST_MAIN elif self._state == ST_MAIN: self._menu.page_id = ST_MAIN selection = self._menu.get_action() if selection not in (ST_RIGHT, ST_LEFT): # TODO self._state = selection elif self._state == ST_GAMES: self._menu.page_id = ST_GAMES selection = self._menu.get_action() if selection in (ST_BACK, ST_LEFT, ST_QUIT): self._state = ST_MAIN elif selection != ST_RIGHT: self._menu.page_id = ST_LOADING no_action = self._menu.get_action(menu.PAGE_ACTION_NO_ACTION) self._game = get_game(selection) self._state = ST_GAME_MENU elif self._state == ST_GAME_MENU: game_menu = self._game.game_menu(self._screen, self._menu_bg_image, self._menu_font, self._logger.log_level) action = game_menu.get_action() if action == menu.PAGE_ACTION_GO: self._state = ST_PLAY_GAME elif action == menu.PAGE_ACTION_QUIT: self._state = ST_QUIT else: self._state = ST_GAMES elif self._state == ST_PLAY_GAME: self._logger.log("Debug", "Play Game!") while self._state == ST_PLAY_GAME: if self._dartboard.state == "Stopped": self._dartboard.play() if self._dartboard.state == "Playing": self._state = ST_MAIN else: self._menu_bg_image = pygame.transform.scale(self._disconnected_bg, (self._screen.get_rect().width, self._screen.get_rect().height)) self._menu.background(self._menu_bg_image) self._menu.page_id = ST_CONNECTING self._menu.get_action(menu.PAGE_ACTION_NO_ACTION) self._dartboard.disconnect() if self._dartboard.reconnect(): self._dartboard.connect() if self._dartboard.state == "Stopped": self._menu_bg_image = pygame.transform.scale(self._connected_bg, (self._screen.get_rect().width, self._screen.get_rect().height)) self._menu.background(self._menu_bg_image) self._menu.page_id = ST_CONNECTED self._menu.get_action(menu.PAGE_ACTION_NO_ACTION) self._dartboard.play() if self._dartboard.state == "Playing": self._state = ST_MAIN else: self._menu_bg_image = pygame.transform.scale(self._disconnected_bg, (self._screen.get_rect().width, self._screen.get_rect().height)) self._menu.background(self._menu_bg_image) if self._state == ST_PLAY_GAME: time.sleep(2) self._menu.page_id = ST_ERROR_CONNECTING if self._menu.get_action() == menu.PAGE_ACTION_MAIN: self._state = ST_MAIN else: self._logger.log("Error", "Unknown Menu State: " + self._state) self._state = ST_MAIN # TODO: ? self._logger.log("Debug", "Exiting run()") except Exception, e: if self._logger: self._logger.log("Error", e.message) self._logger.log("Error", e.args) else: print e finally:
class DartMenu(menu.Menu): def __init__(self, game, screen, background, font, log_level="Warn"): super(DartMenu, self).__init__(None, False) if not os.path.exists("log"): os.mkdir("log") self._logger = Logger(log_level, os.path.join("log", "DartMenu.log")) self._logger.start() self._logger.log("Info", "Started Logging.") self._game = game self._screen = screen self._background = background self._font = font self._add_pages() self._state = DARTS_GAME_MAIN self._page = self._pages[self._state] self._page.background = self._background self._page.prev_state = menu.PAGE_ACTION_MAIN def get_action(self, no_action=None): menu_action = no_action page_action = None nav_next = None nav_prev = menu.PAGE_ACTION_MAIN self._page = self._pages[DARTS_GAME_MAIN] while menu_action is None: if page_action is None: page_action = self._page.get_action() if page_action == menu.PAGE_ACTION_QUIT or page_action == menu.PAGE_ACTION_GO: self._state = menu.PAGE_ACTION_MAIN elif page_action == menu.PAGE_ACTION_BACK: if nav_prev is not None: self._state = nav_prev elif page_action == menu.PAGE_ACTION_NEXT or page_action == menu.PAGE_ACTION_DONE: if nav_next is not None: self._state = nav_next elif page_action in (DARTS_GAME_MAIN, DARTS_INSTRUCTIONS, DARTS_VARIATIONS, DARTS_SETTINGS, DARTS_PLAYERS, DARTS_SELECTIONS, menu.PAGE_ACTION_MAIN, darts.INSTR_SUMMARY, darts.INSTR_OBJECT, darts.INSTR_GAMEPLAY, darts.INSTR_SCORING, darts.INSTR_VARIATIONS): self._state = page_action if self._state == DARTS_GAME_MAIN: page_action = None nav_next = None nav_prev = menu.PAGE_ACTION_MAIN self._page = self._pages[DARTS_GAME_MAIN] elif self._state == menu.PAGE_ACTION_MAIN: menu_action = page_action elif self._state == DARTS_INSTRUCTIONS or self._state == darts.INSTR_SUMMARY: page_action = None nav_next = darts.INSTR_OBJECT nav_prev = DARTS_GAME_MAIN self._page = self._pages[darts.INSTR_SUMMARY] elif self._state == darts.INSTR_OBJECT: page_action = None nav_next = darts.INSTR_GAMEPLAY nav_prev = darts.INSTR_SUMMARY self._page = self._pages[darts.INSTR_OBJECT] elif self._state == darts.INSTR_GAMEPLAY: page_action = None nav_next = darts.INSTR_SCORING nav_prev = darts.INSTR_OBJECT self._page = self._pages[darts.INSTR_GAMEPLAY] elif self._state == darts.INSTR_SCORING: page_action = None nav_next = darts.INSTR_VARIATIONS nav_prev = darts.INSTR_GAMEPLAY self._page = self._pages[darts.INSTR_SCORING] elif self._state == darts.INSTR_VARIATIONS: page_action = None nav_next = DARTS_GAME_MAIN nav_prev = darts.INSTR_SCORING self._page = self._pages[darts.INSTR_VARIATIONS] elif self._state == DARTS_VARIATIONS: if page_action == DARTS_VARIATIONS: page_action = None nav_next = None nav_prev = DARTS_SELECTIONS self._page = self._pages[DARTS_VARIATIONS] else: self._game.variation = page_action page_action = menu.PAGE_ACTION_BACK elif self._state == DARTS_SETTINGS: if page_action == DARTS_SETTINGS: page_action = None nav_next = None nav_prev = DARTS_SELECTIONS self._page = self._pages[DARTS_SETTINGS] else: self._game.settings = page_action page_action = menu.PAGE_ACTION_BACK elif self._state == DARTS_PLAYERS: if page_action == DARTS_PLAYERS: page_action = None nav_next = None nav_prev = DARTS_SELECTIONS self._page = self._pages[DARTS_PLAYERS] else: print page_action print int(page_action) players = [] for i in range(1, int(page_action)+1): players.append(darts.Player("Player {0}".format(i))) self._game.players = players page_action = menu.PAGE_ACTION_BACK elif self._state == DARTS_SELECTIONS: page_action = None nav_next = menu.PAGE_ACTION_MAIN nav_prev = DARTS_GAME_MAIN self._page = self._current_game_selections() return menu_action def _add_pages(self): self._logger.log("Debug", "Entering _add_pages()") max_width = self._screen.get_rect().width - 20 self._pages = {} self._add_game_main_page(DARTS_GAME_MAIN) self._add_instr_summary_page(darts.INSTR_SUMMARY, max_width) self._add_instr_object_page(darts.INSTR_OBJECT, max_width) self._add_instr_gameplay_page(darts.INSTR_GAMEPLAY, max_width) self._add_instr_scoring_page(darts.INSTR_SCORING, max_width) self._add_instr_variations_page(darts.INSTR_VARIATIONS, max_width) self._add_variations_page(DARTS_VARIATIONS) self._add_players_page(DARTS_PLAYERS) self._add_settings_page(DARTS_SETTINGS) def _add_game_main_page(self, id): self._logger.log("Debug", "Entering _add_game_main_page()") menu_items = ((menu.MENU_ITEM_RENDER_BLANK, None, None), (menu.MENU_ITEM_RENDER_NORMAL, DARTS_INSTRUCTIONS, DARTS_INSTRUCTIONS), (menu.MENU_ITEM_RENDER_NORMAL, "Play", DARTS_SELECTIONS) ) # font_size = 72 int(math.ceil(self.display_screen.get_rect().height / (len(menu_items) + 3))) self._pages[id] = menu.MenuPage(id, self._screen, menu_items, self._game.name, (0, 0, 0), self._background, self._font, 72, nav_back=menu.PAGE_ACTION_BACK) def _add_instr_summary_page(self, id, max_width): self._logger.log("Debug", "Entering _add_instr_summary_page()") menu_items = [] menu_items.append((menu.MENU_ITEM_RENDER_NORMAL, 'Summary:', None)) menu_items.append((menu.MENU_ITEM_RENDER_BLANK, None, None)) for line in self._game.instructions[darts.INSTR_SUMMARY]: menu_items.append((menu.MENU_ITEM_RENDER_WRAP, line, None, max_width)) self._pages[id] = menu.MenuPage(id, self._screen, menu_items, self._game.name, (0, 0, 0), self._background, self._font, 36, nav_back=menu.PAGE_ACTION_BACK, nav_next=menu.PAGE_ACTION_NEXT) def _add_instr_object_page(self, id, max_width): self._logger.log("Debug", "Entering _add_instr_object_page()") menu_items = [] menu_items.append((menu.MENU_ITEM_RENDER_NORMAL, 'Object:', None)) menu_items.append((menu.MENU_ITEM_RENDER_BLANK, None, None)) for line in self._game.instructions[darts.INSTR_OBJECT]: menu_items.append((menu.MENU_ITEM_RENDER_WRAP, line, None, max_width)) self._pages[id] = menu.MenuPage(id, self._screen, menu_items, self._game.name, (0, 0, 0), self._background, self._font, 36, nav_back=menu.PAGE_ACTION_BACK, nav_next=menu.PAGE_ACTION_NEXT, nav_main=DARTS_GAME_MAIN) def _add_instr_gameplay_page(self, id, max_width): self._logger.log("Debug", "Entering _add_instr_gameplay_page()") menu_items = [] menu_items.append((menu.MENU_ITEM_RENDER_NORMAL, 'Game Play:', None)) menu_items.append((menu.MENU_ITEM_RENDER_BLANK, None, None)) for line in self._game.instructions[darts.INSTR_GAMEPLAY]: menu_items.append((menu.MENU_ITEM_RENDER_WRAP, line, None, max_width)) self._pages[id] = menu.MenuPage(id, self._screen, menu_items, self._game.name, (0, 0, 0), self._background, self._font, 36, nav_back=menu.PAGE_ACTION_BACK, nav_next=menu.PAGE_ACTION_NEXT, nav_main=DARTS_GAME_MAIN) def _add_instr_scoring_page(self, id, max_width): self._logger.log("Debug", "Entering _add_instr_scoring_page()") menu_items = [] menu_items.append((menu.MENU_ITEM_RENDER_NORMAL, 'Scoring:', None)) menu_items.append((menu.MENU_ITEM_RENDER_BLANK, None, None)) for line in self._game.instructions[darts.INSTR_SCORING]: menu_items.append((menu.MENU_ITEM_RENDER_WRAP, line, None, max_width)) self._pages[id] = menu.MenuPage(id, self._screen, menu_items, self._game.name, (0, 0, 0), self._background, self._font, 36, nav_back=menu.PAGE_ACTION_BACK, nav_next=menu.PAGE_ACTION_NEXT, nav_main=DARTS_GAME_MAIN) def _add_instr_variations_page(self, id, max_width): self._logger.log("Debug", "Entering _add_instr_variations_page()") menu_items = [] menu_items.append((menu.MENU_ITEM_RENDER_NORMAL, 'Variations:', None)) menu_items.append((menu.MENU_ITEM_RENDER_BLANK, None, None)) for line in self._game.instructions[darts.INSTR_VARIATIONS]: menu_items.append((menu.MENU_ITEM_RENDER_WRAP, line, None, max_width)) self._pages[id] = menu.MenuPage(id, self._screen, menu_items, self._game.name, (0, 0, 0), self._background, self._font, 36, nav_back=menu.PAGE_ACTION_BACK, nav_done=menu.PAGE_ACTION_DONE) def _current_game_selections(self): self._logger.log("Debug", "Entering _current_game_selections()") menu_items = [] menu_items.append((menu.MENU_ITEM_RENDER_BLANK, None, None)) menu_items.append((menu.MENU_ITEM_RENDER_NORMAL, self._game.name, None)) if self._game.variation is not None: menu_items.append((menu.MENU_ITEM_RENDER_NORMAL, self._game.variation, DARTS_VARIATIONS)) if self._game.num_players == 1: num_players_text = "1 Player" else: num_players_text = str(self._game.num_players) + " Players" menu_items.append((menu.MENU_ITEM_RENDER_NORMAL, num_players_text, DARTS_PLAYERS)) if self._game.settings is not None: # TODO: make games settings a dictionary and iterate here? menu_items.append((menu.MENU_ITEM_RENDER_NORMAL, self._game.settings, DARTS_SETTINGS)) menu_items.append((menu.MENU_ITEM_RENDER_NORMAL, "Go!", menu.PAGE_ACTION_GO)) return menu.MenuPage(id, self._screen, menu_items, "Game Selection Summary", (0, 0, 0), self._background, self._font, 64, nav_back=menu.PAGE_ACTION_BACK) def _add_players_page(self, id): self._logger.log("Debug", "Entering _add_players_page()") menu_items = [] menu_items.append((menu.MENU_ITEM_RENDER_BLANK, None, None)) for option in self._game.player_options: if option == 1: line = '1 Player' else: line = str(option) + ' Players' menu_items.append((menu.MENU_ITEM_RENDER_NORMAL, line, str(option))) self._pages[id] = menu.MenuPage(id, self._screen, menu_items, "Select Number of Players", (0, 0, 0), self._background, self._font, 48, nav_back=menu.PAGE_ACTION_BACK) def _add_settings_page(self, id): self._logger.log("Debug", "Entering _add_settings_page()") if self._game.setting_options is not None and len(self._game.setting_options) > 0: menu_items = [(menu.MENU_ITEM_RENDER_BLANK, None, None)] for line in self._game.setting_options: menu_items.append((menu.MENU_ITEM_RENDER_NORMAL, line, line)) self._pages[id] = menu.MenuPage(id, self._screen, menu_items, "Select Game Settings", (0, 0, 0), self._background, self._font, 64, nav_back=menu.PAGE_ACTION_BACK) def _add_variations_page(self, id): self._logger.log("Debug", "Entering _add_variations_page()") if self._game.variation_options is not None and len(self._game.variation_options) > 0: menu_items = [(menu.MENU_ITEM_RENDER_BLANK, None, None)] for line in self._game.variation_options: menu_items.append((menu.MENU_ITEM_RENDER_NORMAL, line, line)) self._pages[id] = menu.MenuPage(id, self._screen, menu_items, "Select Game Variation", (0, 0, 0), self._background, self._font, 64, nav_back=menu.PAGE_ACTION_BACK)