source: regenerate_level_data.py @ 386:9f5cc2a880d5

Last change on this file since 386:9f5cc2a880d5 was 386:9f5cc2a880d5, checked in by Neil Muller <drnlmuller@…>, 11 years ago

Add more informative message

  • Property exe set to *
File size: 2.1 KB
Line 
1#!/usr/bin/env python
2
3import os.path
4
5import pygame
6from pygame.locals import SWSURFACE, SRCALPHA
7
8from gamelib import tiles, buildings
9import regenerate_pngs
10
11LEVEL_PATH = "data/levels"
12TILE_WIDTH = 20
13TILE_HEIGHT = 20
14TILES_X = 8
15TILES_Y = 8
16WIDTH = TILES_X * TILE_WIDTH
17HEIGHT = TILES_Y * TILE_HEIGHT
18
19def generate_image(name, basepath):
20    fn, _ = os.path.splitext(os.path.basename(name))
21    svg_name = os.path.join(basepath, fn+".svg")
22    if not os.path.exists(svg_name):
23        print 'Skipping %s, as it seems to be missing' % svg_name
24        return None
25    png_name = os.path.join(LEVEL_PATH, fn+".png")
26    regenerate_pngs.svg_to_png(svg_name, png_name, TILE_WIDTH, TILE_HEIGHT)
27    return pygame.image.load(png_name)
28
29def get_tile_mappings():
30    tile_map = {}
31    for building in buildings.BUILDINGS:
32        tn = building.TILE_NO
33        image = generate_image(building.IMAGE, regenerate_pngs.SPRITE_PATH)
34        if image:
35            tile_map[tn] = image
36    for tn, (_, tile_png) in tiles.TileMap.DEFAULT_TILES.items():
37        if tn not in tile_map:
38            tile_map[tn] = generate_image(tile_png, regenerate_pngs.TILE_PATH)
39    return tile_map
40
41def get_code_mappings():
42    code_map = {}
43    # this list needs to manually kept in sync. This should be fixed
44    for tn, sprite_name in [(1, 'chkn')]:
45        image = generate_image(sprite_name, regenerate_pngs.SPRITE_PATH)
46        if image:
47            code_map[tn] = image
48    return code_map
49
50if __name__ == '__main__':
51    s = pygame.Surface((WIDTH, HEIGHT), SWSURFACE|SRCALPHA, 32)
52    s.fill((0,0,0,0))
53    for n, img in get_tile_mappings().items():
54        rect = (TILE_WIDTH*(n % TILES_X), TILE_HEIGHT*(n / TILES_X), TILE_WIDTH, TILE_HEIGHT)
55        print n, rect
56        s.blit(img, rect)
57    pygame.image.save(s, os.path.join(LEVEL_PATH, "tiles.tga"))
58    s.fill((0,0,0,0))
59    for n, img in get_code_mappings().items():
60        rect = (TILE_WIDTH*(n % TILES_X), TILE_HEIGHT*(n / TILES_X), TILE_WIDTH, TILE_HEIGHT)
61        print n, rect
62        s.blit(img, rect)
63    pygame.image.save(s, os.path.join(LEVEL_PATH, "codes.tga"))
Note: See TracBrowser for help on using the repository browser.