laneya

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

commit
0930d2f14c35f3ead7a5b3a1aa8d61fdfbdb9906
parent
77fb7a9152ef16242fc7f568fa1f25889ddd06c8
Author
Tobias Bengfort <tobias.bengfort@gmx.net>
Date
2014-10-07 20:00
move dirtywords to separate repo

requires to update dependencies via

  `python setup.py develop`

Diffstat

D laneya/dirtywords/__init__.py 11 -----------
D laneya/dirtywords/attr_string.py 104 ------------------------------------------------------------
D laneya/dirtywords/base.py 83 ------------------------------------------------------------
D laneya/dirtywords/curses_dirtywords.py 74 ------------------------------------------------------------
D laneya/dirtywords/example.py 61 ------------------------------------------------------------
D laneya/dirtywords/pygame_dirtywords.py 46 ----------------------------------------------
D laneya/dirtywords/stupid_dirtywords.py 28 ----------------------------
M setup.py 1 +

8 files changed, 1 insertions, 407 deletions


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

@@ -1,11 +0,0 @@
    1    -1 # flake8: noqa
    2    -1 
    3    -1 try:
    4    -1     from pygame_dirtywords import Screen
    5    -1 except ImportError:
    6    -1     try:
    7    -1         from curses_dirtywords import Screen
    8    -1     except ImportError:
    9    -1         from stupid_dirtywords import Screen
   10    -1 
   11    -1 from base import Window

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

@@ -1,104 +0,0 @@
    1    -1 class AttrString(unicode):
    2    -1     def __new__(cls, s, **kwargs):
    3    -1         self = super(AttrString, cls).__new__(cls, s)
    4    -1         self.set_attrs(s, **kwargs)
    5    -1         return self
    6    -1 
    7    -1     def set_attrs(self, reference, **kwargs):
    8    -1         defaults = {
    9    -1             'bold': False,
   10    -1             'italic': False,
   11    -1             'underline': False,
   12    -1             'fg_color': (255, 255, 255),
   13    -1             'bg_color': (0, 0, 0),
   14    -1         }
   15    -1 
   16    -1         for attr in defaults.iterkeys():
   17    -1             if attr in kwargs:
   18    -1                 value = kwargs[attr]
   19    -1             elif isinstance(reference, AttrString):
   20    -1                 value = getattr(reference, attr)
   21    -1             else:
   22    -1                 value = defaults[attr]
   23    -1 
   24    -1             setattr(self, attr, value)
   25    -1 
   26    -1     def get_attrs(self):
   27    -1         return {
   28    -1             'bold': self.bold,
   29    -1             'italic': self.italic,
   30    -1             'underline': self.underline,
   31    -1             'fg_color': self.fg_color,
   32    -1             'bg_color': self.bg_color,
   33    -1         }
   34    -1 
   35    -1     def __iter__(self):
   36    -1         return (self[i] for i in range(len(self)))
   37    -1 
   38    -1     def __getitem__(self, i):
   39    -1         ch = unicode.__getitem__(self, i)
   40    -1         return AttrString(ch, **self.get_attrs())
   41    -1 
   42    -1 
   43    -1 def normal(s):
   44    -1     return unicode(s)
   45    -1 
   46    -1 
   47    -1 def bold(s):
   48    -1     return AttrString(s, bold=True)
   49    -1 
   50    -1 
   51    -1 def italic(s):
   52    -1     return AttrString(s, italic=True)
   53    -1 
   54    -1 
   55    -1 def underline(s):
   56    -1     return AttrString(s, underline=True)
   57    -1 
   58    -1 
   59    -1 def white(s):
   60    -1     return AttrString(s, fg_color=(255, 255, 255))
   61    -1 
   62    -1 
   63    -1 def black(s):
   64    -1     return AttrString(s, fg_color=(0, 0, 0))
   65    -1 
   66    -1 
   67    -1 def red(s):
   68    -1     return AttrString(s, fg_color=(255, 0, 0))
   69    -1 
   70    -1 
   71    -1 def green(s):
   72    -1     return AttrString(s, fg_color=(0, 255, 0))
   73    -1 
   74    -1 
   75    -1 def blue(s):
   76    -1     return AttrString(s, fg_color=(0, 0, 255))
   77    -1 
   78    -1 
   79    -1 def yellow(s):
   80    -1     return AttrString(s, fg_color=(255, 255, 0))
   81    -1 
   82    -1 
   83    -1 def on_white(s):
   84    -1     return AttrString(s, bg_color=(255, 255, 255))
   85    -1 
   86    -1 
   87    -1 def on_black(s):
   88    -1     return AttrString(s, bg_color=(0, 0, 0))
   89    -1 
   90    -1 
   91    -1 def on_red(s):
   92    -1     return AttrString(s, bg_color=(255, 0, 0))
   93    -1 
   94    -1 
   95    -1 def on_green(s):
   96    -1     return AttrString(s, bg_color=(0, 255, 0))
   97    -1 
   98    -1 
   99    -1 def on_blue(s):
  100    -1     return AttrString(s, bg_color=(0, 0, 255))
  101    -1 
  102    -1 
  103    -1 def on_yellow(s):
  104    -1     return AttrString(s, bg_color=(255, 255, 0))

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

