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