annotate nagslang/level.py @ 75:79748a884eb5

Put levels in a levels directory
author Stefano Rivera <stefano@rivera.za.net>
date Sun, 01 Sep 2013 21:44:45 +0200
parents 5db052531510
children ecba9550ad8d
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
75
79748a884eb5 Put levels in a levels directory
Stefano Rivera <stefano@rivera.za.net>
parents: 72
diff changeset
40 with resources.get_file('levels', 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
72
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
65 def save(self):
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
66 closed = True
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
67 for poly in self.polygons.values():
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
68 if len(poly) == 0:
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
69 # We ignore empty polygons
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
70 continue
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
71 elif len(poly) == 1:
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
72 closed = False
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
73 print "\033[31mError: polygon too small\033[0m"
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
74 elif poly[-1] != poly[0]:
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
75 closed = False
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
76 print "\033[31mError: polygon not closed\033[0m"
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
77 if not closed:
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
78 print 'Not saving the level'
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
79 return
75
79748a884eb5 Put levels in a levels directory
Stefano Rivera <stefano@rivera.za.net>
parents: 72
diff changeset
80 with resources.get_file('levels', self.name, mode='w') as f:
72
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
81 f.write('X-Size: %s\n' % self.x)
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
82 f.write('Y-Size: %s\n' % self.y)
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
83 f.write('Base tile: %s\n' % self.basetile)
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
84 for i, poly in self.polygons.items():
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
85 if len(poly) == 0:
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
86 continue
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
87 f.write('Polygon %d : %d\n' % (i, len(poly)))
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
88 for point in poly:
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
89 f.write('Point: %d %d\n' % point)
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
90 print 'level %s saved' % self.name
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
91
50
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
92 def get_size(self):
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
93 return self.x, self.y
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
94
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
95 def set_base_tile(self, new_tile):
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
96 self.basetile = new_tile
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
97 self._tile_image = None
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
98
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
99 def point_to_pygame(self, pos):
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
100 # Convert a point from pymunk (which is what we store)
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
101 # to pygame for drawing
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
102 return (pos[0], self.y - pos[1])
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
103
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
104 def get_walls(self):
53
39d346467052 Draw all the walls.
Simon Cross <hodgestar@gmail.com>
parents: 50
diff changeset
105 return self.polygons.values()
50
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
106
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
107 def _draw_walls(self):
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
108 for index, polygon in self.polygons.items():
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
109 color = POLY_COLORS[index]
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
110 if len(polygon) > 1:
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
111 pointlist = [self.point_to_pygame(p) for p in polygon]
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
112 pygame.draw.lines(self._surface, color, False, pointlist, 2)
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
113
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
114 def get_background(self):
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
115 self._draw_background()
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
116 # Draw polygons
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
117 self._draw_walls()
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
118 return self._surface
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
119
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
120 def _draw_background(self, force=False):
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
121 if self._tile_image is None:
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
122 self._tile_image = resources.get_image(self.basetile)
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
123 if self._surface is not None and not force:
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
124 # We assume we don't change
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
125 return self._surface
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
126 self._surface = pygame.surface.Surface((self.x, self.y), pgl.SRCALPHA)
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
127 self._surface.fill(pygame.color.THECOLORS['black'])
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
128 x_step = self._tile_image.get_rect().width
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
129 y_step = self._tile_image.get_rect().height
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
130 x_count = self.x // x_step + 1
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
131 y_count = self.y / y_step + 1
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
132 for x in range(x_count):
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
133 for y in range(y_count):
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
134 tile_rect = pygame.rect.Rect(x * x_step, y * y_step,
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
135 x_step, y_step)
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
136 self._surface.blit(self._tile_image, tile_rect)
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
137 return self._surface