Mercurial > nagslang
annotate nagslang/level.py @ 321:0d7885e2f063
Add means for listing levels and areas.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Fri, 06 Sep 2013 02:03:16 +0200 |
parents | a5fe5a69689d |
children | 282113d86d75 |
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 |
145
0c49627920eb
Load game objects from level.
Jeremy Thurgood <firxen@gmail.com>
parents:
139
diff
changeset
|
4 from nagslang import game_object as go |
168 | 5 from nagslang import enemies |
201
3495a2025bc6
Break puzzlers out of game_object.py
Stefano Rivera <stefano@rivera.za.net>
parents:
197
diff
changeset
|
6 from nagslang import puzzle |
50
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
7 from nagslang.resources import resources |
139 | 8 from nagslang.yamlish import load, dump |
50
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
9 |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
10 POLY_COLORS = { |
53 | 11 1: pygame.color.THECOLORS['red'], |
12 2: pygame.color.THECOLORS['green'], | |
13 3: pygame.color.THECOLORS['yellow'], | |
14 4: pygame.color.THECOLORS['blue'], | |
15 5: pygame.color.THECOLORS['lightblue'], | |
16 6: pygame.color.THECOLORS['magenta'], | |
17 } | |
50
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 |
197 | 20 LINE_COLOR = pygame.color.THECOLORS['orange'] |
21 | |
22 | |
50
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
23 class Level(object): |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
24 |
277
56e42c00da25
Protagonist and enemies should see the world
Neil Muller <drnlmuller@gmail.com>
parents:
274
diff
changeset
|
25 def __init__(self, name, world): |
50
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
26 self.name = name |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
27 # defaults |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
28 self.x = 800 |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
29 self.y = 600 |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
30 self.polygons = {} |
197 | 31 self.lines = [] |
277
56e42c00da25
Protagonist and enemies should see the world
Neil Muller <drnlmuller@gmail.com>
parents:
274
diff
changeset
|
32 self.world = world |
50
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
33 self.basetile = 'tiles/floor.png' |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
34 self._tile_image = None |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
35 self._surface = None |
95
ecba9550ad8d
Fill the exterior with the blackness of space
Neil Muller <drnlmuller@gmail.com>
parents:
75
diff
changeset
|
36 self._exterior = False |
201
3495a2025bc6
Break puzzlers out of game_object.py
Stefano Rivera <stefano@rivera.za.net>
parents:
197
diff
changeset
|
37 self._glue = puzzle.PuzzleGlue() |
191 | 38 self.drawables = [] |
39 self.overlay_drawables = [] | |
164
06c681ff53aa
Round-tripping through load/save shouldn't discard objects
Neil Muller <drnlmuller@gmail.com>
parents:
145
diff
changeset
|
40 self._game_objects = [] |
168 | 41 self._enemies = [] |
50
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
42 |
145
0c49627920eb
Load game objects from level.
Jeremy Thurgood <firxen@gmail.com>
parents:
139
diff
changeset
|
43 def _get_data(self): |
0c49627920eb
Load game objects from level.
Jeremy Thurgood <firxen@gmail.com>
parents:
139
diff
changeset
|
44 # For overriding in tests. |
75
79748a884eb5
Put levels in a levels directory
Stefano Rivera <stefano@rivera.za.net>
parents:
72
diff
changeset
|
45 with resources.get_file('levels', self.name) as f: |
145
0c49627920eb
Load game objects from level.
Jeremy Thurgood <firxen@gmail.com>
parents:
139
diff
changeset
|
46 return load(f) |
0c49627920eb
Load game objects from level.
Jeremy Thurgood <firxen@gmail.com>
parents:
139
diff
changeset
|
47 |
164
06c681ff53aa
Round-tripping through load/save shouldn't discard objects
Neil Muller <drnlmuller@gmail.com>
parents:
145
diff
changeset
|
48 def _dump_data(self, f): |
06c681ff53aa
Round-tripping through load/save shouldn't discard objects
Neil Muller <drnlmuller@gmail.com>
parents:
145
diff
changeset
|
49 # For manipulation in tests. |
06c681ff53aa
Round-tripping through load/save shouldn't discard objects
Neil Muller <drnlmuller@gmail.com>
parents:
145
diff
changeset
|
50 dump({ |
06c681ff53aa
Round-tripping through load/save shouldn't discard objects
Neil Muller <drnlmuller@gmail.com>
parents:
145
diff
changeset
|
51 'size': [self.x, self.y], |
06c681ff53aa
Round-tripping through load/save shouldn't discard objects
Neil Muller <drnlmuller@gmail.com>
parents:
145
diff
changeset
|
52 'base_tile': self.basetile, |
06c681ff53aa
Round-tripping through load/save shouldn't discard objects
Neil Muller <drnlmuller@gmail.com>
parents:
145
diff
changeset
|
53 'polygons': self.polygons, |
197 | 54 'lines': self.lines, |
164
06c681ff53aa
Round-tripping through load/save shouldn't discard objects
Neil Muller <drnlmuller@gmail.com>
parents:
145
diff
changeset
|
55 'game_objects': self._game_objects, |
168 | 56 'enemies': self._enemies, |
164
06c681ff53aa
Round-tripping through load/save shouldn't discard objects
Neil Muller <drnlmuller@gmail.com>
parents:
145
diff
changeset
|
57 }, f) |
06c681ff53aa
Round-tripping through load/save shouldn't discard objects
Neil Muller <drnlmuller@gmail.com>
parents:
145
diff
changeset
|
58 |
321
0d7885e2f063
Add means for listing levels and areas.
Simon Cross <hodgestar@gmail.com>
parents:
317
diff
changeset
|
59 @classmethod |
0d7885e2f063
Add means for listing levels and areas.
Simon Cross <hodgestar@gmail.com>
parents:
317
diff
changeset
|
60 def list_levels(cls): |
0d7885e2f063
Add means for listing levels and areas.
Simon Cross <hodgestar@gmail.com>
parents:
317
diff
changeset
|
61 with resources.get_file('levels', 'LEVELS') as f: |
0d7885e2f063
Add means for listing levels and areas.
Simon Cross <hodgestar@gmail.com>
parents:
317
diff
changeset
|
62 levels = load(f) |
0d7885e2f063
Add means for listing levels and areas.
Simon Cross <hodgestar@gmail.com>
parents:
317
diff
changeset
|
63 return levels['levels'] |
0d7885e2f063
Add means for listing levels and areas.
Simon Cross <hodgestar@gmail.com>
parents:
317
diff
changeset
|
64 |
145
0c49627920eb
Load game objects from level.
Jeremy Thurgood <firxen@gmail.com>
parents:
139
diff
changeset
|
65 def load(self, space): |
0c49627920eb
Load game objects from level.
Jeremy Thurgood <firxen@gmail.com>
parents:
139
diff
changeset
|
66 data = self._get_data() |
139 | 67 self.x, self.y = data['size'] |
317
a5fe5a69689d
Actually use the base tile specified in the level
Stefano Rivera <stefano@rivera.za.net>
parents:
277
diff
changeset
|
68 self.basetile = data['base_tile'] |
139 | 69 for i, points in data['polygons'].iteritems(): |
70 self.polygons[i] = [] | |
71 for point in points: | |
72 self.polygons[i].append(tuple(point)) | |
197 | 73 self.lines = data.get('lines', []) |
164
06c681ff53aa
Round-tripping through load/save shouldn't discard objects
Neil Muller <drnlmuller@gmail.com>
parents:
145
diff
changeset
|
74 self._game_objects = data.get('game_objects', []) |
06c681ff53aa
Round-tripping through load/save shouldn't discard objects
Neil Muller <drnlmuller@gmail.com>
parents:
145
diff
changeset
|
75 for game_object_dict in self._game_objects: |
145
0c49627920eb
Load game objects from level.
Jeremy Thurgood <firxen@gmail.com>
parents:
139
diff
changeset
|
76 self._create_game_object(space, **game_object_dict) |
168 | 77 self._enemies = data.get('enemies', []) |
78 for enemy_dict in self._enemies: | |
79 self._create_enemy(space, **enemy_dict) | |
145
0c49627920eb
Load game objects from level.
Jeremy Thurgood <firxen@gmail.com>
parents:
139
diff
changeset
|
80 |
0c49627920eb
Load game objects from level.
Jeremy Thurgood <firxen@gmail.com>
parents:
139
diff
changeset
|
81 def _create_game_object(self, space, classname, args, name=None): |
209
ad1d3de210cd
Drop compatibility imports, and allow an optional module on classnames
Stefano Rivera <stefano@rivera.za.net>
parents:
201
diff
changeset
|
82 modules = { |
ad1d3de210cd
Drop compatibility imports, and allow an optional module on classnames
Stefano Rivera <stefano@rivera.za.net>
parents:
201
diff
changeset
|
83 'game_object': go, |
ad1d3de210cd
Drop compatibility imports, and allow an optional module on classnames
Stefano Rivera <stefano@rivera.za.net>
parents:
201
diff
changeset
|
84 'puzzle': puzzle, |
ad1d3de210cd
Drop compatibility imports, and allow an optional module on classnames
Stefano Rivera <stefano@rivera.za.net>
parents:
201
diff
changeset
|
85 } |
ad1d3de210cd
Drop compatibility imports, and allow an optional module on classnames
Stefano Rivera <stefano@rivera.za.net>
parents:
201
diff
changeset
|
86 if '.' in classname: |
ad1d3de210cd
Drop compatibility imports, and allow an optional module on classnames
Stefano Rivera <stefano@rivera.za.net>
parents:
201
diff
changeset
|
87 module, classname = classname.split('.') |
ad1d3de210cd
Drop compatibility imports, and allow an optional module on classnames
Stefano Rivera <stefano@rivera.za.net>
parents:
201
diff
changeset
|
88 else: |
ad1d3de210cd
Drop compatibility imports, and allow an optional module on classnames
Stefano Rivera <stefano@rivera.za.net>
parents:
201
diff
changeset
|
89 module = 'game_object' |
ad1d3de210cd
Drop compatibility imports, and allow an optional module on classnames
Stefano Rivera <stefano@rivera.za.net>
parents:
201
diff
changeset
|
90 cls = getattr(modules[module], classname) |
ad1d3de210cd
Drop compatibility imports, and allow an optional module on classnames
Stefano Rivera <stefano@rivera.za.net>
parents:
201
diff
changeset
|
91 |
201
3495a2025bc6
Break puzzlers out of game_object.py
Stefano Rivera <stefano@rivera.za.net>
parents:
197
diff
changeset
|
92 if issubclass(cls, puzzle.Puzzler): |
145
0c49627920eb
Load game objects from level.
Jeremy Thurgood <firxen@gmail.com>
parents:
139
diff
changeset
|
93 gobj = cls(*args) |
0c49627920eb
Load game objects from level.
Jeremy Thurgood <firxen@gmail.com>
parents:
139
diff
changeset
|
94 elif issubclass(cls, go.GameObject): |
0c49627920eb
Load game objects from level.
Jeremy Thurgood <firxen@gmail.com>
parents:
139
diff
changeset
|
95 gobj = cls(space, *args) |
191 | 96 self.drawables.append(gobj) |
97 if gobj.overlay: | |
98 self.overlay_drawables.append(gobj.overlay) | |
145
0c49627920eb
Load game objects from level.
Jeremy Thurgood <firxen@gmail.com>
parents:
139
diff
changeset
|
99 else: |
0c49627920eb
Load game objects from level.
Jeremy Thurgood <firxen@gmail.com>
parents:
139
diff
changeset
|
100 raise TypeError( |
0c49627920eb
Load game objects from level.
Jeremy Thurgood <firxen@gmail.com>
parents:
139
diff
changeset
|
101 "Expected a subclass of Puzzler or GameObject, got %s" % ( |
0c49627920eb
Load game objects from level.
Jeremy Thurgood <firxen@gmail.com>
parents:
139
diff
changeset
|
102 classname)) |
0c49627920eb
Load game objects from level.
Jeremy Thurgood <firxen@gmail.com>
parents:
139
diff
changeset
|
103 if name is not None: |
0c49627920eb
Load game objects from level.
Jeremy Thurgood <firxen@gmail.com>
parents:
139
diff
changeset
|
104 self._glue.add_component(name, gobj) |
274
988cf7c8b402
Add object returns for use in the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
209
diff
changeset
|
105 return gobj |
50
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
106 |
168 | 107 def _create_enemy(self, space, classname, args, name=None): |
108 cls = getattr(enemies, classname) | |
109 if issubclass(cls, go.GameObject): | |
277
56e42c00da25
Protagonist and enemies should see the world
Neil Muller <drnlmuller@gmail.com>
parents:
274
diff
changeset
|
110 gobj = cls(space, self.world, *args) |
191 | 111 self.drawables.append(gobj) |
168 | 112 else: |
113 raise TypeError( | |
114 "Expected a subclass of GameObject, got %s" % ( | |
115 classname)) | |
116 if name is not None: | |
117 self._glue.add_component(name, gobj) | |
274
988cf7c8b402
Add object returns for use in the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
209
diff
changeset
|
118 return gobj |
168 | 119 |
95
ecba9550ad8d
Fill the exterior with the blackness of space
Neil Muller <drnlmuller@gmail.com>
parents:
75
diff
changeset
|
120 def all_closed(self): |
ecba9550ad8d
Fill the exterior with the blackness of space
Neil Muller <drnlmuller@gmail.com>
parents:
75
diff
changeset
|
121 """Check if all the polygons are closed""" |
72 | 122 closed = True |
122
02423600d958
Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents:
118
diff
changeset
|
123 messages = [] |
02423600d958
Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents:
118
diff
changeset
|
124 for index, poly in self.polygons.items(): |
72 | 125 if len(poly) == 0: |
126 # We ignore empty polygons | |
127 continue | |
128 elif len(poly) == 1: | |
129 closed = False | |
122
02423600d958
Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents:
118
diff
changeset
|
130 messages.append("Error: polygon %s too small" % index) |
72 | 131 elif poly[-1] != poly[0]: |
132 closed = False | |
122
02423600d958
Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents:
118
diff
changeset
|
133 messages.append("Error: polygon %s not closed" % index) |
02423600d958
Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents:
118
diff
changeset
|
134 return closed, messages |
95
ecba9550ad8d
Fill the exterior with the blackness of space
Neil Muller <drnlmuller@gmail.com>
parents:
75
diff
changeset
|
135 |
ecba9550ad8d
Fill the exterior with the blackness of space
Neil Muller <drnlmuller@gmail.com>
parents:
75
diff
changeset
|
136 def save(self): |
122
02423600d958
Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents:
118
diff
changeset
|
137 closed, _ = self.all_closed() |
72 | 138 if not closed: |
122
02423600d958
Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents:
118
diff
changeset
|
139 return False |
75
79748a884eb5
Put levels in a levels directory
Stefano Rivera <stefano@rivera.za.net>
parents:
72
diff
changeset
|
140 with resources.get_file('levels', self.name, mode='w') as f: |
164
06c681ff53aa
Round-tripping through load/save shouldn't discard objects
Neil Muller <drnlmuller@gmail.com>
parents:
145
diff
changeset
|
141 self._dump_data(f) |
122
02423600d958
Use dialogs to report save results
Neil Muller <drnlmuller@gmail.com>
parents:
118
diff
changeset
|
142 return True |
72 | 143 |
50
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
144 def get_size(self): |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
145 return self.x, self.y |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
146 |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
147 def set_base_tile(self, new_tile): |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
148 self.basetile = new_tile |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
149 self._tile_image = None |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
150 |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
151 def point_to_pygame(self, pos): |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
152 # Convert a point from pymunk (which is what we store) |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
153 # to pygame for drawing |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
154 return (pos[0], self.y - pos[1]) |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
155 |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
156 def get_walls(self): |
197 | 157 walls = self.polygons.values() |
158 walls.extend(self.lines) | |
159 return walls | |
50
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
160 |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
161 def _draw_walls(self): |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
162 for index, polygon in self.polygons.items(): |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
163 color = POLY_COLORS[index] |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
164 if len(polygon) > 1: |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
165 pointlist = [self.point_to_pygame(p) for p in polygon] |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
166 pygame.draw.lines(self._surface, color, False, pointlist, 2) |
197 | 167 for line in self.lines: |
168 pointlist = [self.point_to_pygame(p) for p in line] | |
169 pygame.draw.lines(self._surface, LINE_COLOR, False, pointlist, 2) | |
50
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
170 |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
171 def get_background(self): |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
172 self._draw_background() |
95
ecba9550ad8d
Fill the exterior with the blackness of space
Neil Muller <drnlmuller@gmail.com>
parents:
75
diff
changeset
|
173 self._draw_exterior() |
50
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
174 # Draw polygons |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
175 self._draw_walls() |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
176 return self._surface |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
177 |
95
ecba9550ad8d
Fill the exterior with the blackness of space
Neil Muller <drnlmuller@gmail.com>
parents:
75
diff
changeset
|
178 def _draw_exterior(self, force=False): |
ecba9550ad8d
Fill the exterior with the blackness of space
Neil Muller <drnlmuller@gmail.com>
parents:
75
diff
changeset
|
179 """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
|
180 if self._exterior and not force: |
ecba9550ad8d
Fill the exterior with the blackness of space
Neil Muller <drnlmuller@gmail.com>
parents:
75
diff
changeset
|
181 return |
118
c02a99502a90
Tweak 'draw exterior' logic to handle surrounded polygons better
Neil Muller <drnlmuller@gmail.com>
parents:
98
diff
changeset
|
182 white = pygame.color.THECOLORS['white'] |
c02a99502a90
Tweak 'draw exterior' logic to handle surrounded polygons better
Neil Muller <drnlmuller@gmail.com>
parents:
98
diff
changeset
|
183 black = pygame.color.THECOLORS['black'] |
95
ecba9550ad8d
Fill the exterior with the blackness of space
Neil Muller <drnlmuller@gmail.com>
parents:
75
diff
changeset
|
184 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
|
185 surface.fill(black) |
95
ecba9550ad8d
Fill the exterior with the blackness of space
Neil Muller <drnlmuller@gmail.com>
parents:
75
diff
changeset
|
186 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
|
187 if len(polygon) > 1: |
ecba9550ad8d
Fill the exterior with the blackness of space
Neil Muller <drnlmuller@gmail.com>
parents:
75
diff
changeset
|
188 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
|
189 # filled polygons |
118
c02a99502a90
Tweak 'draw exterior' logic to handle surrounded polygons better
Neil Muller <drnlmuller@gmail.com>
parents:
98
diff
changeset
|
190 color = white |
c02a99502a90
Tweak 'draw exterior' logic to handle surrounded polygons better
Neil Muller <drnlmuller@gmail.com>
parents:
98
diff
changeset
|
191 # 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
|
192 # it is treated as negative |
c02a99502a90
Tweak 'draw exterior' logic to handle surrounded polygons better
Neil Muller <drnlmuller@gmail.com>
parents:
98
diff
changeset
|
193 # 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
|
194 # 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
|
195 # behaviour doesn't seem useful) |
c02a99502a90
Tweak 'draw exterior' logic to handle surrounded polygons better
Neil Muller <drnlmuller@gmail.com>
parents:
98
diff
changeset
|
196 # 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
|
197 # 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
|
198 surface.lock() |
c02a99502a90
Tweak 'draw exterior' logic to handle surrounded polygons better
Neil Muller <drnlmuller@gmail.com>
parents:
98
diff
changeset
|
199 for p in pointlist: |
c02a99502a90
Tweak 'draw exterior' logic to handle surrounded polygons better
Neil Muller <drnlmuller@gmail.com>
parents:
98
diff
changeset
|
200 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
|
201 color = black |
c02a99502a90
Tweak 'draw exterior' logic to handle surrounded polygons better
Neil Muller <drnlmuller@gmail.com>
parents:
98
diff
changeset
|
202 surface.unlock() |
c02a99502a90
Tweak 'draw exterior' logic to handle surrounded polygons better
Neil Muller <drnlmuller@gmail.com>
parents:
98
diff
changeset
|
203 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
|
204 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
|
205 self._exterior = True |
ecba9550ad8d
Fill the exterior with the blackness of space
Neil Muller <drnlmuller@gmail.com>
parents:
75
diff
changeset
|
206 |
50
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
207 def _draw_background(self, force=False): |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
208 if self._tile_image is None: |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
209 self._tile_image = resources.get_image(self.basetile) |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
210 if self._surface is not None and not force: |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
211 # We assume we don't change |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
212 return self._surface |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
213 self._surface = pygame.surface.Surface((self.x, self.y), pgl.SRCALPHA) |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
214 self._surface.fill(pygame.color.THECOLORS['black']) |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
215 x_step = self._tile_image.get_rect().width |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
216 y_step = self._tile_image.get_rect().height |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
217 x_count = self.x // x_step + 1 |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
218 y_count = self.y / y_step + 1 |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
219 for x in range(x_count): |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
220 for y in range(y_count): |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
221 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
|
222 x_step, y_step) |
50
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
223 self._surface.blit(self._tile_image, tile_rect) |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
224 return self._surface |