@@ -1,83 +0,0 @@
    1    -1 class BaseScreen(object):
    2    -1     def __init__(self, height, width):
    3    -1         """Initialize screen."""
    4    -1         self.height = height
    5    -1         self.width = width
    6    -1         self.data = [[' ' for xx in range(width)] for yy in range(height)]
    7    -1 
    8    -1     def getch(self):
    9    -1         """Get next keystroke (blocking)."""
   10    -1         raise NotImplementedError
   11    -1 
   12    -1     def putstr(self, y, x, s):
   13    -1         """Write string to position."""
   14    -1         for i, ch in enumerate(s):
   15    -1             try:
   16    -1                 self.data[y][x + i] = ch
   17    -1             except IndexError:
   18    -1                 pass
   19    -1 
   20    -1     def refresh(self):
   21    -1         """Print the current state to the screen."""
   22    -1         raise NotImplementedError
   23    -1 
   24    -1     def cleanup(self):
   25    -1         """Deinitialize screen."""
   26    -1         pass
   27    -1 
   28    -1 
   29    -1 class Screen(BaseScreen):
   30    -1     """Additional utility functions for :py:class:`BaseScreen`."""
   31    -1 
   32    -1     def delch(self, y, x):
   33    -1         """Delete character at position."""
   34    -1         self.putstr(y, x, ' ')
   35    -1 
   36    -1     def fill_row(self, y, ch):
   37    -1         """Fill a complete row with character."""
   38    -1         self.putstr(y, 0, ch * self.width)
   39    -1 
   40    -1     def fill_column(self, x, ch):
   41    -1         """Fill a complete column with character."""
   42    -1         for y in range(self.height):
   43    -1             self.putstr(y, x, ch)
   44    -1 
   45    -1     def fill(self, ch):
   46    -1         """Fill whole screen with character."""
   47    -1         for y in range(self.height):
   48    -1             self.row(y, ch)
   49    -1 
   50    -1     def clear(self):
   51    -1         """Clear whole screen."""
   52    -1         self.fill(' ')
   53    -1 
   54    -1     def border(self, ls='|', rs='|', ts='-', bs='-',
   55    -1                tl='+', tr='+', bl='+', br='+'):
   56    -1         """Draw border around screen."""
   57    -1         self.fill_column(0, ls)
   58    -1         self.fill_column(self.width - 1, rs)
   59    -1         self.fill_row(0, ts)
   60    -1         self.fill_row(self.height - 1, bs)
   61    -1         self.putstr(0, 0, tl)
   62    -1         self.putstr(0, self.width - 1, tr)
   63    -1         self.putstr(self.height - 1, 0, bl)
   64    -1         self.putstr(self.height - 1, self.width - 1, br)
   65    -1 
   66    -1 
   67    -1 class Window(Screen):
   68    -1     """A screen that is rendered onto another screen."""
   69    -1 
   70    -1     def __init__(self, parent, height, width, y, x):
   71    -1         super(Window, self).__init__(height, width)
   72    -1         self.parent = parent
   73    -1         self.y = y
   74    -1         self.x = x
   75    -1 
   76    -1     def getch(self):
   77    -1         return self.parent.getch()
   78    -1 
   79    -1     def refresh(self):
   80    -1         for y in range(self.height):
   81    -1             for x in range(self.width):
   82    -1                 self.parent.putstr(self.y + y, self.x + x, self.data[y][x])
   83    -1         self.parent.refresh()

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

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

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

