source: nagslang/level.py@ 66:8bf0459ebc56

Last change on this file since 66:8bf0459ebc56 was 54:2c1b85b6f457, checked in by Simon Cross <hodgestar@…>, 8 years ago

Add .get_file() to resources.

File size: 3.7 KB
Line 
1import pygame
2import pygame.locals as pgl
3
4from nagslang.resources import resources
5
6POLY_COLORS = {
7 1: pygame.color.THECOLORS['red'],
8 2: pygame.color.THECOLORS['green'],
9 3: pygame.color.THECOLORS['yellow'],
10 4: pygame.color.THECOLORS['blue'],
11 5: pygame.color.THECOLORS['lightblue'],
12 6: pygame.color.THECOLORS['magenta'],
13}
14
15
16class Level(object):
17
18 def __init__(self, name):
19 self.name = name
20 # defaults
21 self.x = 800
22 self.y = 600
23 self.polygons = {}
24 self.basetile = 'tiles/floor.png'
25 self._tile_image = None
26 self._surface = None
27
28 def load(self):
29
30 def add_polygon(polygon, index, num_points):
31 self.polygons[index] = polygon
32 if len(polygon) != num_points:
33 print 'Error - incorrect polygon size'
34 print 'Expected: %d, got %d' % (num_points, len(polygon))
35
36 inpoly = False
37 polygon = []
38 index = 0
39 num_points = 0
40 with resources.get_file(self.name) as f:
41 for line in f:
42 if inpoly:
43 if not line.startswith('Point:'):
44 add_polygon(polygon, index, num_points)
45 polygon = []
46 inpoly = False
47 index = 0
48 else:
49 point = line.split(':', 1)[1]
50 x, y = [int(i) for i in point.split()]
51 polygon.append((x, y))
52 if line.startswith('X-Size:'):
53 self.x = int(line.split(':', 1)[1])
54 elif line.startswith('Y-Size:'):
55 self.y = int(line.split(':', 1)[1])
56 elif line.startswith('Base tile:'):
57 self.basetile = line.split(':', 1)[1].strip()
58 elif line.startswith('Polygon'):
59 rest = line.split(' ', 1)[1]
60 index, num_points = [int(x) for x in rest.split(':', 1)]
61 inpoly = True
62 if index:
63 add_polygon(polygon, index, num_points)
64
65 def get_size(self):
66 return self.x, self.y
67
68 def set_base_tile(self, new_tile):
69 self.basetile = new_tile
70 self._tile_image = None
71
72 def point_to_pygame(self, pos):
73 # Convert a point from pymunk (which is what we store)
74 # to pygame for drawing
75 return (pos[0], self.y - pos[1])
76
77 def get_walls(self):
78 return self.polygons.values()
79
80 def _draw_walls(self):
81 for index, polygon in self.polygons.items():
82 color = POLY_COLORS[index]
83 if len(polygon) > 1:
84 pointlist = [self.point_to_pygame(p) for p in polygon]
85 pygame.draw.lines(self._surface, color, False, pointlist, 2)
86
87 def get_background(self):
88 self._draw_background()
89 # Draw polygons
90 self._draw_walls()
91 return self._surface
92
93 def _draw_background(self, force=False):
94 if self._tile_image is None:
95 self._tile_image = resources.get_image(self.basetile)
96 if self._surface is not None and not force:
97 # We assume we don't change
98 return self._surface
99 self._surface = pygame.surface.Surface((self.x, self.y), pgl.SRCALPHA)
100 self._surface.fill(pygame.color.THECOLORS['black'])
101 x_step = self._tile_image.get_rect().width
102 y_step = self._tile_image.get_rect().height
103 x_count = self.x // x_step + 1
104 y_count = self.y / y_step + 1
105 for x in range(x_count):
106 for y in range(y_count):
107 tile_rect = pygame.rect.Rect(x * x_step, y * y_step,
108 x_step, y_step)
109 self._surface.blit(self._tile_image, tile_rect)
110 return self._surface
Note: See TracBrowser for help on using the repository browser.