Changeset 488:ae8eb7c0f7bb for nagslang


Ignore:
Timestamp:
Sep 7, 2013, 3:40:59 PM (7 years ago)
Author:
Jeremy Thurgood <firxen@…>
Branch:
default
Message:

Better wall and bulkhead drawing.

Location:
nagslang
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • nagslang/game_object.py

    r477 r488  
    456456    def __init__(self, space, end1, end2, key_state=None):
    457457        body = make_body(None, None, (0, 0))
    458         self.shape = pymunk.Segment(body, tuple(end1), tuple(end2), 3)
     458        self.shape = pymunk.Segment(body, tuple(end1), tuple(end2), 7)
    459459        self.shape.collision_type = COLLISION_TYPE_DOOR
    460460        if key_state is None:
  • nagslang/level.py

    r485 r488  
    33import pygame
    44import pygame.locals as pgl
     5import pymunk
    56
    67from nagslang import collectable
     
    89from nagslang import enemies
    910from nagslang import puzzle
    10 from nagslang.utils import tile_surface
     11from nagslang.utils import (
     12    tile_surface, points_to_pygame, extend_line, points_to_lines)
    1113from nagslang.resources import resources
    1214from nagslang.yamlish import load, dump
     
    184186        self._tile_image = None
    185187
    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 
    191188    def get_walls(self):
    192189        walls = self.polygons.values()
     
    194191        return walls
    195192
     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
    196207    def _draw_walls(self):
    197         for index, polygon in self.polygons.items():
    198             color = POLY_COLORS.get(index, pygame.color.THECOLORS['black'])
    199             if len(polygon) > 1:
    200                 pointlist = [self.point_to_pygame(p) for p in polygon]
    201                 pygame.draw.lines(self._surface, color, False, pointlist, 2)
    202         for line in self.lines:
    203             pointlist = [self.point_to_pygame(p) for p in line]
    204             pygame.draw.lines(self._surface, LINE_COLOR, False, pointlist, 2)
     208        inner_colour = pygame.color.THECOLORS['red']
     209        mid_colour = pygame.color.THECOLORS['orange']
     210        outer_colour = pygame.color.THECOLORS['yellow']
     211        self._draw_walls_lines(5, outer_colour, 0)
     212        self._draw_walls_lines(3, outer_colour, 1)
     213        self._draw_walls_lines(3, mid_colour, 0)
     214        self._draw_walls_lines(1, inner_colour, 0)
    205215
    206216    def get_background(self):
     
    222232        for index, polygon in self.polygons.items():
    223233            if len(polygon) > 1:
    224                 pointlist = [self.point_to_pygame(p) for p in polygon]
     234                pointlist = points_to_pygame(self._surface, polygon)
    225235                # filled polygons
    226236                color = white
  • nagslang/render.py

    r474 r488  
    77
    88from nagslang.options import options
    9 from nagslang.utils import tile_surface, vec_from_angle
     9from nagslang.utils import (
     10    tile_surface, vec_from_angle, points_to_pygame, extend_line)
    1011from nagslang.widgets.text import LabelWidget, MultiLineWidget
    1112
     
    4849
    4950class BulkheadRenderer(Renderer):
    50     def _offset_point(self, vec, to_vec, length):
    51         offset = vec_from_angle((to_vec - vec).angle, abs(length))
    52         if length < 0:
    53             offset = -offset
    54         return vec + offset
    55 
    56     def _to_pygame(self, surface, *points):
    57         return [pymunk.pygame_util.to_pygame(p, surface) for p in points]
    58 
    5951    def draw_bulkhead_line(self, surface, a, b):
    60         ai = self._offset_point(a, b, 2)
    61         bi = self._offset_point(b, a, 2)
    62         a, b, ai, bi = self._to_pygame(surface, a, b, ai, bi)
     52        ai, bi = extend_line(a, b, -2)
     53        a, b, ai, bi = points_to_pygame(surface, (a, b, ai, bi))
    6354        pygame.draw.line(
    64             surface, pygame.color.THECOLORS['black'], a, b, 8)
     55            surface, pygame.color.THECOLORS['black'], a, b, 7)
    6556        pygame.draw.line(
    66             surface, pygame.color.THECOLORS['lightblue'], ai, bi, 6)
     57            surface, pygame.color.THECOLORS['lightblue'], ai, bi, 5)
    6758
    6859    def render(self, surface):
    6960        shape = self.game_object.get_shape()
    7061        if self.game_object.puzzler.get_state():
    71             ai = self._offset_point(shape.a, shape.b, 10)
    72             bi = self._offset_point(shape.b, shape.a, 10)
     62            offset = vec_from_angle((shape.b - shape.a).angle, 10)
     63            ai = shape.a + offset
     64            bi = shape.b - offset
    7365            self.draw_bulkhead_line(surface, shape.a, ai)
    7466            self.draw_bulkhead_line(surface, bi, shape.b)
  • nagslang/utils.py

    r387 r488  
    22import pygame.locals as pgl
    33
    4 from pymunk.vec2d import Vec2d
     4import pymunk
     5import pymunk.pygame_util
    56
    67
     
    1617
    1718def vec_from_angle(angle, length=1):
    18     vec = Vec2d(length, 0)
     19    vec = pymunk.Vec2d(length, 0)
    1920    vec.angle = angle
    2021    return vec
     
    2223
    2324def vec_with_length(coords, length=1):
    24     vec = Vec2d(coords)
    25     # Don't crash if we're created a zero length vector
     25    vec = pymunk.Vec2d(coords)
     26    # Don't crash if we've created a zero length vector
    2627    if vec.length != 0:
    2728        vec.length = length
    2829    return vec
     30
     31
     32def points_to_lines(points):
     33    if len(points) < 2:
     34        return
     35    last_point = points[0]
     36    for point in points[1:]:
     37        yield (last_point, point)
     38        last_point = point
     39
     40
     41def extend_line(a, b, length):
     42    offset = vec_from_angle((a - b).angle, abs(length))
     43    if length < 0:
     44        offset = -offset
     45    return (a + offset, b - offset)
     46
     47
     48def points_to_pygame(surface, points):
     49    return [pymunk.pygame_util.to_pygame(p, surface) for p in points]
    2950
    3051
Note: See TracChangeset for help on using the changeset viewer.