source: nagslang/level.py@ 82:11b0017b5e4b

Last change on this file since 82:11b0017b5e4b was 75:79748a884eb5, checked in by Stefano Rivera <stefano@…>, 8 years ago

Put levels in a levels directory

File size: 4.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 inpoly = False
37 polygon = []
38 index = 0
39 num_points = 0
40 with resources.get_file('levels', 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 save(self):
66 closed = True
67 for poly in self.polygons.values():
68 if len(poly) == 0:
69 # We ignore empty polygons
70 continue
71 elif len(poly) == 1:
72 closed = False
73 print "\033[31mError: polygon too small\033[0m"
74 elif poly[-1] != poly[0]:
75 closed = False
76 print "\033[31mError: polygon not closed\033[0m"
77 if not closed:
78 print 'Not saving the level'
79 return
80 with resources.get_file('levels', self.name, mode='w') as f:
81 f.write('X-Size: %s\n' % self.x)
82 f.write('Y-Size: %s\n' % self.y)
83 f.write('Base tile: %s\n' % self.basetile)
84 for i, poly in self.polygons.items():
85 if len(poly) == 0:
86 continue
87 f.write('Polygon %d : %d\n' % (i, len(poly)))
88 for point in poly:
89 f.write('Point: %d %d\n' % point)
90 print 'level %s saved' % self.name
91
92 def get_size(self):
93 return self.x, self.y
94
95 def set_base_tile(self, new_tile):
96 self.basetile = new_tile
97 self._tile_image = None
98
99 def point_to_pygame(self, pos):
100 # Convert a point from pymunk (which is what we store)
101 # to pygame for drawing
102 return (pos[0], self.y - pos[1])
103
104 def get_walls(self):
105 return self.polygons.values()
106
107 def _draw_walls(self):
108 for index, polygon in self.polygons.items():
109 color = POLY_COLORS[index]
110 if len(polygon) > 1:
111 pointlist = [self.point_to_pygame(p) for p in polygon]
112 pygame.draw.lines(self._surface, color, False, pointlist, 2)
113
114 def get_background(self):
115 self._draw_background()
116 # Draw polygons
117 self._draw_walls()
118 return self._surface
119
120 def _draw_background(self, force=False):
121 if self._tile_image is None:
122 self._tile_image = resources.get_image(self.basetile)
123 if self._surface is not None and not force:
124 # We assume we don't change
125 return self._surface
126 self._surface = pygame.surface.Surface((self.x, self.y), pgl.SRCALPHA)
127 self._surface.fill(pygame.color.THECOLORS['black'])
128 x_step = self._tile_image.get_rect().width
129 y_step = self._tile_image.get_rect().height
130 x_count = self.x // x_step + 1
131 y_count = self.y / y_step + 1
132 for x in range(x_count):
133 for y in range(y_count):
134 tile_rect = pygame.rect.Rect(x * x_step, y * y_step,
135 x_step, y_step)
136 self._surface.blit(self._tile_image, tile_rect)
137 return self._surface
Note: See TracBrowser for help on using the repository browser.