diff --git a/gameshell.py b/gameshell.py index e5e8683..9ea9ad9 100644 --- a/gameshell.py +++ b/gameshell.py @@ -23,6 +23,7 @@ class GameShell(Shell): super(GameShell, self).__init__() self.outstream = _sys.stdout self.gameBase = gameBase + self.colorMode = 24 # register functions @@ -45,6 +46,8 @@ class GameShell(Shell): self.registerCommand('bag', self.inv) self.registerCommand('items', self.inv) self.registerCommand('status', self.status) + self.registerCommand('options', self.options) + self.registerCommand('colorTest', self.colorTest) self.registerAlias('run', ['go', '-r']) # Helper functions @@ -53,6 +56,51 @@ class GameShell(Shell): super(GameShell, self).man(args) heapq.heappush(self.gameBase.eventQueue, (self.gameBase.gameTime, gameevents.NoOpEvent())) + def options(self, args): + i = 0 + while i < len(args): + if args[i] == 'color': + i += 1 + if args[i] in ('0', '3', '4', '8', '24'): + self.colorMode = int(args[i]) + i += 1 + + def __colorTestRoutine(self, lower, upper): + colors = [] + step = int((upper - lower) / 8) + #print(step) + for g in range(lower, upper, step): + colors.append(self.color(upper-1, g, lower, False)) + #print(len(colors)) + colors.append(self.color(upper-1, upper-1, lower, False)) + for r in range(upper-step, lower-1, -step): + colors.append(self.color(r, upper-1, lower, False)) + #print(len(colors)) + for b in range(lower+step, upper, step): + colors.append(self.color(lower, upper-1, b, False)) + #print(len(colors)) + colors.append(self.color(lower, upper-1, upper-1, False)) + for g in range(upper-step, lower-1, -step): + colors.append(self.color(lower, g, upper-1, False)) + #print(len(colors)) + for r in range(lower+step, upper, step): + colors.append(self.color(r, lower, upper-1, False)) + colors.append(self.color(upper-1, lower, upper-1, False)) + #print(len(colors)) + for b in range(upper-step, lower, -step): + colors.append(self.color(upper-1, lower, b, False)) + #print(len(colors)) + colors.append('\x1b[0m') + print(' '.join(colors)) + + def colorTest(self, args): + for i in (3, 4, 8, 24): + print(i) + self.colorMode = i + self.__colorTestRoutine(0, 128) # dark + self.__colorTestRoutine(0, 256) # medium + self.__colorTestRoutine(128, 256) # light + def showMap(self, args): """map [-l] See a map of the local area. "ls" is an alias of map. @@ -65,35 +113,48 @@ If -l is given, a map legend will be printed under the map.""" doors = {} useables = {} items = {} - for y in range(self.gameBase.level.dimensions[1]): - rows.append(['{0:2} '.format(y)]) - for x in range(self.gameBase.level.dimensions[0]): - pos = self.gameBase.level.mapMatrix[y][x] - thing = self.gameBase.level.getThingAtPos(index) + level = self.gameBase.level + textColor = level.wallColors[0] + floorColor = level.floorColors[0] + for y in range(level.dimensions[1]): + rows.append(['\x1b[0m{0:2} {1}{2}'.format(y, self.color(textColor[1:]), self.color(floorColor[1:], fg = False))]) + rows[-1].append(self.color(textColor[1:])) + for x in range(level.dimensions[0]): + pos = level.mapMatrix[y][x] + thing = level.getThingAtPos(index) if x == self.gameBase.playerx and y == self.gameBase.playery: + if '#0000FF' != textColor: + textColor = '#0000FF' + rows[-1].append(self.color(textColor[1:])) rows[-1].append('()') elif thing: + if thing.graphic[1] != textColor: + textColor = thing.graphic[1] + rows[-1].append(self.color(textColor[1:])) if thing.thingType == 'x': # exit rows[-1].append('X{0}'.format(thing.exitid)) - exits[thing.exitid] = thing.name + exits[thing.exitid] = (thing.name, thing.graphic[1]) elif thing.thingType == 'd': # door - doors[len(doors)+1] = thing.name + doors[len(doors)+1] = (thing.name, thing.graphic[1]) rows[-1].append('D{0}'.format(len(doors))) elif thing.thingType == 'u': # useable - useables[len(useables)+1] = thing.name + useables[len(useables)+1] = (thing.name, thing.graphic[1]) rows[-1].append('U{0}'.format(len(useables))) elif thing.thingType == 'i': # item - items[len(items)+1] = thing.name + items[len(items)+1] = (thing.name, thing.graphic[1]) rows[-1].append('I{0}'.format(len(items))) elif pos[0] == 'w': + if level.wallColors[level.mapMatrix[y][x][1]] != textColor: + textColor = level.wallColors[level.mapMatrix[y][x][1]] + rows[-1].append(self.color(textColor[1:])) sides = 0 - if y > 0 and self.gameBase.level.mapMatrix[y-1][x][0] == 'w': + if y > 0 and level.mapMatrix[y-1][x][0] == 'w': sides += GameShell.UP - if x < self.gameBase.level.dimensions[0]-1 and self.gameBase.level.mapMatrix[y][x+1][0] == 'w': + if x < level.dimensions[0]-1 and level.mapMatrix[y][x+1][0] == 'w': sides += GameShell.RIGHT - if y < self.gameBase.level.dimensions[1]-1 and self.gameBase.level.mapMatrix[y+1][x][0] == 'w': + if y < level.dimensions[1]-1 and level.mapMatrix[y+1][x][0] == 'w': sides += GameShell.DOWN - if x > 0 and self.gameBase.level.mapMatrix[y][x-1][0] == 'w': + if x > 0 and level.mapMatrix[y][x-1][0] == 'w': sides += GameShell.LEFT rows[-1].append(GameShell.WALLS[sides]) else: @@ -102,23 +163,24 @@ If -l is given, a map legend will be printed under the map.""" rows[-1] = ''.join(rows[-1]) print(xAxis) - print('\n'.join(rows)) + print('\x1b[0m\n'.join(rows) + '\x1b[0m') + self.clearColor() if len(args) > 0: if args[0] == '-l' or args[0] == 'l' or args[0] == 'legend': - legend = ["\n---Legend---\n", "() - {0}".format(self.gameBase.playerName), + legend = ["\n---Legend---\n", "{0}()\x1b[0m - {1}".format(self.color('0000FF'), self.gameBase.playerName), "Xn - Exit to another area"] for i in exits: - legend.append(' X{0} - {1}'.format(i, exits[i])) + legend.append(' {0}X{1}\x1b[0m - {2}'.format(self.color(exits[i][1][1:]), i, exits[i][0])) legend.append("Un - Useable object") for i in useables: - legend.append(' U{0} - {1}'.format(i, useables[i])) + legend.append(' {0}U{1}\x1b[0m - {2}'.format(self.color(useables[i][1][1:]), i, useables[i][0])) legend.append("In - Item") for i in items: - legend.append(' I{0} - {1}'.format(i, items[i])) + legend.append(' {0}I{1}\x1b[0m - {2}'.format(self.color(items[i][1][1:]), i, items[i][0])) legend.append("Dn - Door") for i in doors: - legend.append(' D{0} - {1}'.format(i, doors[i])) + legend.append(' {0}D{1}\x1b[0m - {2}'.format(self.color(doors[i][1][1:]), i, doors[i][0])) print('\n'.join(legend)) heapq.heappush(self.gameBase.eventQueue, (self.gameBase.gameTime, gameevents.NoOpEvent())) return diff --git a/shell.py b/shell.py index c09d550..586df56 100644 --- a/shell.py +++ b/shell.py @@ -20,11 +20,14 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. # -# Ver. 0.1.0017 +# Ver. 0.1.0026 import types as _types import traceback as _tb +import math as _math + +_CONV24TO8 = 6 / 256 class Shell(object): @@ -33,8 +36,86 @@ class Shell(object): self.__aliases = {} self.ps1 = '> ' self.ps2 = '> ' + self.colorMode = 0 # bits of color depth. Supports: 0, 3, 4, 8, 24 self.__exit = False + def __color24(self, r, g, b, fg = True): + if fg: + return '\x1b[38;2;{0};{1};{2}m'.format(r, g, b) + else: + return '\x1b[48;2;{0};{1};{2}m'.format(r, g, b) + + def __color8(self, r, g, b, fg = True): + r = _math.floor(r * _CONV24TO8) + g = _math.floor(g * _CONV24TO8) + b = _math.floor(b * _CONV24TO8) + ret = 16 + b + 6 * g + 36 * r + if fg: + return '\x1b[38;5;{0}m'.format(ret) + else: + return '\x1b[48;5;{0}m'.format(ret) + + def __color4(self, r, g, b, fg = True): + color = _math.floor(r / 128) + 2 * _math.floor(g / 128) + 4 * _math.floor(b / 128) + r2, g2, b2 = r % 128, g % 128, b % 128 + if r2 + g2 + b2 >= 192: + color += 60 + if fg: + color += 30 + else: + color += 40 + return '\x1b[{0}m'.format(color) + + def __color3(self, r, g, b, fg = True): + color = _math.floor(r / 128) + 2 * _math.floor(g / 128) + 4 * _math.floor(b / 128) + if fg: + color += 30 + else: + color += 40 + return '\x1b[{0}m'.format(color) + + def colorFromHex(self, color): + """expects string formmatted like 3377DD""" + return int(color[0:2], 16), int(color[2:4], 16), int(color[4:6], 16) + + def color(self, r, g = 0, b = 0, fg = True): + if isinstance(r, str): + r, g, b = self.colorFromHex(r) + if self.colorMode == 0: # no color + return '' + elif self.colorMode == 3: + return self.__color3(r, g, b, fg) + elif self.colorMode == 4: + return self.__color4(r, g, b, fg) + elif self.colorMode == 8: + return self.__color8(r, g, b, fg) + elif self.colorMode == 24: + return self.__color24(r, g, b, fg) + else: + return '' + + def setColor(self, r, g = 0, b = 0, fg = True): + """Set the text color.""" + if isinstance(r, str): + r, g, b = self.colorFromHex(r) + if self.colorMode == 0: # no color + return + elif self.colorMode == 3: + print(self.__color3(r, g, b, fg), end = '') + elif self.colorMode == 4: + print(self.__color4(r, g, b, fg), end = '') + elif self.colorMode == 8: + print(self.__color8(r, g, b, fg), end = '') + elif self.colorMode == 24: + print(self.__color24(r, g, b, fg), end = '') + else: + return + return + + def clearColor(self): + print('\x1b[0m', end = '') + return + def run(self): """The main game/shell loop""" while not self.__exit: diff --git a/testing/test1.txt b/testing/test1.txt deleted file mode 100644 index 09ee937..0000000 --- a/testing/test1.txt +++ /dev/null @@ -1,33 +0,0 @@ -w0w0w0w0w0w0w0w0w0w0w0w0w0w0w0w0w0w0w0w0w0w0w0w0w0w0w0w0w0w0 -w0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e0w0 -w0w0w0w0e0e0e0e0e0e0e0e0e0w0e0w0e0w0e0w0e0w0e0e0e0e0e0e0w0w0 -w0w0w0w0e0e0e0e0e0w0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e0e0w0w0 -w0w0e0e0e0e0w0w0e0w0e0e0e0e0e0e0e0e0e0e0e0e0w0w0w0w0e0e0w0w0 -w0w0w0e0e0e0w0w0e0w0e0e0e0e0e0e0e0e0e0e0e0e0w0e0e0e0e0e0w0w0 -w0e0e0e0w0e0e0e0e0w0e0e0e0e0e0e0e0e0e0e0e0e0w0e0e0e0e0e0w0w0 -w0e0e0e0w0e0e0e0e0w0e0e0e0w0w0w0e0e0w0w0w0w0w0e0e0e0e0e0e0w0 -w0e0e0e0e0e0e0e0w0w0w0w0e0w0e0e0e0e0e0e0w0w0w0e0w0w0e0e0w0w0 -w0w0w0w0w0w0w0w0w0w0e0e0e0w0e0e0e0e0e0e0w0w0w0e0w0w0e0e0w0w0 -w0e0e0w0e0x1e0e0w0e0e0e0e0w0e0e0w0e0w0e0e0w0w0e0e0e0e0w0w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