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