Mercurial > rinkhals
comparison gamelib/sprite_cursor.py @ 267:9cc7bc5cd10c
Refactor sprite cursor a bit to make sub-classing easier. Add (unused) SmallSpriteCursor sub-class. Remove unnecessary pygame.font import.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Sat, 05 Sep 2009 14:02:04 +0000 |
parents | 0bd214cf9018 |
children |
comparison
equal
deleted
inserted
replaced
266:390ef4387e05 | 267:9cc7bc5cd10c |
---|---|
2 | 2 |
3 Currently mostly used when placing buildings. | 3 Currently mostly used when placing buildings. |
4 """ | 4 """ |
5 | 5 |
6 import pygame | 6 import pygame |
7 import pygame.font | |
8 from pygame.locals import SRCALPHA | 7 from pygame.locals import SRCALPHA |
9 | 8 |
10 import imagecache | 9 import imagecache |
11 import constants | 10 import constants |
12 from pgu.vid import Sprite | 11 from pgu.vid import Sprite |
14 # ignore os.popen3 warning generated by pygame.font.SysFont | 13 # ignore os.popen3 warning generated by pygame.font.SysFont |
15 import warnings | 14 import warnings |
16 warnings.filterwarnings("ignore", "os.popen3 is deprecated.") | 15 warnings.filterwarnings("ignore", "os.popen3 is deprecated.") |
17 | 16 |
18 class SpriteCursor(Sprite): | 17 class SpriteCursor(Sprite): |
19 """A Sprite used as an on-board cursor.""" | 18 """A Sprite used as an on-board cursor. |
19 """ | |
20 | 20 |
21 def __init__(self, image_name, tv, cost=None): | 21 def __init__(self, image_name, tv, cost=None): |
22 self._font = pygame.font.SysFont('Vera', 20, bold=True) | 22 self._font = pygame.font.SysFont('Vera', 20, bold=True) |
23 image = imagecache.load_image(image_name, ["sprite_cursor"]) | 23 image = imagecache.load_image(image_name, ["sprite_cursor"]) |
24 | 24 |
25 if cost is not None: | 25 if cost is not None: |
26 image = image.copy() | 26 image = self._apply_text(image, str(cost)) |
27 text = self._font.render(str(cost), True, constants.FG_COLOR) | |
28 w, h = image.get_size() | |
29 x, y = text.get_size() | |
30 image.blit(text, (w - x, h - y)) | |
31 | 27 |
32 # Create the sprite somewhere far off screen | 28 # Create the sprite somewhere far off screen |
33 Sprite.__init__(self, image, (-1000, -1000)) | 29 Sprite.__init__(self, image, (-1000, -1000)) |
34 self._tv = tv | 30 self._tv = tv |
35 | 31 |
32 def _apply_text(self, image, stext): | |
33 """Apply the text to the image.""" | |
34 image = image.copy() | |
35 text = self._font.render(stext, True, constants.FG_COLOR) | |
36 w, h = image.get_size() | |
37 x, y = text.get_size() | |
38 image.blit(text, (w - x, h - y)) | |
39 return image | |
40 | |
36 def set_pos(self, tile_pos): | 41 def set_pos(self, tile_pos): |
37 """Set the cursor position on the gameboard.""" | 42 """Set the cursor position on the gameboard.""" |
38 self.rect.x, self.rect.y = self._tv.tile_to_view(tile_pos) | 43 self.rect.x, self.rect.y = self._tv.tile_to_view(tile_pos) |
44 | |
45 class SmallSpriteCursor(SpriteCursor): | |
46 """A sprite cursor for use with images too small for the associated text.""" | |
47 | |
48 def _apply_text(self, image, stext): | |
49 text = self._font.render(stext, True, constants.FG_COLOR) | |
50 w, h = image.get_size() | |
51 x, y = text.get_size() | |
52 | |
53 new_w, new_h = w + x, max(h, y) | |
54 | |
55 new_image = pygame.Surface((new_w, new_h), SRCALPHA) | |
56 new_image.blit(image, (0, 0)) | |
57 new_image.blit(text, (w, new_h - y)) | |
58 return new_image |