24
|
1 """Extension to pgu's tilevid."""
|
|
2
|
|
3 from pgu import tilevid, vid
|
|
4 import pygame
|
30
|
5 from pygame.locals import BLEND_RGBA_MULT
|
24
|
6 import os
|
|
7
|
|
8 TILE_MAP = {
|
|
9 0: "woodland",
|
|
10 1: "grassland",
|
|
11 2: "fence",
|
|
12 3: "henhouse",
|
|
13 4: "chicken",
|
|
14 }
|
|
15
|
|
16 REVERSE_TILE_MAP = dict((v, k) for k, v in TILE_MAP.iteritems())
|
|
17
|
|
18 class FarmVid(tilevid.Tilevid):
|
|
19 """Extension of pgu's TileVid class to handle the complications
|
|
20 of raising chickens.
|
|
21 """
|
|
22 def __init__(self):
|
|
23 tilevid.Tilevid.__init__(self)
|
|
24
|
|
25 def png_folder_load_tiles(self, path):
|
|
26 """Load tiles from a folder of PNG files."""
|
|
27 for dirpath, dirnames, filenames in os.walk(path):
|
|
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]
|
|
34 img = pygame.image.load(os.path.join(dirpath, filename)).convert_alpha()
|
|
35 self.tiles[n] = FarmTile(img)
|
|
36
|
30
|
37 def sun(self, sun_on):
|
|
38 """Make it night."""
|
|
39 for tile in self.tiles:
|
|
40 if hasattr(tile, "sun"):
|
|
41 tile.sun(sun_on)
|
|
42 for sprite in self.sprites:
|
|
43 if hasattr(sprite, "sun"):
|
|
44 sprite.sun(sun_on)
|
|
45
|
24
|
46 class FarmTile(vid.Tile):
|
30
|
47
|
|
48 NIGHT_COLOUR = (100.0, 100.0, 200.0, 255.0)
|
|
49
|
|
50 def __init__(self, image):
|
|
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
|
|
55
|
|
56 def sun(self, sun_on):
|
|
57 if sun_on:
|
|
58 self.image = self.day_image
|
|
59 else:
|
|
60 self.image = self.night_image
|