def init():
height_term, width_term = get_terminal_size()
height_min = COL_HEIGHT * HEIGHT + 2 + 9
width_min = COL_WIDTH * WIDTH + 2 + 5
if height_term < height_min or width_term < width_min:
# resize the terminal to fit the minimum size to display the connect4 before exit
stdout.write("\x1b[8;{h};{w}t".format(h=max(height_min, height_term), w=max(width_min, width_term)))
exit('\033[91m' + 'The terminal was too small, you can now restart ' + '\033[1m' + 'Connect4' + '\033[0m')
stdscr = curses.initscr()
height,width = stdscr.getmaxyx()
if height < height_min or width < width_min:
# abort the program if the terminal can't be resized
curses.endwin()
exit('Please resize your terminal [%d%s%d] (minimum required %d%s%d)' %(width, 'x', height, width_min, 'x', height_min))
curses.noecho()
curses.cbreak()
curses.curs_set(0)
stdscr.keypad(1)
#define the different colors
if curses.can_change_color():
defineColors()
#return stdscr, width
stdscr.clear()
stdscr.border(0)
return stdscr, width, height
python类can_change_color()的实例源码
def addString(y, x, string, string_color=color.BLACK, bold=False):
if x == position.CENTER:
x = width/2 - len(string)/2
options = 0
if curses.can_change_color():
# tokens special cases color
if string == 'X':
options = curses.color_pair(color.RED) if not bold else curses.color_pair(color.RED_H) | curses.A_BOLD
elif string == 'O':
options = curses.color_pair(color.YELLOW) if not bold else curses.color_pair(color.YELLOW_H) | curses.A_BOLD
else:
options = curses.color_pair(string_color)
if bold:
options |= curses.A_BOLD
stdscr.addstr(y, x, string, options)
stdscr.refresh()
# main display
def gray(self, scale):
if curses.can_change_color():
return curses.color_pair(self.GRAY_BASE + int(round(scale * (self.GRAYS - 1))))
else:
return curses.color_pair(self.WHITE)
def init_colors(self):
if curses.has_colors() and curses.can_change_color():
curses.init_color(self.COLOR_BLACK, 0, 0, 0)
curses.init_color(self.COLOR_WHITE, 1000, 1000, 1000)
curses.init_color(self.COLOR_BLUE, 0, 0, 1000)
curses.init_color(self.COLOR_RED, 1000, 0, 0)
curses.init_color(self.COLOR_GREEN, 0, 1000, 0)
for i in xrange(0, self.GRAYS):
curses.init_color(
self.GRAY_BASE + i,
i * 1000 / (self.GRAYS - 1),
i * 1000 / (self.GRAYS - 1),
i * 1000 / (self.GRAYS - 1)
)
curses.init_pair(
self.GRAY_BASE + i,
self.GRAY_BASE + i,
self.COLOR_BLACK
)
else:
self.COLOR_BLACK = curses.COLOR_BLACK
self.COLOR_WHITE = curses.COLOR_WHITE
self.COLOR_BLUE = curses.COLOR_BLUE
self.COLOR_RED = curses.COLOR_RED
self.COLOR_GREEN = curses.COLOR_GREEN
for i in xrange(0, self.GRAYS):
curses.init_pair(
self.GRAY_BASE + i,
self.COLOR_WHITE,
self.COLOR_BLACK
)
curses.init_pair(self.BLACK, self.COLOR_BLACK, self.COLOR_BLACK)
curses.init_pair(self.WHITE, self.COLOR_WHITE, self.COLOR_BLACK)
curses.init_pair(self.BLUE, self.COLOR_BLUE, self.COLOR_BLACK)
curses.init_pair(self.RED, self.COLOR_RED, self.COLOR_BLACK)
curses.init_pair(self.GREEN, self.COLOR_GREEN, self.COLOR_BLACK)
def gray(self, scale):
if curses.can_change_color():
return curses.color_pair(self.GRAY_BASE + int(round(scale * (self.GRAYS - 1))))
else:
return curses.color_pair(self.WHITE)
def main(screen):
"""
Main entry point
:param screen:
:return:
"""
logging.info("Supports color: {}".format(curses.can_change_color()))
logging.info("Colors: {}".format(curses.COLORS))
logging.info("Color Pairs: {}".format(curses.COLOR_PAIRS))
logging.info("Loading config")
with open("ascii_qgis.config") as f:
global config
config = json.load(f)
init_colors()
screen.refresh()
global scr, pad, aboutwindow, legendwindow, mapwindow, modeline
scr = screen
pad = EditPad()
modeline = ModeLine()
mapwindow = Map()
legendwindow = Legend()
aboutwindow = AboutWindow()
legendwindow.render_legend()
mapwindow.render_map()
screen.addstr(0, 0, "ASCII")
screen.addstr(0, 5, " QGIS Enterprise", curses.color_pair(4))
screen.refresh()
if config.get('showhelp', True):
show_help()
pad.focus()
def start(screen):
curses.noecho()
curses.cbreak()
screen.keypad(True)
curses.start_color()
curses.use_default_colors()
curses.curs_set(0)
if curses.can_change_color():
curses.init_color(COLOR_DARKBLACK, 0, 0, 0)
curses.init_color(COLOR_SUPERWHITE, 1000, 1000, 1000)
curses.init_pair(PAIR_ACTIVE_TAB, COLOR_SUPERWHITE, COLOR_DARKBLACK)
curses.init_pair(PAIR_TABBAR_BG, COLOR_DARKBLACK, COLOR_SUPERWHITE)
else:
curses.init_pair(PAIR_ACTIVE_TAB,
curses.COLOR_WHITE, curses.COLOR_BLACK)
curses.init_pair(PAIR_TABBAR_BG,
curses.COLOR_BLACK, curses.COLOR_WHITE)
curses.init_pair(PAIR_INACTIVE_TAB,
curses.COLOR_WHITE, curses.COLOR_BLACK)
curses.init_pair(PAIR_ACTIVE_ACCOUNT_SEL,
curses.COLOR_BLACK, curses.COLOR_WHITE)
curses.init_pair(PAIR_INACTIVE_ACCOUNT_SEL, curses.COLOR_WHITE, -1)
curses.init_pair(PAIR_POSITIVE_VALUE, curses.COLOR_GREEN, -1)
curses.init_pair(PAIR_NEGATIVE_VALUE, curses.COLOR_RED, -1)
websockets_path = "ws://localhost:8888"
async with api.WebSocket(websockets_path) as ws:
app = Application(screen, ws)
await app.start()
def main(screen):
screen.clear()
screen.keypad(True)
curses.curs_set(False)
width = curses.COLS
height = curses.LINES
if(height < 20 or width < 50):
raise RuntimeError("This terminal is too damn small!")
if not (curses.has_colors()):
raise RuntimeError("This terminal does not support colors!")
if not (curses.can_change_color()):
raise RuntimeError("This terminal does not support changing color definitions!")
conf = configs.nice_conf
menu.confmenu(conf, screen)
screen.nodelay(True)
screen.clear()
screen.refresh()
mainwin = curses.newwin(height-7, width, 0, 0)
statuswin = curses.newwin(7, width, height-7, 0)
while(1):
world = World(mainwin, conf)
activeplayer = 0
n_turns = 1
for p in itertools.cycle(world.players):
if(p.isdead):
continue
world.wind = randint(max(-conf['wind_max'], world.wind-conf['wind_change']),
min( conf['wind_max'], world.wind+conf['wind_change']))
p.isactive = True
p.active_shots = 0
while ((p.isactive or p.active_shots > 0) and not len([p for p in world.players if not p.isdead]) <= 1 ):
gamestep(screen, mainwin, statuswin, p, world, conf, n_turns)
if (len([p for p in world.players if not p.isdead]) == 1):
gameover(screen, [p for p in world.players if not p.isdead][0])
break
if (len([p for p in world.players if not p.isdead]) == 0):
gameover(screen, None)
break
n_turns += 1
def init_colors(self):
if curses.has_colors() and curses.can_change_color():
curses.init_color(self.COLOR_BLACK, 0, 0, 0)
curses.init_color(self.COLOR_WHITE, 1000, 1000, 1000)
curses.init_color(self.COLOR_BLUE, 0, 0, 1000)
curses.init_color(self.COLOR_RED, 1000, 0, 0)
curses.init_color(self.COLOR_GREEN, 0, 1000, 0)
# this will remove flicker, but gives boring colors
'''
self.COLOR_BLACK = curses.COLOR_BLACK
self.COLOR_WHITE = curses.COLOR_WHITE
self.COLOR_BLUE = curses.COLOR_BLUE
self.COLOR_RED = curses.COLOR_RED
self.COLOR_GREEN = curses.COLOR_GREEN
'''
for i in xrange(0, self.GRAYS):
curses.init_color(
self.GRAY_BASE + i,
i * 1000 / (self.GRAYS - 1),
i * 1000 / (self.GRAYS - 1),
i * 1000 / (self.GRAYS - 1)
)
curses.init_pair(
self.GRAY_BASE + i,
self.GRAY_BASE + i,
self.COLOR_BLACK
)
else:
self.COLOR_BLACK = curses.COLOR_BLACK
self.COLOR_WHITE = curses.COLOR_WHITE
self.COLOR_BLUE = curses.COLOR_BLUE
self.COLOR_RED = curses.COLOR_RED
self.COLOR_GREEN = curses.COLOR_GREEN
for i in xrange(0, self.GRAYS):
curses.init_pair(
self.GRAY_BASE + i,
self.COLOR_WHITE,
self.COLOR_BLACK
)
curses.init_pair(self.BLACK, self.COLOR_BLACK, self.COLOR_BLACK)
curses.init_pair(self.WHITE, self.COLOR_WHITE, self.COLOR_BLACK)
curses.init_pair(self.BLUE, self.COLOR_BLUE, self.COLOR_BLACK)
curses.init_pair(self.RED, self.COLOR_RED, self.COLOR_BLACK)
curses.init_pair(self.GREEN, self.COLOR_GREEN, self.COLOR_BLACK)
def __init__(self, cursesScreen):
self.clicks = 0
self.debugMouseEvent = (0, 0, 0, 0, 0)
self.exiting = False
self.modalUi = None
self.modeStack = []
self.priorClick = 0
self.savedMouseButton1Down = False
self.savedMouseWindow = None
self.savedMouseX = -1
self.savedMouseY = -1
self.cursesScreen = cursesScreen
self.ch = 0
curses.mousemask(-1)
curses.mouseinterval(0)
# Enable mouse tracking in xterm.
sys.stdout.write('\033[?1002;h\n')
#sys.stdout.write('\033[?1005;h\n')
curses.meta(1)
# Access ^c before shell does.
curses.raw()
# Enable Bracketed Paste Mode.
sys.stdout.write('\033[?2004;h\n')
#curses.start_color()
curses.use_default_colors()
if 0:
assert(curses.COLORS == 256)
assert(curses.can_change_color() == 1)
assert(curses.has_colors() == 1)
app.log.detail("color_content:")
for i in range(0, curses.COLORS):
app.log.detail("color", i, ": ", curses.color_content(i))
for i in range(16, curses.COLORS):
curses.init_color(i, 500, 500, i * 787 % 1000)
app.log.detail("color_content, after:")
for i in range(0, curses.COLORS):
app.log.detail("color", i, ": ", curses.color_content(i))
self.setUpPalette()
if 1:
#rows, cols = self.cursesScreen.getmaxyx()
cursesWindow = self.cursesScreen
cursesWindow.leaveok(1) # Don't update cursor position.
cursesWindow.scrollok(0)
cursesWindow.timeout(10)
cursesWindow.keypad(1)
self.top, self.left = cursesWindow.getyx()
self.rows, self.cols = cursesWindow.getmaxyx()
app.window.mainCursesWindow = cursesWindow
self.zOrder = []
def debugDraw(self, win):
"""Draw real-time debug information to the screen."""
if not self.debugWindow:
return
textBuffer = win.textBuffer
y, x = win.top, win.left
maxRow, maxCol = win.rows, win.cols
self.debugWindow.writeLineRow = 0
intent = "noIntent"
try: intent = win.userIntent
except: pass
color = app.color.get('debug_window')
self.debugWindow.writeLine(
" cRow %3d cCol %2d goalCol %2d %s"
%(win.textBuffer.penRow, win.textBuffer.penCol, win.textBuffer.goalCol,
intent),
color)
self.debugWindow.writeLine(
" pRow %3d pCol %2d chRow %4d"
%(textBuffer.penRow, textBuffer.penCol,
textBuffer.debugUpperChangedRow), color)
self.debugWindow.writeLine(
" mkrRow %3d mkrCol %2d sm %d"
%(textBuffer.markerRow, textBuffer.markerCol,
textBuffer.selectionMode),
color)
self.debugWindow.writeLine(
"scrlRow %3d scrlCol %2d lines %3d"
%(win.scrollRow, win.scrollCol, len(textBuffer.lines)),
color)
self.debugWindow.writeLine(
"y %2d x %2d maxRow %d maxCol %d baud %d color %d"
%(y, x, maxRow, maxCol, curses.baudrate(), curses.can_change_color()),
color)
screenRows, screenCols = self.cursesScreen.getmaxyx()
self.debugWindow.writeLine(
"scr rows %d cols %d mlt %f/%f pt %f"
%(screenRows, screenCols, self.mainLoopTime, self.mainLoopTimePeak,
textBuffer.parserTime), color)
self.debugWindow.writeLine(
"ch %3s %s"
%(self.ch, app.curses_util.cursesKeyName(self.ch) or 'UNKNOWN'),
color)
self.debugWindow.writeLine("win %r"%(win,),
color)
self.debugWindow.writeLine("win %r"%(self.focusedWindow,),
color)
self.debugWindow.writeLine("tb %r"%(textBuffer,),
color)
(id, mouseCol, mouseRow, mouseZ, bState) = self.debugMouseEvent
self.debugWindow.writeLine(
"mouse id %d, mouseCol %d, mouseRow %d, mouseZ %d"
%(id, mouseCol, mouseRow, mouseZ), color)
self.debugWindow.writeLine(
"bState %s %d"
%(app.curses_util.mouseButtonName(bState), bState),
color)
self.debugWindow.writeLine(
"startAndEnd %r"
%(textBuffer.startAndEnd(),),
color)