Mercurial > rinkhals
annotate gamelib/tiles.py @ 49:dd9d2a4dd494
Update tile loading to use imagecache and new data.py auto-converting of paths.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Mon, 31 Aug 2009 17:01:59 +0000 |
parents | 2eec29085060 |
children | 32fb395cf71c |
rev | line source |
---|---|
24 | 1 """Extension to pgu's tilevid.""" |
2 | |
3 from pgu import tilevid, vid | |
4 import os | |
49
dd9d2a4dd494
Update tile loading to use imagecache and new data.py auto-converting of paths.
Simon Cross <hodgestar@gmail.com>
parents:
30
diff
changeset
|
5 import imagecache |
24 | 6 |
7 TILE_MAP = { | |
8 0: "woodland", | |
9 1: "grassland", | |
10 2: "fence", | |
11 3: "henhouse", | |
12 4: "chicken", | |
13 } | |
14 | |
15 REVERSE_TILE_MAP = dict((v, k) for k, v in TILE_MAP.iteritems()) | |
16 | |
17 class FarmVid(tilevid.Tilevid): | |
18 """Extension of pgu's TileVid class to handle the complications | |
19 of raising chickens. | |
20 """ | |
21 def __init__(self): | |
22 tilevid.Tilevid.__init__(self) | |
23 | |
24 def png_folder_load_tiles(self, path): | |
25 """Load tiles from a folder of PNG files.""" | |
26 for dirpath, dirnames, filenames in os.walk(path): | |
49
dd9d2a4dd494
Update tile loading to use imagecache and new data.py auto-converting of paths.
Simon Cross <hodgestar@gmail.com>
parents:
30
diff
changeset
|
27 abstract_dirpath = "/".join(dirpath.split(os.path.sep)) |
24 | 28 for filename in filenames: |
29 basename, ext = os.path.splitext(filename) | |
30 if ext != ".png": | |
31 continue | |
32 if basename in REVERSE_TILE_MAP: | |
33 n = REVERSE_TILE_MAP[basename] | |
49
dd9d2a4dd494
Update tile loading to use imagecache and new data.py auto-converting of paths.
Simon Cross <hodgestar@gmail.com>
parents:
30
diff
changeset
|
34 self.tiles[n] = FarmTile(abstract_dirpath + "/" + filename) |
24 | 35 |
30 | 36 def sun(self, sun_on): |
37 """Make it night.""" | |
38 for tile in self.tiles: | |
39 if hasattr(tile, "sun"): | |
40 tile.sun(sun_on) | |
41 for sprite in self.sprites: | |
42 if hasattr(sprite, "sun"): | |
43 sprite.sun(sun_on) | |
44 | |
24 | 45 class FarmTile(vid.Tile): |
30 | 46 |
49
dd9d2a4dd494
Update tile loading to use imagecache and new data.py auto-converting of paths.
Simon Cross <hodgestar@gmail.com>
parents:
30
diff
changeset
|
47 def __init__(self, image_name): |
dd9d2a4dd494
Update tile loading to use imagecache and new data.py auto-converting of paths.
Simon Cross <hodgestar@gmail.com>
parents:
30
diff
changeset
|
48 self.day_image = imagecache.load_image(image_name) |
dd9d2a4dd494
Update tile loading to use imagecache and new data.py auto-converting of paths.
Simon Cross <hodgestar@gmail.com>
parents:
30
diff
changeset
|
49 self.night_image = imagecache.load_image(image_name, ("night",)) |
30 | 50 self.image = self.day_image |
51 | |
52 def sun(self, sun_on): | |
53 if sun_on: | |
54 self.image = self.day_image | |
55 else: | |
56 self.image = self.night_image |