# HG changeset patch # User Simon Cross # Date 1252086577 0 # Node ID f06010d34cd3fa2d9bc041c6403b71c7676ea1af # Parent cef972d285f77adbf564020c341645c1d211c84b Add sprite cursors for building placement. diff -r cef972d285f7 -r f06010d34cd3 gamelib/gameboard.py --- a/gamelib/gameboard.py Fri Sep 04 17:39:29 2009 +0000 +++ b/gamelib/gameboard.py Fri Sep 04 17:49:37 2009 +0000 @@ -1,7 +1,7 @@ import random import pygame -from pygame.locals import MOUSEBUTTONDOWN, KEYDOWN, K_UP, K_DOWN, K_LEFT, K_RIGHT +from pygame.locals import MOUSEBUTTONDOWN, MOUSEMOTION, KEYDOWN, K_UP, K_DOWN, K_LEFT, K_RIGHT from pgu import gui import data @@ -13,6 +13,7 @@ import equipment import sound import cursors +import sprite_cursor class OpaqueLabel(gui.Label): def __init__(self, value, **params): @@ -158,6 +159,8 @@ def event(self, e): if e.type == MOUSEBUTTONDOWN: self.gameboard.use_tool(e) + elif e.type == MOUSEMOTION and self.gameboard.sprite_cursor: + self.gameboard.update_sprite_cursor(e) class GameBoard(object): @@ -179,6 +182,7 @@ self.selected_tool = None self.animal_to_place = None + self.sprite_cursor = None self.chickens = set() self.foxes = set() self.buildings = [] @@ -218,10 +222,20 @@ pygame.mouse.set_cursor(*cursor) else: pygame.mouse.set_cursor(*cursors.cursors['arrow']) + if self.sprite_cursor: + self.tv.sprites.remove(self.sprite_cursor) + self.sprite_cursor = None + if buildings.is_building(tool): + self.sprite_cursor = sprite_cursor.SpriteCursor(tool.IMAGE, self.tv) + self.tv.sprites.append(self.sprite_cursor) def reset_cursor(self): pygame.mouse.set_cursor(*cursors.cursors['arrow']) + def update_sprite_cursor(self, e): + tile_pos = self.tv.screen_to_tile(e.pos) + self.sprite_cursor.set_pos(tile_pos) + def in_bounds(self, pos): """Check if a position is within the game boundaries""" if pos.x < 0 or pos.y < 0: diff -r cef972d285f7 -r f06010d34cd3 gamelib/imagecache.py --- a/gamelib/imagecache.py Fri Sep 04 17:39:29 2009 +0000 +++ b/gamelib/imagecache.py Fri Sep 04 17:49:37 2009 +0000 @@ -56,7 +56,7 @@ # modifiers -from pygame.locals import BLEND_MULT, BLEND_ADD +from pygame.locals import BLEND_MULT, BLEND_ADD, BLEND_RGBA_MULT NIGHT_COLOUR = (100.0, 100.0, 200.0, 255.0) DARKEN_COLOUR = (100.0, 100.0, 100.0, 255.0) LIGHTEN_COLOUR = (200.0, 200.0, 200.0, 225.0) @@ -92,6 +92,11 @@ lighten.blit(overlay, (over_x, over_y), None, BLEND_ADD) return lighten +def sprite_cursor(image): + cursor = image.copy() + cursor.fill((255, 255, 255, 100), None, BLEND_RGBA_MULT) + return cursor + # globals cache = ImageCache() @@ -99,4 +104,5 @@ cache.register_modifier("right_facing", convert_to_right_facing) cache.register_modifier("darken_center", darken_center) cache.register_modifier("lighten_most", lighten_most) +cache.register_modifier("sprite_cursor", sprite_cursor) load_image = cache.load_image diff -r cef972d285f7 -r f06010d34cd3 gamelib/sprite_cursor.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gamelib/sprite_cursor.py Fri Sep 04 17:49:37 2009 +0000 @@ -0,0 +1,20 @@ +"""In-game sprite cursors for the gameboard. + + Currently mostly used when placing buildings. + """ + +import imagecache +from pgu.vid import Sprite + +class SpriteCursor(Sprite): + """A Sprite used as an on-board cursor.""" + + def __init__(self, image_name, tv): + image = imagecache.load_image(image_name, ["sprite_cursor"]) + # 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)