source: gamelib/tiles.py @ 62:98ce098f8cbb

Last change on this file since 62:98ce098f8cbb was 62:98ce098f8cbb, checked in by Neil Muller <drnlmuller@…>, 11 years ago

Add broken fence tile

File size: 2.7 KB
Line 
1"""Extension to pgu's tilevid."""
2
3from pgu import tilevid, vid
4import os
5import imagecache
6
7class TileMap(object):
8    """Helper class for describing all the game tiles."""
9
10    # number -> (name, image file name)
11    DEFAULT_TILES = {
12        0: ("woodland", "woodland.png"),
13        1: ("grassland", "grassland.png"),
14        2: ("fence", "fence.png"),
15        3: ("henhouse", "grassland.png"),
16        4: ("guardtower", "guardtower.png"),
17        5: ("broken fence", "broken_fence.png"),
18    }
19
20    def __init__(self, tiles=None):
21        if tiles is None:
22            tiles = self.DEFAULT_TILES.copy()
23        self._tiles = tiles
24        self._reverse_map = dict((v[0], k) for k, v in self._tiles.iteritems())
25
26    def __getitem__(self, n):
27        """Get the string name of tile n."""
28        return self._tiles[n][0]
29
30    def tile_number(self, name):
31        """Return the tile number of the tile with the given name."""
32        return self._reverse_map[name]
33
34    def tiles_for_image(self, image_name):
35        """Return tile numbers associated with the given image name."""
36        for n, (name, image) in self._tiles.iteritems():
37            if image_name == image:
38                yield n
39
40TILE_MAP = TileMap()
41REVERSE_TILE_MAP = TILE_MAP._reverse_map
42
43
44class FarmVid(tilevid.Tilevid):
45    """Extension of pgu's TileVid class to handle the complications
46       of raising chickens.
47       """
48    def __init__(self):
49        tilevid.Tilevid.__init__(self)
50
51    def png_folder_load_tiles(self, path):
52        """Load tiles from a folder of PNG files."""
53        for dirpath, dirnames, filenames in os.walk(path):
54            abstract_dirpath = "/".join(dirpath.split(os.path.sep))
55            for filename in filenames:
56                image_name = abstract_dirpath + "/" + filename
57                for tile_no in TILE_MAP.tiles_for_image(filename):
58                    tile_name = TILE_MAP[tile_no]
59                    self.tiles[tile_no] = FarmTile(tile_no, tile_name, image_name)
60
61    def sun(self, sun_on):
62        """Make it night."""
63        for tile in self.tiles:
64            if hasattr(tile, "sun"):
65                tile.sun(sun_on)
66        for sprite in self.sprites:
67            if hasattr(sprite, "sun"):
68                sprite.sun(sun_on)
69
70class FarmTile(vid.Tile):
71
72    def __init__(self, tile_no, tile_name, image_name):
73        self.tile_no = tile_no
74        self.tile_name = tile_name
75        self.day_image = imagecache.load_image(image_name)
76        self.night_image = imagecache.load_image(image_name, ("night",))
77        vid.Tile.__init__(self, self.day_image)
78
79    def sun(self, sun_on):
80        if sun_on:
81            self.image = self.day_image
82        else:
83            self.image = self.night_image
Note: See TracBrowser for help on using the repository browser.