source: nagslang/level.py@ 53:39d346467052

Last change on this file since 53:39d346467052 was 53:39d346467052, checked in by Simon Cross <hodgestar@…>, 8 years ago

Draw all the walls.

File size: 3.8 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 levelfile = resources.get_resource_path(self.name)
37 inpoly = False
38 polygon = []
39 index = 0
40 num_points = 0
41 with file(levelfile, 'rU') as f:
42 for line in f:
43 if inpoly:
44 if not line.startswith('Point:'):
45 add_polygon(polygon, index, num_points)
46 polygon = []
47 inpoly = False
48 index = 0
49 else:
50 point = line.split(':', 1)[1]
51 x, y = [int(i) for i in point.split()]
52 polygon.append((x, y))
53 if line.startswith('X-Size:'):
54 self.x = int(line.split(':', 1)[1])
55 elif line.startswith('Y-Size:'):
56 self.y = int(line.split(':', 1)[1])
57 elif line.startswith('Base tile:'):
58 self.basetile = line.split(':', 1)[1].strip()
59 elif line.startswith('Polygon'):
60 rest = line.split(' ', 1)[1]
61 index, num_points = [int(x) for x in rest.split(':', 1)]
62 inpoly = True
63 if index:
64 add_polygon(polygon, index, num_points)
65
66 def get_size(self):
67 return self.x, self.y
68
69 def set_base_tile(self, new_tile):
70 self.basetile = new_tile
71 self._tile_image = None
72
73 def point_to_pygame(self, pos):
74 # Convert a point from pymunk (which is what we store)
75 # to pygame for drawing
76 return (pos[0], self.y - pos[1])
77
78 def get_walls(self):
79 return self.polygons.values()
80
81 def _draw_walls(self):
82 for index, polygon in self.polygons.items():
83 color = POLY_COLORS[index]
84 if len(polygon) > 1:
85 pointlist = [self.point_to_pygame(p) for p in polygon]
86 pygame.draw.lines(self._surface, color, False, pointlist, 2)
87
88 def get_background(self):
89 self._draw_background()
90 # Draw polygons
91 self._draw_walls()
92 return self._surface
93
94 def _draw_background(self, force=False):
95 if self._tile_image is None:
96 self._tile_image = resources.get_image(self.basetile)
97 if self._surface is not None and not force:
98 # We assume we don't change
99 return self._surface
100 self._surface = pygame.surface.Surface((self.x, self.y), pgl.SRCALPHA)
101 self._surface.fill(pygame.color.THECOLORS['black'])
102 x_step = self._tile_image.get_rect().width
103 y_step = self._tile_image.get_rect().height
104 x_count = self.x // x_step + 1
105 y_count = self.y / y_step + 1
106 for x in range(x_count):
107 for y in range(y_count):
108 tile_rect = pygame.rect.Rect(x * x_step, y * y_step,
109 x_step, y_step)
110 self._surface.blit(self._tile_image, tile_rect)
111 return self._surface
Note: See TracBrowser for help on using the repository browser.