annotate nagslang/level.py @ 139:d1f543ff0805

YAML levels
author Stefano Rivera <stefano@rivera.za.net>
date Mon, 02 Sep 2013 18:27:58 +0200
parents 02423600d958
children 0c49627920eb
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
139
d1f543ff0805 YAML levels
Stefano Rivera <stefano@rivera.za.net>
parents: 122
diff changeset
5 from nagslang.yamlish import load, dump
50
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
6
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
7 POLY_COLORS = {
53
39d346467052 Draw all the walls.
Simon Cross <hodgestar@gmail.com>
parents: 50
diff changeset
8 1: pygame.color.THECOLORS['red'],
39d346467052 Draw all the walls.
Simon Cross <hodgestar@gmail.com>
parents: 50
diff changeset
9 2: pygame.color.THECOLORS['green'],
39d346467052 Draw all the walls.
Simon Cross <hodgestar@gmail.com>
parents: 50
diff changeset
10 3: pygame.color.THECOLORS['yellow'],
39d346467052 Draw all the walls.
Simon Cross <hodgestar@gmail.com>
parents: 50
diff changeset
11 4: pygame.color.THECOLORS['blue'],
39d346467052 Draw all the walls.
Simon Cross <hodgestar@gmail.com>
parents: 50
diff changeset
12 5: pygame.color.THECOLORS['lightblue'],
39d346467052 Draw all the walls.
Simon Cross <hodgestar@gmail.com>
parents: 50
diff changeset
13 6: pygame.color.THECOLORS['magenta'],
39d346467052 Draw all the walls.
Simon Cross <hodgestar@gmail.com>
parents: 50
diff changeset
14 }
50
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
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
17 class Level(object):
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
18
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
19 def __init__(self, name):
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
20 self.name = name
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
21 # defaults
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
22 self.x = 800
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
23 self.y = 600
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
24 self.polygons = {}
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
25 self.basetile = 'tiles/floor.png'
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
26 self._tile_image = None
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
27 self._surface = None
95
ecba9550ad8d Fill the exterior with the blackness of space
Neil Muller <drnlmuller@gmail.com>
parents: 75
diff changeset
28 self._exterior = False
50
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 load(self):
75
79748a884eb5 Put levels in a levels directory
Stefano Rivera <stefano@rivera.za.net>
parents: 72
diff changeset
31 with resources.get_file('levels', self.name) as f:
139
d1f543ff0805 YAML levels
Stefano Rivera <stefano@rivera.za.net>
parents: 122
diff changeset
32 data = load(f)
d1f543ff0805 YAML levels
Stefano Rivera <stefano@rivera.za.net>
parents: 122
diff changeset
33 self.x, self.y = data['size']
d1f543ff0805 YAML levels
Stefano Rivera <stefano@rivera.za.net>
parents: 122
diff changeset
34 self.base_tile = data['base_tile']
d1f543ff0805 YAML levels
Stefano Rivera <stefano@rivera.za.net>
parents: 122
diff changeset
35 for i, points in data['polygons'].iteritems():
d1f543ff0805 YAML levels
Stefano Rivera <stefano@rivera.za.net>
parents: 122
diff changeset
36 self.polygons[i] = []
d1f543ff0805 YAML levels
Stefano Rivera <stefano@rivera.za.net>
parents: 122
diff changeset
37 for point in points:
d1f543ff0805 YAML levels
Stefano Rivera <stefano@rivera.za.net>
parents: 122
diff changeset
38 self.polygons[i].append(tuple(point))
50
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
39
95
ecba9550ad8d Fill the exterior with the blackness of space
Neil Muller <drnlmuller@gmail.com>
parents: 75
diff changeset
40 def all_closed(self):
ecba9550ad8d Fill the exterior with the blackness of space
Neil Muller <drnlmuller@gmail.com>
parents: 75
diff changeset
41 """Check if all the polygons are closed"""
72
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
42 closed = True
122
02423600d958 Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents: 118
diff changeset
43 messages = []
02423600d958 Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents: 118
diff changeset
44 for index, poly in self.polygons.items():
72
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
45 if len(poly) == 0:
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
46 # We ignore empty polygons
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
47 continue
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
48 elif len(poly) == 1:
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
49 closed = False
122
02423600d958 Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents: 118
diff changeset
50 messages.append("Error: polygon %s too small" % index)
72
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
51 elif poly[-1] != poly[0]:
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
52 closed = False
122
02423600d958 Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents: 118
diff changeset
53 messages.append("Error: polygon %s not closed" % index)
02423600d958 Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents: 118
diff changeset
54 return closed, messages
95
ecba9550ad8d Fill the exterior with the blackness of space
Neil Muller <drnlmuller@gmail.com>
parents: 75
diff changeset
55
ecba9550ad8d Fill the exterior with the blackness of space
Neil Muller <drnlmuller@gmail.com>
parents: 75
diff changeset
56 def save(self):
122
02423600d958 Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents: 118
diff changeset
57 closed, _ = self.all_closed()
72
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
58 if not closed:
122
02423600d958 Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents: 118
diff changeset
59 return False
75
79748a884eb5 Put levels in a levels directory
Stefano Rivera <stefano@rivera.za.net>
parents: 72
diff changeset
60 with resources.get_file('levels', self.name, mode='w') as f:
139
d1f543ff0805 YAML levels
Stefano Rivera <stefano@rivera.za.net>
parents: 122
diff changeset
61 dump({
d1f543ff0805 YAML levels
Stefano Rivera <stefano@rivera.za.net>
parents: 122
diff changeset
62 'size': [self.x, self.y],
d1f543ff0805 YAML levels
Stefano Rivera <stefano@rivera.za.net>
parents: 122
diff changeset
63 'base_tile': self.basetile,
d1f543ff0805 YAML levels
Stefano Rivera <stefano@rivera.za.net>
parents: 122
diff changeset
64 'polygons': self.polygons,
d1f543ff0805 YAML levels
Stefano Rivera <stefano@rivera.za.net>
parents: 122
diff changeset
65 }, f)
122
02423600d958 Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents: 118
diff changeset
66 return True
72
5db052531510 Move save() to Level
Stefano Rivera <stefano@rivera.za.net>
parents: 54
diff changeset
67
50
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
68 def get_size(self):
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
69 return self.x, self.y
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
70
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
71 def set_base_tile(self, new_tile):
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
72 self.basetile = new_tile
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
73 self._tile_image = None
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
74
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
75 def point_to_pygame(self, pos):
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
76 # Convert a point from pymunk (which is what we store)
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
77 # to pygame for drawing
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
78 return (pos[0], self.y - pos[1])
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 get_walls(self):
53
39d346467052 Draw all the walls.
Simon Cross <hodgestar@gmail.com>
parents: 50
diff changeset
81 return self.polygons.values()
50
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
82
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
83 def _draw_walls(self):
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
84 for index, polygon in self.polygons.items():
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
85 color = POLY_COLORS[index]
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
86 if len(polygon) > 1:
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
87 pointlist = [self.point_to_pygame(p) for p in polygon]
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
88 pygame.draw.lines(self._surface, color, False, pointlist, 2)
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
89
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
90 def get_background(self):
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
91 self._draw_background()
95
ecba9550ad8d Fill the exterior with the blackness of space
Neil Muller <drnlmuller@gmail.com>
parents: 75
diff changeset
92 self._draw_exterior()
50
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
93 # Draw polygons
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
94 self._draw_walls()
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
95 return self._surface
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
96
95
ecba9550ad8d Fill the exterior with the blackness of space
Neil Muller <drnlmuller@gmail.com>
parents: 75
diff changeset
97 def _draw_exterior(self, force=False):
ecba9550ad8d Fill the exterior with the blackness of space
Neil Muller <drnlmuller@gmail.com>
parents: 75
diff changeset
98 """Fill the exterior of the level with black"""
ecba9550ad8d Fill the exterior with the blackness of space
Neil Muller <drnlmuller@gmail.com>
parents: 75
diff changeset
99 if self._exterior and not force:
ecba9550ad8d Fill the exterior with the blackness of space
Neil Muller <drnlmuller@gmail.com>
parents: 75
diff changeset
100 return
118
c02a99502a90 Tweak 'draw exterior' logic to handle surrounded polygons better
Neil Muller <drnlmuller@gmail.com>
parents: 98
diff changeset
101 white = pygame.color.THECOLORS['white']
c02a99502a90 Tweak 'draw exterior' logic to handle surrounded polygons better
Neil Muller <drnlmuller@gmail.com>
parents: 98
diff changeset
102 black = pygame.color.THECOLORS['black']
95
ecba9550ad8d Fill the exterior with the blackness of space
Neil Muller <drnlmuller@gmail.com>
parents: 75
diff changeset
103 surface = pygame.surface.Surface((self.x, self.y), pgl.SRCALPHA)
118
c02a99502a90 Tweak 'draw exterior' logic to handle surrounded polygons better
Neil Muller <drnlmuller@gmail.com>
parents: 98
diff changeset
104 surface.fill(black)
95
ecba9550ad8d Fill the exterior with the blackness of space
Neil Muller <drnlmuller@gmail.com>
parents: 75
diff changeset
105 for index, polygon in self.polygons.items():
ecba9550ad8d Fill the exterior with the blackness of space
Neil Muller <drnlmuller@gmail.com>
parents: 75
diff changeset
106 if len(polygon) > 1:
ecba9550ad8d Fill the exterior with the blackness of space
Neil Muller <drnlmuller@gmail.com>
parents: 75
diff changeset
107 pointlist = [self.point_to_pygame(p) for p in polygon]
ecba9550ad8d Fill the exterior with the blackness of space
Neil Muller <drnlmuller@gmail.com>
parents: 75
diff changeset
108 # filled polygons
118
c02a99502a90 Tweak 'draw exterior' logic to handle surrounded polygons better
Neil Muller <drnlmuller@gmail.com>
parents: 98
diff changeset
109 color = white
c02a99502a90 Tweak 'draw exterior' logic to handle surrounded polygons better
Neil Muller <drnlmuller@gmail.com>
parents: 98
diff changeset
110 # If a polygon overlaps on of the existing polygons,
c02a99502a90 Tweak 'draw exterior' logic to handle surrounded polygons better
Neil Muller <drnlmuller@gmail.com>
parents: 98
diff changeset
111 # it is treated as negative
c02a99502a90 Tweak 'draw exterior' logic to handle surrounded polygons better
Neil Muller <drnlmuller@gmail.com>
parents: 98
diff changeset
112 # This is not a complete inversion, since any overlap
c02a99502a90 Tweak 'draw exterior' logic to handle surrounded polygons better
Neil Muller <drnlmuller@gmail.com>
parents: 98
diff changeset
113 # triggers this (inversion is easy enough, but the
c02a99502a90 Tweak 'draw exterior' logic to handle surrounded polygons better
Neil Muller <drnlmuller@gmail.com>
parents: 98
diff changeset
114 # behaviour doesn't seem useful)
c02a99502a90 Tweak 'draw exterior' logic to handle surrounded polygons better
Neil Muller <drnlmuller@gmail.com>
parents: 98
diff changeset
115 # We also only check the vertexes - not breaking this
c02a99502a90 Tweak 'draw exterior' logic to handle surrounded polygons better
Neil Muller <drnlmuller@gmail.com>
parents: 98
diff changeset
116 # assumption is left to the level designers
c02a99502a90 Tweak 'draw exterior' logic to handle surrounded polygons better
Neil Muller <drnlmuller@gmail.com>
parents: 98
diff changeset
117 surface.lock()
c02a99502a90 Tweak 'draw exterior' logic to handle surrounded polygons better
Neil Muller <drnlmuller@gmail.com>
parents: 98
diff changeset
118 for p in pointlist:
c02a99502a90 Tweak 'draw exterior' logic to handle surrounded polygons better
Neil Muller <drnlmuller@gmail.com>
parents: 98
diff changeset
119 if surface.get_at(p) == white:
c02a99502a90 Tweak 'draw exterior' logic to handle surrounded polygons better
Neil Muller <drnlmuller@gmail.com>
parents: 98
diff changeset
120 color = black
c02a99502a90 Tweak 'draw exterior' logic to handle surrounded polygons better
Neil Muller <drnlmuller@gmail.com>
parents: 98
diff changeset
121 surface.unlock()
c02a99502a90 Tweak 'draw exterior' logic to handle surrounded polygons better
Neil Muller <drnlmuller@gmail.com>
parents: 98
diff changeset
122 pygame.draw.polygon(surface, color, pointlist, 0)
95
ecba9550ad8d Fill the exterior with the blackness of space
Neil Muller <drnlmuller@gmail.com>
parents: 75
diff changeset
123 self._surface.blit(surface, (0, 0), special_flags=pgl.BLEND_RGBA_MULT)
ecba9550ad8d Fill the exterior with the blackness of space
Neil Muller <drnlmuller@gmail.com>
parents: 75
diff changeset
124 self._exterior = True
ecba9550ad8d Fill the exterior with the blackness of space
Neil Muller <drnlmuller@gmail.com>
parents: 75
diff changeset
125
50
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
126 def _draw_background(self, force=False):
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
127 if self._tile_image is None:
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
128 self._tile_image = resources.get_image(self.basetile)
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
129 if self._surface is not None and not force:
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
130 # We assume we don't change
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
131 return self._surface
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
132 self._surface = pygame.surface.Surface((self.x, self.y), pgl.SRCALPHA)
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
133 self._surface.fill(pygame.color.THECOLORS['black'])
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
134 x_step = self._tile_image.get_rect().width
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
135 y_step = self._tile_image.get_rect().height
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
136 x_count = self.x // x_step + 1
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
137 y_count = self.y / y_step + 1
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
138 for x in range(x_count):
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
139 for y in range(y_count):
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
140 tile_rect = pygame.rect.Rect(x * x_step, y * y_step,
98
93256a0987a2 Fix newer pep8 continuation complaint
Neil Muller <drnlmuller@gmail.com>
parents: 95
diff changeset
141 x_step, y_step)
50
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
142 self._surface.blit(self._tile_image, tile_rect)
94d47bfcc7bb Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
143 return self._surface