laneya

multiplayer roguelike game
git clone https://git.ce9e.org/laneya.git

commit
77fb7a9152ef16242fc7f568fa1f25889ddd06c8
parent
a142837ba608d7a299f3b0d52e663903d32646d9
Author
Tobias Bengfort <tobias.bengfort@gmx.net>
Date
2014-10-06 22:16
implement colors in curses

Diffstat

M laneya/dirtywords/curses_dirtywords.py 41 +++++++++++++++++++++++++++++++++++++++++

1 files changed, 41 insertions, 0 deletions


diff --git a/laneya/dirtywords/curses_dirtywords.py b/laneya/dirtywords/curses_dirtywords.py

@@ -4,6 +4,7 @@ except ImportError:
    4     4     import curses
    5     5 
    6     6 import base
   -1     7 from attr_string import AttrString
    7     8 
    8     9 
    9    10 class Screen(base.Screen):
@@ -12,20 +13,60 @@ class Screen(base.Screen):
   12    13         self.width = width
   13    14 
   14    15         curses.initscr()
   -1    16         curses.start_color()
   15    17         curses.noecho()
   16    18         curses.curs_set(0)
   -1    19 
   -1    20         # use black background everywhere
   -1    21         for i in range(1, 8):
   -1    22             curses.init_pair(i, i, 0)
   -1    23 
   17    24         self.curses_window = curses.newwin(height, width, 0, 0)
   18    25 
   19    26     def getch(self):
   20    27         return self.curses_window.getch()
   21    28 
   -1    29     def _get_color(self, color):
   -1    30         r, g, b = color
   -1    31         r = int(round(r / 255.0)) * 255
   -1    32         g = int(round(g / 255.0)) * 255
   -1    33         b = int(round(b / 255.0)) * 255
   -1    34 
   -1    35         if (r, g, b) == (0, 0, 0):
   -1    36             return 0
   -1    37         elif (r, g, b) == (255, 0, 0):
   -1    38             return 1
   -1    39         elif (r, g, b) == (0, 255, 0):
   -1    40             return 2
   -1    41         elif (r, g, b) == (255, 255, 0):
   -1    42             return 3
   -1    43         elif (r, g, b) == (0, 0, 255):
   -1    44             return 4
   -1    45         elif (r, g, b) == (255, 0, 255):
   -1    46             return 5
   -1    47         elif (r, g, b) == (0, 255, 255):
   -1    48             return 6
   -1    49         elif (r, g, b) == (255, 255, 255):
   -1    50             return 7
   -1    51 
   22    52     def putstr(self, y, x, s):
   23    53         for i, ch in enumerate(s):
   -1    54             ch = AttrString(ch)
   -1    55             if ch.bold:
   -1    56                 self.curses_window.attron(curses.A_BOLD)
   -1    57             if ch.underline:
   -1    58                 self.curses_window.attron(curses.A_UNDERLINE)
   -1    59             color = self._get_color(ch.fg_color)
   -1    60             self.curses_window.attron(curses.color_pair(color))
   -1    61 
   24    62             try:
   25    63                 self.curses_window.addstr(y, x + i, ch)
   26    64             except:
   27    65                 pass
   28    66 
   -1    67             self.curses_window.attroff(curses.A_BOLD)
   -1    68             self.curses_window.attroff(curses.A_UNDERLINE)
   -1    69 
   29    70     def refresh(self):
   30    71         self.curses_window.refresh()
   31    72