Changeset 267:9cc7bc5cd10c


Ignore:
Timestamp:
Sep 5, 2009, 2:02:04 PM (11 years ago)
Author:
Simon Cross <hodgestar@…>
Branch:
default
Convert:
svn:b4e93282-eac8-4b8b-b765-0f5d36de2b68@268
Message:

Refactor sprite cursor a bit to make sub-classing easier. Add (unused) SmallSpriteCursor? sub-class. Remove unnecessary pygame.font import.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gamelib/sprite_cursor.py

    r232 r267  
    55
    66import pygame
    7 import pygame.font
    87from pygame.locals import SRCALPHA
    98
     
    1716
    1817class SpriteCursor(Sprite):
    19     """A Sprite used as an on-board cursor."""
     18    """A Sprite used as an on-board cursor.
     19       """
    2020
    2121    def __init__(self, image_name, tv, cost=None):
     
    2424
    2525        if cost is not None:
    26             image = image.copy()
    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))
     26            image = self._apply_text(image, str(cost))
    3127
    3228        # Create the sprite somewhere far off screen
     
    3430        self._tv = tv
    3531
     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
    3641    def set_pos(self, tile_pos):
    3742        """Set the cursor position on the gameboard."""
    3843        self.rect.x, self.rect.y = self._tv.tile_to_view(tile_pos)
     44
     45class 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
Note: See TracChangeset for help on using the changeset viewer.