Mercurial > nagslang
comparison nagslang/level.py @ 488:ae8eb7c0f7bb
Better wall and bulkhead drawing.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Sat, 07 Sep 2013 17:40:59 +0200 |
parents | 69b8d6cbc692 |
children | 7d8c6e7ffd2b |
comparison
equal
deleted
inserted
replaced
487:b2dbdcfbf4bb | 488:ae8eb7c0f7bb |
---|---|
1 import os | 1 import os |
2 | 2 |
3 import pygame | 3 import pygame |
4 import pygame.locals as pgl | 4 import pygame.locals as pgl |
5 import pymunk | |
5 | 6 |
6 from nagslang import collectable | 7 from nagslang import collectable |
7 from nagslang import game_object as go | 8 from nagslang import game_object as go |
8 from nagslang import enemies | 9 from nagslang import enemies |
9 from nagslang import puzzle | 10 from nagslang import puzzle |
10 from nagslang.utils import tile_surface | 11 from nagslang.utils import ( |
12 tile_surface, points_to_pygame, extend_line, points_to_lines) | |
11 from nagslang.resources import resources | 13 from nagslang.resources import resources |
12 from nagslang.yamlish import load, dump | 14 from nagslang.yamlish import load, dump |
13 | 15 |
14 POLY_COLORS = { | 16 POLY_COLORS = { |
15 1: pygame.color.THECOLORS['red'], | 17 1: pygame.color.THECOLORS['red'], |
181 | 183 |
182 def set_base_tile(self, new_tile): | 184 def set_base_tile(self, new_tile): |
183 self.basetile = new_tile | 185 self.basetile = new_tile |
184 self._tile_image = None | 186 self._tile_image = None |
185 | 187 |
186 def point_to_pygame(self, pos): | |
187 # Convert a point from pymunk (which is what we store) | |
188 # to pygame for drawing | |
189 return (pos[0], self.y - pos[1]) | |
190 | |
191 def get_walls(self): | 188 def get_walls(self): |
192 walls = self.polygons.values() | 189 walls = self.polygons.values() |
193 walls.extend(self.lines) | 190 walls.extend(self.lines) |
194 return walls | 191 return walls |
195 | 192 |
193 def _draw_wall_line(self, points, width, colour, extend): | |
194 for line in points_to_lines(points): | |
195 if extend: | |
196 line = extend_line( | |
197 pymunk.Vec2d(line[0]), pymunk.Vec2d(line[1]), extend) | |
198 line = points_to_pygame(self._surface, line) | |
199 pygame.draw.line(self._surface, colour, line[0], line[1], width) | |
200 | |
201 def _draw_walls_lines(self, width, colour, extend): | |
202 for index, polygon in self.polygons.items(): | |
203 self._draw_wall_line(polygon, width, colour, extend) | |
204 for line in self.lines: | |
205 self._draw_wall_line(line, width, colour, extend) | |
206 | |
196 def _draw_walls(self): | 207 def _draw_walls(self): |
197 for index, polygon in self.polygons.items(): | 208 inner_colour = pygame.color.THECOLORS['red'] |
198 color = POLY_COLORS.get(index, pygame.color.THECOLORS['black']) | 209 mid_colour = pygame.color.THECOLORS['orange'] |
199 if len(polygon) > 1: | 210 outer_colour = pygame.color.THECOLORS['yellow'] |
200 pointlist = [self.point_to_pygame(p) for p in polygon] | 211 self._draw_walls_lines(5, outer_colour, 0) |
201 pygame.draw.lines(self._surface, color, False, pointlist, 2) | 212 self._draw_walls_lines(3, outer_colour, 1) |
202 for line in self.lines: | 213 self._draw_walls_lines(3, mid_colour, 0) |
203 pointlist = [self.point_to_pygame(p) for p in line] | 214 self._draw_walls_lines(1, inner_colour, 0) |
204 pygame.draw.lines(self._surface, LINE_COLOR, False, pointlist, 2) | |
205 | 215 |
206 def get_background(self): | 216 def get_background(self): |
207 if self._surface is None: | 217 if self._surface is None: |
208 self._draw_background() | 218 self._draw_background() |
209 self._draw_exterior() | 219 self._draw_exterior() |
219 black = pygame.color.THECOLORS['black'] | 229 black = pygame.color.THECOLORS['black'] |
220 surface = pygame.surface.Surface((self.x, self.y), pgl.SRCALPHA) | 230 surface = pygame.surface.Surface((self.x, self.y), pgl.SRCALPHA) |
221 surface.fill(black) | 231 surface.fill(black) |
222 for index, polygon in self.polygons.items(): | 232 for index, polygon in self.polygons.items(): |
223 if len(polygon) > 1: | 233 if len(polygon) > 1: |
224 pointlist = [self.point_to_pygame(p) for p in polygon] | 234 pointlist = points_to_pygame(self._surface, polygon) |
225 # filled polygons | 235 # filled polygons |
226 color = white | 236 color = white |
227 # If a polygon overlaps on of the existing polygons, | 237 # If a polygon overlaps on of the existing polygons, |
228 # it is treated as negative | 238 # it is treated as negative |
229 # This is not a complete inversion, since any overlap | 239 # This is not a complete inversion, since any overlap |