comparison 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
comparison
equal deleted inserted replaced
48:030bea282f28 49:dd9d2a4dd494
1 """Extension to pgu's tilevid.""" 1 """Extension to pgu's tilevid."""
2 2
3 from pgu import tilevid, vid 3 from pgu import tilevid, vid
4 import pygame
5 from pygame.locals import BLEND_RGBA_MULT
6 import os 4 import os
5 import imagecache
7 6
8 TILE_MAP = { 7 TILE_MAP = {
9 0: "woodland", 8 0: "woodland",
10 1: "grassland", 9 1: "grassland",
11 2: "fence", 10 2: "fence",
23 tilevid.Tilevid.__init__(self) 22 tilevid.Tilevid.__init__(self)
24 23
25 def png_folder_load_tiles(self, path): 24 def png_folder_load_tiles(self, path):
26 """Load tiles from a folder of PNG files.""" 25 """Load tiles from a folder of PNG files."""
27 for dirpath, dirnames, filenames in os.walk(path): 26 for dirpath, dirnames, filenames in os.walk(path):
27 abstract_dirpath = "/".join(dirpath.split(os.path.sep))
28 for filename in filenames: 28 for filename in filenames:
29 basename, ext = os.path.splitext(filename) 29 basename, ext = os.path.splitext(filename)
30 if ext != ".png": 30 if ext != ".png":
31 continue 31 continue
32 if basename in REVERSE_TILE_MAP: 32 if basename in REVERSE_TILE_MAP:
33 n = REVERSE_TILE_MAP[basename] 33 n = REVERSE_TILE_MAP[basename]
34 img = pygame.image.load(os.path.join(dirpath, filename)).convert_alpha() 34 self.tiles[n] = FarmTile(abstract_dirpath + "/" + filename)
35 self.tiles[n] = FarmTile(img)
36 35
37 def sun(self, sun_on): 36 def sun(self, sun_on):
38 """Make it night.""" 37 """Make it night."""
39 for tile in self.tiles: 38 for tile in self.tiles:
40 if hasattr(tile, "sun"): 39 if hasattr(tile, "sun"):
43 if hasattr(sprite, "sun"): 42 if hasattr(sprite, "sun"):
44 sprite.sun(sun_on) 43 sprite.sun(sun_on)
45 44
46 class FarmTile(vid.Tile): 45 class FarmTile(vid.Tile):
47 46
48 NIGHT_COLOUR = (100.0, 100.0, 200.0, 255.0) 47 def __init__(self, image_name):
49 48 self.day_image = imagecache.load_image(image_name)
50 def __init__(self, image): 49 self.night_image = imagecache.load_image(image_name, ("night",))
51 self.day_image = image
52 self.night_image = image.copy()
53 self.night_image.fill(self.NIGHT_COLOUR, None, BLEND_RGBA_MULT)
54 self.image = self.day_image 50 self.image = self.day_image
55 51
56 def sun(self, sun_on): 52 def sun(self, sun_on):
57 if sun_on: 53 if sun_on:
58 self.image = self.day_image 54 self.image = self.day_image