annotate nagslang/level.py @ 54:2c1b85b6f457

Add .get_file() to resources.
author Simon Cross <hodgestar@gmail.com>
date Sun, 01 Sep 2013 18:46:05 +0200
parents 39d346467052
children 5db052531510
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 = {
53
39d346467052 Draw all the walls.
Simon Cross <hodgestar@gmail.com>
parents: 50
diff changeset
7 1: pygame.color.THECOLORS['red'],
39d346467052 Draw all the walls.
Simon Cross <hodgestar@gmail.com>
parents: 50
diff changeset
8 2: pygame.color.THECOLORS['green'],
39d346467052 Draw all the walls.
Simon Cross <hodgestar@gmail.com>
parents: 50
diff changeset
9 3: pygame.color.THECOLORS['yellow'],
39d346467052 Draw all the walls.
Simon Cross <hodgestar@gmail.com>
parents: 50
diff changeset
10 4: pygame.color.THECOLORS['blue'],
39d346467052 Draw all the walls.
Simon Cross <hodgestar@gmail.com>
parents: 50
diff changeset
11 5: pygame.color.THECOLORS['lightblue'],
39d346467052 Draw all the walls.
Simon Cross <hodgestar@gmail.com>
parents: 50
diff changeset
12 6: pygame.color.THECOLORS['magenta'],
39d346467052 Draw all the walls.
Simon Cross <hodgestar@gmail.com>
parents: 50
diff changeset
13 }
50
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 inpoly = False
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
37 polygon = []
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
38 index = 0
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
39 num_points = 0
54
2c1b85b6f457 Add .get_file() to resources.
Simon Cross <hodgestar@gmail.com>
parents: 53
diff changeset
40 with resources.get_file(self.name) as f:
50
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
41 for line in f:
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
42 if inpoly:
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
43 if not line.startswith('Point:'):
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
44 add_polygon(polygon, index, num_points)
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
45 polygon = []
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
46 inpoly = False
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
47 index = 0
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
48 else:
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
49 point = line.split(':', 1)[1]
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
50 x, y = [int(i) for i in point.split()]
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
51 polygon.append((x, y))
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
52 if line.startswith('X-Size:'):
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
53 self.x = int(line.split(':', 1)[1])
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
54 elif line.startswith('Y-Size:'):
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
55 self.y = int(line.split(':', 1)[1])
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
56 elif line.startswith('Base tile:'):
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
57 self.basetile = line.split(':', 1)[1].strip()
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
58 elif line.startswith('Polygon'):
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
59 rest = line.split(' ', 1)[1]
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
60 index, num_points = [int(x) for x in rest.split(':', 1)]
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
61 inpoly = True
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
62 if index:
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
63 add_polygon(polygon, index, num_points)
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
64
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
65 def get_size(self):
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
66 return self.x, self.y
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
67
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
68 def set_base_tile(self, new_tile):
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
69 self.basetile = new_tile
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
70 self._tile_image = None
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
71
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
72 def point_to_pygame(self, pos):
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
73 # Convert a point from pymunk (which is what we store)
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
74 # to pygame for drawing
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
75 return (pos[0], self.y - pos[1])
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
76
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
77 def get_walls(self):
53
39d346467052 Draw all the walls.
Simon Cross <hodgestar@gmail.com>
parents: 50
diff changeset
78 return self.polygons.values()
50
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
79
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
80 def _draw_walls(self):
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
81 for index, polygon in self.polygons.items():
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
82 color = POLY_COLORS[index]
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
83 if len(polygon) > 1:
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
84 pointlist = [self.point_to_pygame(p) for p in polygon]
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
85 pygame.draw.lines(self._surface, color, False, pointlist, 2)
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
86
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
87 def get_background(self):
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
88 self._draw_background()
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
89 # Draw polygons
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
90 self._draw_walls()
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
91 return self._surface
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
92
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
93 def _draw_background(self, force=False):
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
94 if self._tile_image is None:
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
95 self._tile_image = resources.get_image(self.basetile)
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
96 if self._surface is not None and not force:
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
97 # We assume we don't change
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
98 return self._surface
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
99 self._surface = pygame.surface.Surface((self.x, self.y), pgl.SRCALPHA)
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
100 self._surface.fill(pygame.color.THECOLORS['black'])
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
101 x_step = self._tile_image.get_rect().width
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
102 y_step = self._tile_image.get_rect().height
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
103 x_count = self.x // x_step + 1
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
104 y_count = self.y / y_step + 1
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
105 for x in range(x_count):
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
106 for y in range(y_count):
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
107 tile_rect = pygame.rect.Rect(x * x_step, y * y_step,
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
108 x_step, y_step)
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
109 self._surface.blit(self._tile_image, tile_rect)
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
110 return self._surface