laneya

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

commit
a142837ba608d7a299f3b0d52e663903d32646d9
parent
c77e5eb4d0acf32069b1938070c85393a91011d2
Author
Tobias Bengfort <tobias.bengfort@gmx.net>
Date
2014-10-06 21:36
pygame AttrString implementation

Diffstat

M laneya/dirtywords/pygame_dirtywords.py 29 +++++++++++++++++------------

1 files changed, 17 insertions, 12 deletions


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

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