Site Sponsors:
Python - Color Console with Cursor Support 
For those who would like to create an enhanced menu system ... or perhaps your first console game ... here is a re-write for what I posted this morning:

class Cursor:

@staticmethod
def goto(xpos, ypos):
if xpos < 1:
xpos = 1
if ypos < 1:
ypos = 1
print(u'\u001b[{};{}H'.format(ypos, xpos), end='')

@staticmethod
def on():
print(u'\u001b[?25h', end='')

@staticmethod
def off():
print(u'\u001b[?25l', end='')


class Console:
color = {
'black': 30,
'red': 31,
'green': 32,
'yellow': 33,
'blue': 34,
'magenta': 35,
'cyan': 36,
'white': 37,
# 'extended': 38,
'default': 39
}

scheme = {
"fore":0, # FOREGROUND
"back":10, # BACKGROUND
"bfore":60, # BRIGHT FORE
"bback": 70 # BRIGHT BACK
}

@staticmethod
def _esc(zint, zmode):
print(u"\u001b[{}{}".format(zint, zmode), end='')

@staticmethod
def cls():
Console._esc('1', 'J')
for line in range(100):
print()
Cursor.goto(1,1)

@staticmethod
def get_colors():
return sorted(Console.color.keys())

@staticmethod
def get_schemes():
return sorted(Console.scheme.keys())

@staticmethod
def set_color(color, scheme='fore'):
if scheme in Console.scheme:
if color in Console.color:
Console._esc(Console.color[color] + Console.scheme[scheme], 'm')
return
Console._esc(Console.color['default'], 'm')

@staticmethod
def set_colors(fore, back, cfore='fore', cback='back'):
if fore in Console.color and back in Console.color:
if cfore in Console.scheme and cback in Console.scheme:
Console._esc(Console.color[fore] + Console.scheme[cfore], 'm')
Console._esc(Console.color[back] + Console.scheme[cback], 'm')

@staticmethod
def has_color():
import os
if os.name is 'posix':
return True
if os.name is 'nt':
import platform
ver = platform.version()
if ver is None or len(ver) is 0:
return False
ver = ver.split('.')
if ver[0] == '10':
return True
return False

if __name__ == "__main__":
if Console.has_color() is False:
print("Sorry, at the moment only POSIX has default VT100 / ANSI support.")
print("Some version of Windows use ANSY.SYS / VT100.sys. 'Google it.")
exit(-1)

print("1.) Cursor Test")
Console.set_colors('yellow', 'blue')
Console.cls()
y = 0
for x in range(1, 20, 2):
y += 2
Cursor.goto(x, y)
print("(", x, y, ")", end='')

print()
input("Press enter for next test ...")

Console.set_colors('blue', 'white')
Console.cls()
print("2.) Color Test")
input("Press enter for test ...")
print(Console.set_color("color"), "unknown?")
Console.cls()

Cursor.goto(1,10)
for color in Console.get_colors():
Console.set_color(color); print(color, end="...")
Console.set_color(color, 'bfore'); print("bright fore", color, end="...")
Console.set_color('default')
Console.set_color(color, 'bback'); print("bright back", color, end="...")
Console.set_color('default', 'back')
print()

print()

Cursor.goto(5,5)
Console.set_color("yellow")
Console.set_color("green", 'bback')
print("(5,5)", end='')
print()
print("*** TESTING SUCCESS ***")


Unfortunately, the price of adding cursor management pretty much prohibits test case review (above) under any (?) modern IDE.



Be prepared to use bash on Windows (cygwin) or on POSIX.

--Sharing is caring!


-Randall


p.s. If you are looking for a reasonable, POSIX way to get mouse movements, function keys, and other device events, then check out the low-level information at kernel.org. --Most can be made to work on WinDOS under Cygwin, as well!


Google Keywords: Free Source Code Public Domain Roguelike Console Terminal TTY DOS Prompt

[ add comment ] ( 28 views )   |  permalink  |  related link

<Back | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | Next> Last>>