Mercurial > rinkhals
changeset 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 | 390ef4387e05 |
children | 47c411d20b00 |
files | gamelib/sprite_cursor.py |
diffstat | 1 files changed, 27 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/gamelib/sprite_cursor.py Sat Sep 05 14:01:14 2009 +0000 +++ b/gamelib/sprite_cursor.py Sat Sep 05 14:02:04 2009 +0000 @@ -4,7 +4,6 @@ """ import pygame -import pygame.font from pygame.locals import SRCALPHA import imagecache @@ -16,23 +15,44 @@ warnings.filterwarnings("ignore", "os.popen3 is deprecated.") class SpriteCursor(Sprite): - """A Sprite used as an on-board cursor.""" + """A Sprite used as an on-board cursor. + """ def __init__(self, image_name, tv, cost=None): self._font = pygame.font.SysFont('Vera', 20, bold=True) image = imagecache.load_image(image_name, ["sprite_cursor"]) if cost is not None: - image = image.copy() - text = self._font.render(str(cost), True, constants.FG_COLOR) - w, h = image.get_size() - x, y = text.get_size() - image.blit(text, (w - x, h - y)) + image = self._apply_text(image, str(cost)) # Create the sprite somewhere far off screen Sprite.__init__(self, image, (-1000, -1000)) self._tv = tv + def _apply_text(self, image, stext): + """Apply the text to the image.""" + image = image.copy() + text = self._font.render(stext, True, constants.FG_COLOR) + w, h = image.get_size() + x, y = text.get_size() + image.blit(text, (w - x, h - y)) + return image + def set_pos(self, tile_pos): """Set the cursor position on the gameboard.""" self.rect.x, self.rect.y = self._tv.tile_to_view(tile_pos) + +class SmallSpriteCursor(SpriteCursor): + """A sprite cursor for use with images too small for the associated text.""" + + def _apply_text(self, image, stext): + text = self._font.render(stext, True, constants.FG_COLOR) + w, h = image.get_size() + x, y = text.get_size() + + new_w, new_h = w + x, max(h, y) + + new_image = pygame.Surface((new_w, new_h), SRCALPHA) + new_image.blit(image, (0, 0)) + new_image.blit(text, (w, new_h - y)) + return new_image