comparison gamelib/buildings.py @ 57:08665fa60345

Implement henhouses and henhouse adding.
author Simon Cross <hodgestar@gmail.com>
date Mon, 31 Aug 2009 18:34:10 +0000
parents fb5be14ea930
children 1047ccd22dac
comparison
equal deleted inserted replaced
56:205789321a46 57:08665fa60345
1 """Classes for various buildings in the game.""" 1 """Classes for various buildings in the game."""
2 2
3 from pgu.vid import Sprite 3 from pgu.vid import Sprite
4 4
5 import imagecache 5 import imagecache
6 import tiles
6 7
7 class Building(Sprite): 8 class Building(Sprite):
8 """Base class for buildings""" 9 """Base class for buildings"""
9 10
10 def __init__(self, image, pos): 11 GRASSLAND = tiles.REVERSE_TILE_MAP['grassland']
12
13 def __init__(self, day_image, night_image, pos, size, tile_no):
14 """Initial image, tile vid position, size and tile number for building."""
11 # Create the building somewhere far off screen 15 # Create the building somewhere far off screen
12 Sprite.__init__(self, image, (-1000, -1000)) 16 Sprite.__init__(self, day_image, (-1000, -1000))
17 self.day_image = day_image
18 self.night_image = night_image
13 self.pos = pos 19 self.pos = pos
20 self.size = size
21 self.tile_no = tile_no
22
23 def tile_positions(self):
24 """Return pairs of (x, y) tile positions for each of the tile positions
25 occupied by the building."""
26 xpos, ypos = self.pos
27 xsize, ysize = self.size
28
29 for dx in xrange(xsize):
30 for dy in xrange(ysize):
31 yield (xpos + dx, ypos + dy)
14 32
15 def loop(self, tv, _sprite): 33 def loop(self, tv, _sprite):
16 ppos = tv.tile_to_view(self.pos) 34 ppos = tv.tile_to_view(self.pos)
17 self.rect.x = ppos[0] 35 self.rect.x = ppos[0]
18 self.rect.y = ppos[1] 36 self.rect.y = ppos[1]
20 def move(self, state): 38 def move(self, state):
21 """Given the game state, return a new position for the object""" 39 """Given the game state, return a new position for the object"""
22 # Default is not to move 40 # Default is not to move
23 return self.pos 41 return self.pos
24 42
43 def place(self, tv):
44 """Check that the building can be placed at its current position
45 and place it if possible.
46 """
47 xpos, ypos = self.pos
48 xsize, ysize = self.size
49
50 # check that all spaces under the structure are grassland
51 for tile_pos in self.tile_positions():
52 if not tv.get(tile_pos) == self.GRASSLAND:
53 return False
54
55 # place tile
56 for tile_pos in self.tile_positions():
57 tv.set(tile_pos, self.tile_no)
58
59 return True
60
61 def sun(self, sun_on):
62 if sun_on:
63 self.setimage(self.day_image)
64 else:
65 self.setimage(self.night_image)
66
25 class HenHouse(Building): 67 class HenHouse(Building):
26 """A HenHouse.""" 68 """A HenHouse."""
27 69
70 HENHOUSE = tiles.REVERSE_TILE_MAP['henhouse']
71
28 def __init__(self, pos): 72 def __init__(self, pos):
29 image = imagecache.load_image('sprites/henhouse.png') 73 day_image = imagecache.load_image('sprites/henhouse.png')
30 Building.__init__(self, image, pos) 74 night_image = imagecache.load_image('sprites/henhouse.png', ('night',))
75 size = (3, 2)
76 Building.__init__(self, day_image, night_image, pos, size, self.HENHOUSE)
77
78
79 class GuardTower(Building):
80 """A GuardTower."""
81
82 GUARDTOWER = tiles.REVERSE_TILE_MAP['guardtower']
83
84 def __init__(self, pos):
85 day_image = imagecache.load_image('sprites/guardtower.png')
86 night_image = imagecache.load_image('sprites/guardtower.png', ('night',))
87 size = (1, 1)
88 Building.__init__(self, day_image, night_image, pos, size, self.GUARDTOWER)