def _setup_colour_pairs(self): """ Initialize all 63 color pairs based on the term: bg * 8 + 7 - fg So to get a color, we just need to use that term and get the right color pair number. """ if not self.has_color: return ''' for fg in xrange(8): for bg in xrange(8): # leave out white on black if fg == curses_.COLOR_WHITE and \ bg == curses_.COLOR_BLACK: continue curses.init_pair(bg * 8 + 7 - fg, fg, bg) ''' for fg in xrange(16): for bg in xrange(16): # leave out white on black #if fg == curses_.COLOR_WHITE and \ # bg == curses_.COLOR_BLACK: # continue curses.init_pair(bg * 16 + 15 - fg, fg, bg)
def main(): try: stdscr = unicurses.initscr() unicurses.cbreak() # unicurses.noecho() unicurses.start_color() stdscr.keypad(1) # Determine if we need color if args.nocolor: unicurses.init_pair(1, unicurses.COLOR_WHITE, unicurses.COLOR_BLACK) unicurses.init_pair(2, unicurses.COLOR_WHITE, unicurses.COLOR_BLACK) else: unicurses.init_pair(1, unicurses.COLOR_BLUE, unicurses.COLOR_BLACK) unicurses.init_pair(2, unicurses.COLOR_RED, unicurses.COLOR_BLACK) unicurses.init_pair(3, unicurses.COLOR_GREEN, unicurses.COLOR_BLACK) # Game loop after this point gameLoop(stdscr) except KeyboardInterrupt: pass # TODO: Use global variables to fix this #saveGame( playerName ) finally: stdscr.erase() stdscr.refresh() stdscr.keypad(0) unicurses.echo() unicurses.nocbreak() unicurses.endwin()
def create_color_id(cls, text_color: int, background_color: int) -> int: """Creates and returns the id of a Color Combination of text color and background color.""" global _COLOR_COUNTER unicurses.init_pair(_COLOR_COUNTER, text_color, background_color) color_id = _COLOR_COUNTER _COLOR_COUNTER += 1 return color_id
def printHeaderMessage(): """Print the header message at the top of the terminal.""" unicurses.init_pair(1, unicurses.COLOR_RED, unicurses.COLOR_BLACK) unicurses.attron(unicurses.COLOR_PAIR(1)) unicurses.mvaddstr(1, 24, "Conway's game of life") unicurses.mvaddstr(4, 24, "Python implementation by: CodingBeagle") unicurses.mvaddstr(7, 24, "Press 'q' to terminate the application") unicurses.attroff(unicurses.COLOR_PAIR(1))
def init_colors(self): # Draw board uc.init_pair(11, uc.COLOR_BLUE, uc.COLOR_BLACK) uc.init_pair(12, uc.COLOR_WHITE, uc.COLOR_BLACK) uc.init_pair(13, uc.COLOR_RED, uc.COLOR_BLACK) # Status menu uc.init_pair(4, uc.COLOR_WHITE, uc.COLOR_BLUE) # close uc.init_pair(1, uc.COLOR_WHITE, uc.COLOR_RED)
def create_color_and_id(cls, text_color: int, background_color: int) -> _Tuple[int, int]: """Creates and returns the color and id of a Color Combination of text color and background color.""" global _COLOR_COUNTER unicurses.init_pair(_COLOR_COUNTER, text_color, background_color) color_id = _COLOR_COUNTER color = unicurses.color_pair(color_id) _COLOR_COUNTER += 1 return color, color_id
def make_color(foreground, background): global global_color_number color_number = global_color_number curses.init_pair(color_number, foreground, background) global_color_number += 1 return color_number
def printGameBoardFrame(): """Draw a frame in the terminal which surrounds the game of life board.""" unicurses.init_pair(2, unicurses.COLOR_GREEN, unicurses.COLOR_BLACK) unicurses.attron(unicurses.COLOR_PAIR(2)) for frameWidthCoordinate in range(GAME_BOARD_COLUMNS+2): unicurses.mvaddstr(BOARD_FRAME_Y_OFFSET - 1, (frameWidthCoordinate + BOARD_FRAME_X_OFFSET) - 1, "@") unicurses.mvaddstr(BOARD_FRAME_Y_OFFSET + GAME_BOARD_ROWS, (frameWidthCoordinate + BOARD_FRAME_X_OFFSET) - 1, "@") for frameHeightCoordinate in range(GAME_BOARD_ROWS+2): unicurses.mvaddstr((BOARD_FRAME_Y_OFFSET - 1) + frameHeightCoordinate, BOARD_FRAME_X_OFFSET - 1, "@") unicurses.mvaddstr((BOARD_FRAME_Y_OFFSET -1) + frameHeightCoordinate, BOARD_FRAME_X_OFFSET + GAME_BOARD_COLUMNS, "@") unicurses.attroff(unicurses.COLOR_PAIR(2))
def start(): global screen screen = curses.initscr() curses.noecho() curses.cbreak() curses.curs_set(0) screen.keypad(1) screen.nodelay(1) curses.nonl() screen.scrollok(False) curses.start_color() for i in range(0, 17): curses.init_pair(i+1, i, 0) #log.log("Pair : ({}, {}, 0)".format(i+1,i)) h, w = screen.getmaxyx() log.log("Screen size : {}x{}".format(h, w))
def initattrs(self): unicurses.init_pair(1, unicurses.COLOR_YELLOW, unicurses.COLOR_BLACK) unicurses.init_pair(2, unicurses.COLOR_GREEN, unicurses.COLOR_BLACK) unicurses.init_pair(3, unicurses.COLOR_CYAN, unicurses.COLOR_BLACK) unicurses.init_pair(4, unicurses.COLOR_RED, unicurses.COLOR_BLACK) self.attr = {} self.attr['ttl'] = unicurses.A_BOLD self.attr['key'] = unicurses.A_BOLD | unicurses.color_pair(1) self.attr['val'] = unicurses.A_BOLD | unicurses.color_pair(2) self.attr['slp'] = unicurses.A_BOLD | unicurses.color_pair(3) self.attr['err'] = unicurses.A_BOLD | unicurses.color_pair(4)
def draw_statusbar(self): uc.wclear(self.win_statusbar) s = '[q] quit ' if self.on_menu: s += '[\u2191\u2193] move ' s += '[\u21B5] select ' else: s += '[\u2190\u2191\u2192\u2193] move ' s += '[r] rotate ' s += '[s] suggest ' s += '[\u21B5] commit ' s += f'{self.num_ships_set}/{len(self.player.ship_list)} set' uc.waddstr(self.win_statusbar, s) uc.init_pair(4, uc.COLOR_WHITE, uc.COLOR_BLUE) uc.wbkgd(self.win_statusbar, uc.COLOR_PAIR(4)) uc.wrefresh(self.win_statusbar)
def main(): ## Curses normal init sequence stdscr = curses.initscr() curses.noecho() # no echo, but we still see the cursor curses.curs_set(False) #turns off the cursor drawing stdscr.keypad(True) # allows special keys and arrow keys try: curses.start_color() curses.init_pair(1, curses.COLOR_YELLOW, curses.COLOR_GREEN) curses.init_pair(2, curses.COLOR_RED, curses.COLOR_GREEN) dude = curses.newwin(1, 1, 10, 30) curses.waddstr(dude, "@", curses.color_pair(2) + curses.A_BOLD) dude_panel = curses.new_panel(dude) grass = curses.newwin(10, 50, 5, 5) grass.bkgd(" ", curses.color_pair(1)) grass_panel = curses.new_panel(grass) curses.top_panel(dude_panel) curses.update_panels() curses.doupdate() while True: key = curses.getch() if key == 27: break curses.update_panels() curses.doupdate() except Exception as e: stdscr.addstr(0, 0, str(e)) stdscr.getch() finally: curses.endwin() return 0
def main(): ## Curses normal init sequence stdscr = curses.initscr() try: curses.start_color() curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK) curses.init_pair(2, curses.COLOR_RED, curses.COLOR_WHITE) stdscr.addstr("Hello World!", curses.color_pair(1)) stdscr.addstr("\nHello World!", curses.color_pair(2) + curses.A_BLINK) stdscr.addstr("\nHello World!", curses.color_pair(1) + curses.A_REVERSE) stdscr.getch() except Exception as e: stdscr.addstr(0, 0, str(e)) stdscr.getch() finally: curses.endwin() return 0
def init_colors(): """ Initialize color pairs from the terminal color palette. Pair 0 is the default, pairs 1-16 are the palette colors, pairs 17-32 are palette colors with a different background. We assume that color 8 has good contrast with other colors. """ global HAS_COLORS HAS_COLORS = curses.has_colors() if HAS_COLORS: curses.start_color() curses.use_default_colors() global HAS_BACKGROUND_COLORS HAS_BACKGROUND_COLORS = True if HAS_BACKGROUND_COLORS: info('Terminal supports background colors.') else: info('Terminal does not support background colors.') global COLOR_PAIRS #COLOR_PAIRS = min(16, curses.COLORS) COLOR_PAIRS = 16 info('Terminal supports {} colors. Using {} colorpairs.'.format(16, COLOR_PAIRS)) for i in range(COLOR_PAIRS): curses.init_pair(i + 1, i, -1) try: curses.init_pair(i + 1 + COLOR_PAIRS, i, 8) curses.init_pair(i + 1 + COLOR_PAIRS + COLOR_PAIRS, i, 9) except: HAS_BACKGROUND_COLORS = False
def __init__(self): self.stdscr = unicurses.initscr() self.client = Client() self.client.connect() self.receive_data_thread = threading.Thread( target=self.display_received_data) self.receive_data_thread.daemon = True unicurses.start_color() unicurses.init_pair(1, unicurses.COLOR_GREEN, unicurses.COLOR_BLACK) self.height, self.width = self.stdscr.getmaxyx() self.displayWindow = unicurses.newwin(self.height - 6, self.width, 0, 0) self.infoWindow = unicurses.newwin(3, self.width, self.height - 6, 0) self.inputWindow = unicurses.newwin(3, self.width, self.height - 3, 0) self.inputWindow.move(1, 1) self.msg = '' self.init_display_screen() self.init_info_screen() try: self.receive_data_thread.start() except (KeyboardInterrupt, SystemExit): sys.exit()
def close(self): uc.init_pair(1, uc.COLOR_WHITE, uc.COLOR_RED) prompt_start_x = self.max_x // 2 - 19 prompt_start_y = self.max_y // 3 if prompt_start_x < 1: prompt_start_x = 1 if prompt_start_y < 1: prompt_start_y = 1 win_prompt = uc.newwin(3, 38, prompt_start_y, prompt_start_x) uc.box(win_prompt, 0, 0) uc.mvwaddstr(win_prompt, 1, 1, 'Do you want to close the game? (y|n)') uc.wbkgd(win_prompt, uc.COLOR_PAIR(1)) uc.wrefresh(win_prompt) answer = uc.wgetch(stdscr) if answer == ord('y') or answer == ord('Y'): uc.endwin() exit() else: uc.delwin(win_prompt)
def draw_board(self): uc.wclear(self.win_board) offset_x = 2 offset_y = 1 uc.box(self.win_boardarea, 0, 0) uc.wmove(self.win_boardarea, offset_y, offset_x) if self.on_menu: s = self.player.board.__str__() else: s = self.player.board.show_new_ship(self.new_ship_x, self.new_ship_y, self.menu[self.menu_hi].id, self.menu[self.menu_hi].length, self.new_ship_hor) uc.init_pair(11, uc.COLOR_BLUE, uc.COLOR_BLACK) uc.init_pair(12, uc.COLOR_WHITE, uc.COLOR_BLACK) uc.init_pair(13, uc.COLOR_RED, uc.COLOR_BLACK) for chr in s: if chr == '~': uc.wattron(self.win_board, uc.COLOR_PAIR(11)) uc.waddstr(self.win_board, chr) uc.wattroff(self.win_board, uc.COLOR_PAIR(11)) elif chr == 'O': uc.wattron(self.win_board, uc.COLOR_PAIR(12)) uc.waddstr(self.win_board, chr) uc.wattroff(self.win_board, uc.COLOR_PAIR(12)) elif chr == '#': uc.wattron(self.win_board, uc.COLOR_PAIR(13)) uc.waddstr(self.win_board, chr) uc.wattroff(self.win_board, uc.COLOR_PAIR(13)) else: uc.wattron(self.win_board, uc.COLOR_PAIR(12)) uc.waddstr(self.win_board, chr) uc.wattroff(self.win_board, uc.COLOR_PAIR(12)) uc.wbkgd(self.win_statusbar, uc.COLOR_PAIR(11)) uc.wrefresh(self.win_boardarea) uc.update_panels()
elif not self.main_menu.enabled and self.game_menu_interface.blocked: self.game_field.key_event(key) else: self.game_menu_interface.key_event(key) if __name__ == "__main__": stdscr = initscr() clear() noecho() cbreak() curs_set(0) keypad(stdscr, True) start_color() use_default_colors() nodelay(stdscr, True) init_pair(1, COLOR_BLACK, COLOR_WHITE) init_pair(2, COLOR_WHITE, COLOR_BLUE) init_pair(3, COLOR_BLACK, COLOR_BLUE) init_pair(4, COLOR_WHITE, COLOR_CYAN) init_pair(5, COLOR_YELLOW, COLOR_GREEN) init_pair(6, COLOR_GREEN, COLOR_BLACK) init_pair(7, COLOR_RED, COLOR_BLACK) init_pair(8, COLOR_BLUE, COLOR_YELLOW) init_pair(9, COLOR_RED, COLOR_YELLOW) init_pair(10, COLOR_WHITE, COLOR_RED) init_pair(11, COLOR_RED, COLOR_CYAN) WHITE_BLACK = COLOR_PAIR(1) BLUE_WHITE = COLOR_PAIR(2) BLUE_BLACK = COLOR_PAIR(3) CYAN_WHITE = COLOR_PAIR(4)
import sys import time, random import unicurses as curses screen = curses.initscr() screen.nodelay(1) screen.border() curses.noecho() curses.curs_set(0) dims = screen.getmaxyx() height,width = dims[0]-1, dims[1]-1 curses.start_color() curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK) row,col= 0,0 for i in range(256): screen.addch(row,col,i,curses.color_pair(0) ) col +=1 if col>75: row +=1 col = 1 screen.refresh() time.sleep(0.051) curses.endwin()
import sys import time, random import unicurses as curses screen = curses.initscr() screen.nodelay(1) screen.border() curses.noecho() curses.curs_set(0) dims = screen.getmaxyx() height, width = dims[0] - 1, dims[1] - 1 curses.start_color() curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK) row, col = 0, 0 for i in range(256): screen.addch(row, col, i, curses.color_pair(0)) col += 1 if col > 75: row += 1 col = 1 screen.refresh() time.sleep(0.051) curses.endwin()
x += 7 # NLINES = 10 NCOLS = 40 my_wins = [0] * 3 my_panels = [0] * 3 stdscr = uni.initscr() uni.start_color() uni.cbreak() uni.noecho() uni.keypad(stdscr, True) uni.init_pair(1, uni.COLOR_RED, uni.COLOR_BLACK) uni.init_pair(2, uni.COLOR_GREEN, uni.COLOR_BLACK) uni.init_pair(3, uni.COLOR_BLUE, uni.COLOR_BLACK) uni.init_pair(4, uni.COLOR_CYAN, uni.COLOR_BLACK) init_wins(my_wins, 1) #3) my_panels[0] = uni.new_panel(my_wins[0]) #my_panels[1] = uni.new_panel(my_wins[1]) #my_panels[2] = uni.new_panel(my_wins[2]) uni.set_panel_userptr(my_panels[0], my_panels[1]) #uni.set_panel_userptr(my_panels[1], my_panels[2]) #uni.set_panel_userptr(my_panels[2], my_panels[0]) uni.update_panels()
locale.setlocale(locale.LC_ALL, '') encoding = locale.getpreferredencoding() # get the preferred system encoding for unicode support if sys.platform == 'win32': # Windows: set codepage to 65001 for unicode support os.system('chcp 65001') # Settings broadcaster_names = { # Broadcaster names for the 'BCST' bar 'djprofessork': 'DJ Professor K', 'noisetanks': 'Noise Tanks', 'seaman': 'Seaman' } unicurses.init_pair(1, unicurses.COLOR_BLUE, unicurses.COLOR_BLACK) # default user color pair unicurses.init_pair(2, unicurses.COLOR_CYAN, unicurses.COLOR_BLACK) # registered user color pair unicurses.init_pair(3, unicurses.COLOR_YELLOW, unicurses.COLOR_BLACK) # DJPK color pair default_color = unicurses.color_pair(1) | unicurses.A_BOLD # default user color registered_color = unicurses.color_pair(2) | unicurses.A_BOLD # registered user color djpk_color = unicurses.color_pair(3) | unicurses.A_BOLD # DJPK color login_text = open('./screens/login.txt', 'r').read() # login text loaded from file chat_text = open('./screens/chat.txt', 'r').read() # chat text loaded from file # Core functions and classes def write(line, x, y, effect=0): """
self.game_field.key_event(key) else: self.game_menu_interface.key_event(key) if __name__ == "__main__": stdscr = initscr() clear() noecho() cbreak() curs_set(0) keypad(stdscr, True) start_color() use_default_colors() nodelay(stdscr, True) init_pair(1, COLOR_BLACK, COLOR_WHITE) init_pair(2, COLOR_WHITE, COLOR_BLUE) init_pair(3, COLOR_BLACK, COLOR_BLUE) init_pair(4, COLOR_WHITE, COLOR_CYAN) init_pair(5, COLOR_YELLOW, COLOR_GREEN) init_pair(6, COLOR_GREEN, COLOR_BLACK) init_pair(7, COLOR_RED, COLOR_BLACK) init_pair(8, COLOR_BLUE, COLOR_YELLOW) init_pair(9, COLOR_RED, COLOR_YELLOW) init_pair(10, COLOR_WHITE, COLOR_RED) init_pair(11, COLOR_RED, COLOR_CYAN) WHITE_BLACK = COLOR_PAIR(1) BLUE_WHITE = COLOR_PAIR(2) BLUE_BLACK = COLOR_PAIR(3) CYAN_WHITE = COLOR_PAIR(4)
def initattrs(self): unicurses.init_pair(1, unicurses.COLOR_YELLOW, unicurses.COLOR_BLACK) unicurses.init_pair(2, unicurses.COLOR_GREEN, unicurses.COLOR_BLACK) unicurses.init_pair(3, unicurses.COLOR_CYAN, unicurses.COLOR_BLACK) unicurses.init_pair(4, unicurses.COLOR_RED, unicurses.COLOR_BLACK) unicurses.init_pair(5, unicurses.COLOR_WHITE, unicurses.COLOR_CYAN) unicurses.init_pair(6, unicurses.COLOR_WHITE, unicurses.COLOR_BLUE) self.attr = {} self.attr['ttl'] = unicurses.A_BOLD self.attr['key'] = unicurses.A_BOLD | unicurses.color_pair(1) self.attr['val'] = unicurses.A_BOLD | unicurses.color_pair(2) self.attr['slp'] = unicurses.A_BOLD | unicurses.color_pair(3) self.attr['err'] = unicurses.A_BOLD | unicurses.color_pair(4) self.attr['pfg'] = unicurses.A_BOLD | unicurses.color_pair(5) self.attr['pbg'] = unicurses.A_BOLD | unicurses.color_pair(6)
# Dict of colors colors = { "black": uni.COLOR_BLACK, "blue": uni.COLOR_BLUE, "cyan": uni.COLOR_CYAN, "green": uni.COLOR_GREEN, "magenta": uni.COLOR_MAGENTA, "red": uni.COLOR_RED, "white": uni.COLOR_WHITE, "yello": uni.COLOR_YELLOW } # Color pairs uni.init_pair(0, uni.COLOR_WHITE, uni.COLOR_BLACK) uni.init_pair(1, uni.COLOR_BLACK, uni.COLOR_WHITE) uni.init_pair(2, uni.COLOR_CYAN, uni.COLOR_BLUE) uni.init_pair(3, uni.COLOR_GREEN, uni.COLOR_BLACK) ''' # Named color pairs color_schemes = { "normal": uni.COLOR_PAIR(0), "inverse": uni.COLOR_PAIR(1), "cyan": uni.COLOR_PAIR(2), "green": uni.COLOR_PAIR(3) } ''' YES_KEYS = ["y", 121, "Y", 89]