comparison gamelib/gameboard.py @ 186:f06010d34cd3

Add sprite cursors for building placement.
author Simon Cross <hodgestar@gmail.com>
date Fri, 04 Sep 2009 17:49:37 +0000
parents 2b9176c35397
children 37af9e5dd292
comparison
equal deleted inserted replaced
185:cef972d285f7 186:f06010d34cd3
1 import random 1 import random
2 2
3 import pygame 3 import pygame
4 from pygame.locals import MOUSEBUTTONDOWN, KEYDOWN, K_UP, K_DOWN, K_LEFT, K_RIGHT 4 from pygame.locals import MOUSEBUTTONDOWN, MOUSEMOTION, KEYDOWN, K_UP, K_DOWN, K_LEFT, K_RIGHT
5 from pgu import gui 5 from pgu import gui
6 6
7 import data 7 import data
8 import tiles 8 import tiles
9 import icons 9 import icons
11 import buildings 11 import buildings
12 import animal 12 import animal
13 import equipment 13 import equipment
14 import sound 14 import sound
15 import cursors 15 import cursors
16 import sprite_cursor
16 17
17 class OpaqueLabel(gui.Label): 18 class OpaqueLabel(gui.Label):
18 def __init__(self, value, **params): 19 def __init__(self, value, **params):
19 gui.Label.__init__(self, value, **params) 20 gui.Label.__init__(self, value, **params)
20 if 'width' in params: 21 if 'width' in params:
156 self.vid.view.move_ip((x, y)) 157 self.vid.view.move_ip((x, y))
157 158
158 def event(self, e): 159 def event(self, e):
159 if e.type == MOUSEBUTTONDOWN: 160 if e.type == MOUSEBUTTONDOWN:
160 self.gameboard.use_tool(e) 161 self.gameboard.use_tool(e)
162 elif e.type == MOUSEMOTION and self.gameboard.sprite_cursor:
163 self.gameboard.update_sprite_cursor(e)
161 164
162 165
163 class GameBoard(object): 166 class GameBoard(object):
164 TILE_DIMENSIONS = (20, 20) 167 TILE_DIMENSIONS = (20, 20)
165 TOOLBAR_WIDTH = 140 168 TOOLBAR_WIDTH = 140
177 self.tv.tga_load_level(data.filepath('level1.tga')) 180 self.tv.tga_load_level(data.filepath('level1.tga'))
178 self.create_display() 181 self.create_display()
179 182
180 self.selected_tool = None 183 self.selected_tool = None
181 self.animal_to_place = None 184 self.animal_to_place = None
185 self.sprite_cursor = None
182 self.chickens = set() 186 self.chickens = set()
183 self.foxes = set() 187 self.foxes = set()
184 self.buildings = [] 188 self.buildings = []
185 self.cash = 0 189 self.cash = 0
186 self.eggs = 0 190 self.eggs = 0
216 self.animal_to_place = None 220 self.animal_to_place = None
217 if cursor: 221 if cursor:
218 pygame.mouse.set_cursor(*cursor) 222 pygame.mouse.set_cursor(*cursor)
219 else: 223 else:
220 pygame.mouse.set_cursor(*cursors.cursors['arrow']) 224 pygame.mouse.set_cursor(*cursors.cursors['arrow'])
225 if self.sprite_cursor:
226 self.tv.sprites.remove(self.sprite_cursor)
227 self.sprite_cursor = None
228 if buildings.is_building(tool):
229 self.sprite_cursor = sprite_cursor.SpriteCursor(tool.IMAGE, self.tv)
230 self.tv.sprites.append(self.sprite_cursor)
221 231
222 def reset_cursor(self): 232 def reset_cursor(self):
223 pygame.mouse.set_cursor(*cursors.cursors['arrow']) 233 pygame.mouse.set_cursor(*cursors.cursors['arrow'])
234
235 def update_sprite_cursor(self, e):
236 tile_pos = self.tv.screen_to_tile(e.pos)
237 self.sprite_cursor.set_pos(tile_pos)
224 238
225 def in_bounds(self, pos): 239 def in_bounds(self, pos):
226 """Check if a position is within the game boundaries""" 240 """Check if a position is within the game boundaries"""
227 if pos.x < 0 or pos.y < 0: 241 if pos.x < 0 or pos.y < 0:
228 return False 242 return False