@@ -1,61 +0,0 @@
    1    -1 import sys
    2    -1 
    3    -1 try:
    4    -1     from pygame_dirtywords import Screen
    5    -1 except ImportError:
    6    -1     try:
    7    -1         from curses_dirtywords import Screen
    8    -1     except ImportError:
    9    -1         from stupid_dirtywords import Screen
   10    -1 
   11    -1 from attr_string import italic
   12    -1 from attr_string import blue
   13    -1 
   14    -1 
   15    -1 class Player(object):
   16    -1     def __init__(self, win):
   17    -1         self.win = win
   18    -1         self.y = win.height / 2
   19    -1         self.x = win.width / 2
   20    -1 
   21    -1     def move(self, direction):
   22    -1         self.win.putstr(self.y, self.x, ' ')
   23    -1 
   24    -1         if direction == 'up':
   25    -1             self.y -= 1
   26    -1         elif direction == 'right':
   27    -1             self.x += 1
   28    -1         elif direction == 'down':
   29    -1             self.y += 1
   30    -1         elif direction == 'left':
   31    -1             self.x -= 1
   32    -1 
   33    -1         self.win.putstr(self.y, self.x, italic(blue('X')))
   34    -1         self.win.refresh()
   35    -1 
   36    -1 
   37    -1 if __name__ == '__main__':
   38    -1     scr = Screen(32, 100)
   39    -1 
   40    -1     try:
   41    -1         scr.border()
   42    -1 
   43    -1         player = Player(scr)
   44    -1         player.move('down')  # initial refresh
   45    -1 
   46    -1         while 1:
   47    -1             ch = scr.getch()
   48    -1             if ch == ord('h'):
   49    -1                 player.move('left')
   50    -1             elif ch == ord('j'):
   51    -1                 player.move('down')
   52    -1             elif ch == ord('k'):
   53    -1                 player.move('up')
   54    -1             elif ch == ord('l'):
   55    -1                 player.move('right')
   56    -1             elif ch == ord('q'):
   57    -1                 scr.cleanup()
   58    -1                 sys.exit()
   59    -1     except:
   60    -1         scr.cleanup()
   61    -1         raise

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

@@ -1,46 +0,0 @@
    1    -1 import pygame
    2    -1 from pygame.locals import KEYDOWN
    3    -1 
    4    -1 import base
    5    -1 from attr_string import AttrString
    6    -1 
    7    -1 
    8    -1 class Screen(base.Screen):
    9    -1     def __init__(self, height, width):
   10    -1         super(Screen, self).__init__(height, width)
   11    -1 
   12    -1         pygame.init()
   13    -1         self.clock = pygame.time.Clock()
   14    -1 
   15    -1         self.font = pygame.font.SysFont('monospace', 10)
   16    -1         self.fontwidth, self.fontheight = self.font.size(' ')
   17    -1 
   18    -1         self.pygame_screen = pygame.display.set_mode(
   19    -1             (self.fontwidth * width, self.fontheight * height))
   20    -1 
   21    -1     def getch(self):
   22    -1         while True:
   23    -1             event = pygame.event.wait()
   24    -1             if event.type == KEYDOWN:
   25    -1                 return event.key
   26    -1 
   27    -1     def _render_ch(self, ch):
   28    -1         ch = AttrString(ch)
   29    -1         self.font.set_bold(ch.bold)
   30    -1         self.font.set_italic(ch.italic)
   31    -1         self.font.set_underline(ch.underline)
   32    -1         return self.font.render(ch, True, ch.fg_color, ch.bg_color)
   33    -1 
   34    -1     def putstr(self, y, x, s):
   35    -1         super(Screen, self).putstr(y, x, s)
   36    -1         for i, ch in enumerate(s):
   37    -1             self.pygame_screen.blit(
   38    -1                 self._render_ch(ch),
   39    -1                 ((x + i) * self.fontwidth, y * self.fontheight))
   40    -1 
   41    -1     def refresh(self):
   42    -1         self.clock.tick()
   43    -1         pygame.display.flip()
   44    -1 
   45    -1     def cleanup(self):
   46    -1         pygame.quit()

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

@@ -1,28 +0,0 @@
    1    -1 import sys
    2    -1 
    3    -1 import base
    4    -1 
    5    -1 
    6    -1 class Screen(base.Screen):
    7    -1     def getch(self):
    8    -1         # http://code.activestate.com/recipes/134892/
    9    -1 
   10    -1         try:
   11    -1             import msvcrt
   12    -1             return msvcrt.getch()
   13    -1         except ImportError:
   14    -1             import termios
   15    -1             import tty
   16    -1             fd = sys.stdin.fileno()
   17    -1             old_settings = termios.tcgetattr(fd)
   18    -1             try:
   19    -1                 tty.setraw(sys.stdin.fileno())
   20    -1                 ch = sys.stdin.read(1)
   21    -1             finally:
   22    -1                 termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
   23    -1             return ord(ch)
   24    -1 
   25    -1     def refresh(self):
   26    -1         spacing = '\n' * self.height * 2
   27    -1         s = '\n'.join([''.join(row) for row in self.data])
   28    -1         print(spacing + s)

diff --git a/setup.py b/setup.py

@@ -12,6 +12,7 @@ setup(
   12    12     packages=['laneya'],
   13    13     install_requires=[
   14    14         'twisted',
   -1    15         'dirtywords',
   15    16     ],
   16    17     entry_points={'console_scripts': [
   17    18         'laneya=laneya.client:main',