Mercurial > nagslang
comparison nagslang/utils.py @ 488:ae8eb7c0f7bb
Better wall and bulkhead drawing.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Sat, 07 Sep 2013 17:40:59 +0200 |
parents | ca89d566f9ef |
children |
comparison
equal
deleted
inserted
replaced
487:b2dbdcfbf4bb | 488:ae8eb7c0f7bb |
---|---|
1 import pygame | 1 import pygame |
2 import pygame.locals as pgl | 2 import pygame.locals as pgl |
3 | 3 |
4 from pymunk.vec2d import Vec2d | 4 import pymunk |
5 import pymunk.pygame_util | |
5 | 6 |
6 | 7 |
7 def convert_colour(colour): | 8 def convert_colour(colour): |
8 if isinstance(colour, pygame.Color): | 9 if isinstance(colour, pygame.Color): |
9 return colour | 10 return colour |
13 return pygame.Color(colour) | 14 return pygame.Color(colour) |
14 raise ValueError() | 15 raise ValueError() |
15 | 16 |
16 | 17 |
17 def vec_from_angle(angle, length=1): | 18 def vec_from_angle(angle, length=1): |
18 vec = Vec2d(length, 0) | 19 vec = pymunk.Vec2d(length, 0) |
19 vec.angle = angle | 20 vec.angle = angle |
20 return vec | 21 return vec |
21 | 22 |
22 | 23 |
23 def vec_with_length(coords, length=1): | 24 def vec_with_length(coords, length=1): |
24 vec = Vec2d(coords) | 25 vec = pymunk.Vec2d(coords) |
25 # Don't crash if we're created a zero length vector | 26 # Don't crash if we've created a zero length vector |
26 if vec.length != 0: | 27 if vec.length != 0: |
27 vec.length = length | 28 vec.length = length |
28 return vec | 29 return vec |
30 | |
31 | |
32 def 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 | |
41 def 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 | |
48 def points_to_pygame(surface, points): | |
49 return [pymunk.pygame_util.to_pygame(p, surface) for p in points] | |
29 | 50 |
30 | 51 |
31 def tile_surface(size, tile_image): | 52 def tile_surface(size, tile_image): |
32 # create a surface, approriately tiled | 53 # create a surface, approriately tiled |
33 surface = pygame.surface.Surface(size, pgl.SRCALPHA) | 54 surface = pygame.surface.Surface(size, pgl.SRCALPHA) |