# HG changeset patch # User Simon Cross # Date 1252159324 0 # Node ID 9cc7bc5cd10c0b50e13f71f1923a333ed8e4d6ed # Parent 390ef4387e05cb8444b1d109105c3e8ee61946b1 Refactor sprite cursor a bit to make sub-classing easier. Add (unused) SmallSpriteCursor sub-class. Remove unnecessary pygame.font import. diff -r 390ef4387e05 -r 9cc7bc5cd10c gamelib/sprite_cursor.py --- 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