def update(): key = graphics.screen.getch() if key > 0: if key == keys['DOWN']: if game.direction[1] == -1: return game.direction = (0, 1) elif key == keys['LEFT']: if game.direction[0] == 1: return game.direction = (-1, 0) elif key == keys['RIGHT']: if game.direction[0] == -1: return game.direction = (1, 0) elif key == keys['UP']: if game.direction[1] == 1: return game.direction = (0, -1) elif key == keys['Q']: __main__.exit() exit() elif gameloop.state == 1 and key == keys['ENTER']: gameloop.init()
def load_theme(): global colors_map try: with open(theme_file): # Parse XML tree = ET.parse(theme_file) root = tree.getroot() # Colors theme['colors']['bg'] = (get_curses_color(root[0][0].attrib.get('font')), get_curses_color(root[0][0].attrib.get('background'))) theme['colors']['snake'] = (get_curses_color(root[0][1].attrib.get('font')), get_curses_color(root[0][1].attrib.get('background'))) theme['colors']['apple'] = (get_curses_color(root[0][2].attrib.get('font')), get_curses_color(root[0][2].attrib.get('background'))) theme['colors']['border'] = (get_curses_color(root[0][3].attrib.get('font')), get_curses_color(root[0][3].attrib.get('background'))) theme['colors']['lives'] = (get_curses_color(root[0][4].attrib.get('font')), get_curses_color(root[0][4].attrib.get('background'))) # Tiles theme['tiles']['bg'] = root[1][0].text theme['tiles']['snake-body'] = root[1][1].text theme['tiles']['apple'] = root[1][2].text theme['tiles']['border-h'] = root[1][3].text theme['tiles']['border-v'] = root[1][4].text theme['tiles']['border-c'] = root[1][5].text theme['tiles']['lives'] = root[1][6].text colors_map = get_colors_map() except IOError: __main__.exit()
def init(): global size, width, height, padding, boundaries, passSize # Get containing terminal window and set it to maximised pid = os.getpid() win = _get_window_by_child_pid(pid) gdk_window_settings(win, maximized=True) time.sleep(0.1) available_size = (width, height) = console.getTerminalSize() try: # Check for screen resolution h = gdk.screen_height() # Select a set of sizes depending on the screen resolution if h > 1024: chosen_size = config.game_sizes_big[parser.args.board] elif h <= 1024 and h > 720: chosen_size = config.game_sizes_medium[parser.args.board] else: chosen_size = config.game_sizes_small[parser.args.board] passSize = parser.args.board except Exception: from kano.logging import logger logger.error("Can't find board size: {}".format(parser.args.board)) __main__.exit() # Calculate width if chosen_size[0] > available_size[0] / 2: width = (available_size[0] / 2) - 3 else: width = chosen_size[0] # Calculate height if chosen_size[1] > available_size[1]: height = available_size[1] else: height = chosen_size[1] size = (width, height) padding_x = int(math.floor(available_size[0] - width) / 4) padding_y = int(math.floor(available_size[1] - height) / 2) padding = (padding_y, padding_x, padding_y, padding_x) boundaries = { "bottom": int(math.floor(height / 2)), "left": int(math.floor(-width / 2)), "right": int(math.floor(width / 2)), "top": int(math.floor(-height / 2)), }
def navigate_back(): global menu_stack, currentIdx, currentMenu, theme_name if (len(menu_stack) == 1): __main__.exit() menu_stack.pop() cur = menu_stack[len(menu_stack) - 1] currentMenu = cur[0] currentIdx = 0 # Reset Screenshot title just in case if currentMenu == menus.editMain: menus.editMain[3] = [_("Take Screenshot"), "screenshot"] # Redraw with first theme on the list if currentMenu == menus.naming: theme_name = currentMenu[0][0] redraw_board()
def update(): global set key = graphics.screen.getch() if key > 0 and not set: if key == keys['DOWN']: if game.direction[1] == -1: return set = True game.direction = (0, 1) elif key == keys['LEFT']: if game.direction[0] == 1: return set = True game.direction = (-1, 0) elif key == keys['RIGHT']: if game.direction[0] == -1: return set = True game.direction = (1, 0) elif key == keys['UP']: if game.direction[1] == 1: return set = True game.direction = (0, -1) if key > 0: if key == keys['q'] or key == keys['Q']: __main__.exit() elif gameloop.state == 2 and key == keys['ENTER']: if parser.args.tutorial: __main__.exit() else: gameloop.init() elif gameloop.state == 0 and key == keys['ENTER']: gameloop.init()
def load_theme(): global colors_map try: with open(theme_file): # Parse XML tree = ET.parse(theme_file) root = tree.getroot() # Colors theme['colors']['bg'] = (get_curses_color( root[0][0].attrib.get('font')), get_curses_color( root[0][0].attrib.get('background'))) theme['colors']['snake'] = ( get_curses_color(root[0][1].attrib.get('font')), get_curses_color(root[0][1].attrib.get('background'))) theme['colors']['apple'] = ( get_curses_color(root[0][2].attrib.get('font')), get_curses_color(root[0][2].attrib.get('background'))) theme['colors']['border'] = ( get_curses_color(root[0][3].attrib.get('font')), get_curses_color(root[0][3].attrib.get('background'))) theme['colors']['lives'] = ( get_curses_color(root[0][4].attrib.get('font')), get_curses_color(root[0][4].attrib.get('background'))) # Tiles theme['tiles']['bg'] = root[1][0].text theme['tiles']['snake-body'] = root[1][1].text theme['tiles']['apple'] = root[1][2].text theme['tiles']['border-h'] = root[1][3].text theme['tiles']['border-v'] = root[1][4].text theme['tiles']['border-c'] = root[1][5].text theme['tiles']['lives'] = root[1][6].text colors_map = get_colors_map() except IOError: __main__.exit()
def update(): global tile, currentIdx, currentMenu, currentCategory, prevIndex, symbolMode, nameMode, theme_name, menu_stack key = graphics.screen.getch() if key > 0: ''' Symbol Mode ''' if symbolMode: input_string(key) if tile == '': tile = ' ' category = currentMenu[currentIdx][0] theme.set_tiles_theme(category, tile[:2]) tile = '' redraw_board() symbolMode = False return ''' Enter Name for new theme ''' if nameMode: input_string(key) theme_name = tile tile = '' # Sanitise name theme_name = theme_name.replace(" ", "-") # Replaces spaces theme_name = re.sub(r'[^a-zA-Z0-9-]', r'', theme_name) # Remove special characters theme_name = theme_name[:20] # Trim to 20 characters # Crate new theme and refresh redraw_board() menus.update_naming() # Go back to main menu del menu_stack[:] menu_stack = [[menus.main, '']] menu_stack.append([menus.editMain, theme_name]) currentMenu = menus.editMain currentIdx = 0 nameMode = False return ''' Check KEYS ''' if key == keys['DOWN']: currentIdx = (currentIdx + 1) % len(currentMenu) # Preview colors if currentMenu == menus.colors: set_color() elif currentMenu == menus.naming: # pass the saved name from the theme theme_name = currentMenu[currentIdx][0] redraw_board() return elif key == keys['UP']: currentIdx = (currentIdx - 1) % len(currentMenu) # Preview colors if currentMenu == menus.colors: set_color() elif currentMenu == menus.naming: # Pass the saved name from the theme theme_name = currentMenu[currentIdx][0] redraw_board() return elif key == keys['LEFT']: navigate_back() return elif key == keys['ENTER'] or key == keys['RIGHT']: # Back if (currentIdx == len(currentMenu) - 1): navigate_back() return # Color option elif currentMenu[currentIdx][1] is None: set_color() navigate_back() return # Tile option elif currentMenu[currentIdx][1] == "symbols": symbolMode = True return # Custom Name Mode elif currentMenu[currentIdx][1] == "name": nameMode = True return # Delete elif currentMenu[currentIdx][1] == "delete": delete_theme() return # Screenshot elif currentMenu[currentIdx][1] == "screenshot": take_screenshot() menus.editMain[3] = ["Take Screenshot [TAKEN]", "screenshot"] return # Modify existing theme elif currentMenu[currentIdx][1] == "existing": # pass the saved name from the theme theme_name = currentMenu[currentIdx][0] theme.update_name() redraw_board() graphics.update() title = currentMenu[currentIdx][0] menu_stack.append([menus.editMain, title]) currentMenu = menus.editMain currentIdx = 0 return # Submenu else: # Prevent Delete of custom-theme if currentMenu == menus.editMain and currentIdx == 2 and \ theme_name == os.path.splitext(theme.CUSTOM_THEME)[0]: return prevIndex = currentIdx title = currentMenu[currentIdx][0] menu_stack.append([currentMenu[currentIdx][1], title]) if (currentMenu == menus.board or currentMenu == menus.elements): currentCategory = title currentMenu = currentMenu[currentIdx][1] currentIdx = 0 # Preview colors if currentMenu == menus.colors: set_color() # Redraw with first theme on the list if currentMenu == menus.naming: theme_name = currentMenu[0][0] redraw_board() elif key == keys['Q']: __main__.exit() exit()
def update(): global tile, currentIdx, currentMenu, currentCategory, prevIndex, symbolMode, nameMode, theme_name, menu_stack key = graphics.screen.getch() if key > 0: ''' Symbol Mode ''' if symbolMode: input_string(key) if tile == '': tile = ' ' category = currentMenu[currentIdx][0] theme.set_tiles_theme(category, tile[:2]) tile = '' redraw_board() symbolMode = False return ''' Enter Name for new theme ''' if nameMode: input_string(key) theme_name = tile tile = '' # Sanitise name theme_name = theme_name.replace(" ", "-") # Replaces spaces theme_name = re.sub(r'[^a-zA-Z0-9-]', r'', theme_name) # Remove special characters theme_name = theme_name[:20] # Trim to 20 characters # Crate new theme and refresh redraw_board() menus.update_naming() # Go back to main menu del menu_stack[:] menu_stack = [[menus.main, '']] menu_stack.append([menus.editMain, theme_name]) currentMenu = menus.editMain currentIdx = 0 nameMode = False return ''' Check KEYS ''' if key == keys['DOWN']: currentIdx = (currentIdx + 1) % len(currentMenu) # Preview colors if currentMenu == menus.colors: set_color() elif currentMenu == menus.naming: # pass the saved name from the theme theme_name = currentMenu[currentIdx][0] redraw_board() return elif key == keys['UP']: currentIdx = (currentIdx - 1) % len(currentMenu) # Preview colors if currentMenu == menus.colors: set_color() elif currentMenu == menus.naming: # Pass the saved name from the theme theme_name = currentMenu[currentIdx][0] redraw_board() return elif key == keys['LEFT']: navigate_back() return elif key == keys['ENTER'] or key == keys['RIGHT']: # Back if (currentIdx == len(currentMenu) - 1): navigate_back() return # Color option elif currentMenu[currentIdx][1] is None: set_color() navigate_back() return # Tile option elif currentMenu[currentIdx][1] == "symbols": symbolMode = True return # Custom Name Mode elif currentMenu[currentIdx][1] == "name": nameMode = True return # Delete elif currentMenu[currentIdx][1] == "delete": delete_theme() return # Screenshot elif currentMenu[currentIdx][1] == "screenshot": take_screenshot() menus.editMain[3] = [ _("Take Screenshot [TAKEN]"), "screenshot" ] return # Modify existing theme elif currentMenu[currentIdx][1] == "existing": # pass the saved name from the theme theme_name = currentMenu[currentIdx][0] theme.update_name() redraw_board() graphics.update() title = currentMenu[currentIdx][0] menu_stack.append([menus.editMain, title]) currentMenu = menus.editMain currentIdx = 0 return # Submenu else: # Prevent Delete of custom-theme if currentMenu == menus.editMain and currentIdx == 2 and \ theme_name == os.path.splitext(theme.CUSTOM_THEME)[0]: return prevIndex = currentIdx title = currentMenu[currentIdx][0] menu_stack.append([currentMenu[currentIdx][1], title]) if (currentMenu == menus.board or currentMenu == menus.elements): currentCategory = title currentMenu = currentMenu[currentIdx][1] currentIdx = 0 # Preview colors if currentMenu == menus.colors: set_color() # Redraw with first theme on the list if currentMenu == menus.naming: theme_name = currentMenu[0][0] redraw_board() elif key == keys['Q']: __main__.exit()