view gamelib/sprite_cursor.py @ 266:390ef4387e05

Update TODO
author Neil Muller <drnlmuller@gmail.com>
date Sat, 05 Sep 2009 14:01:14 +0000
parents 0bd214cf9018
children 9cc7bc5cd10c
line wrap: on
line source

"""In-game sprite cursors for the gameboard.

   Currently mostly used when placing buildings.
   """

import pygame
import pygame.font
from pygame.locals import SRCALPHA

import imagecache
import constants
from pgu.vid import Sprite

# ignore os.popen3 warning generated by pygame.font.SysFont
import warnings
warnings.filterwarnings("ignore", "os.popen3 is deprecated.")

class SpriteCursor(Sprite):
    """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))

        # Create the sprite somewhere far off screen
        Sprite.__init__(self, image, (-1000, -1000))
        self._tv = tv

    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)