Mercurial > nagslang
annotate nagslang/utils.py @ 596:8f53087c3745
Last aliens for crew quarters.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Sat, 07 Sep 2013 23:38:07 +0200 |
parents | ae8eb7c0f7bb |
children |
rev | line source |
---|---|
29 | 1 import pygame |
348
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
2 import pygame.locals as pgl |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
3 |
488
ae8eb7c0f7bb
Better wall and bulkhead drawing.
Jeremy Thurgood <firxen@gmail.com>
parents:
387
diff
changeset
|
4 import pymunk |
ae8eb7c0f7bb
Better wall and bulkhead drawing.
Jeremy Thurgood <firxen@gmail.com>
parents:
387
diff
changeset
|
5 import pymunk.pygame_util |
29 | 6 |
7 | |
8 def convert_colour(colour): | |
9 if isinstance(colour, pygame.Color): | |
10 return colour | |
11 if isinstance(colour, tuple): | |
12 return pygame.Color(*colour) | |
13 if isinstance(colour, basestring): | |
14 return pygame.Color(colour) | |
15 raise ValueError() | |
334 | 16 |
17 | |
18 def vec_from_angle(angle, length=1): | |
488
ae8eb7c0f7bb
Better wall and bulkhead drawing.
Jeremy Thurgood <firxen@gmail.com>
parents:
387
diff
changeset
|
19 vec = pymunk.Vec2d(length, 0) |
334 | 20 vec.angle = angle |
21 return vec | |
22 | |
23 | |
24 def vec_with_length(coords, length=1): | |
488
ae8eb7c0f7bb
Better wall and bulkhead drawing.
Jeremy Thurgood <firxen@gmail.com>
parents:
387
diff
changeset
|
25 vec = pymunk.Vec2d(coords) |
ae8eb7c0f7bb
Better wall and bulkhead drawing.
Jeremy Thurgood <firxen@gmail.com>
parents:
387
diff
changeset
|
26 # Don't crash if we've created a zero length vector |
343 | 27 if vec.length != 0: |
28 vec.length = length | |
334 | 29 return vec |
348
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
30 |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
31 |
488
ae8eb7c0f7bb
Better wall and bulkhead drawing.
Jeremy Thurgood <firxen@gmail.com>
parents:
387
diff
changeset
|
32 def points_to_lines(points): |
ae8eb7c0f7bb
Better wall and bulkhead drawing.
Jeremy Thurgood <firxen@gmail.com>
parents:
387
diff
changeset
|
33 if len(points) < 2: |
ae8eb7c0f7bb
Better wall and bulkhead drawing.
Jeremy Thurgood <firxen@gmail.com>
parents:
387
diff
changeset
|
34 return |
ae8eb7c0f7bb
Better wall and bulkhead drawing.
Jeremy Thurgood <firxen@gmail.com>
parents:
387
diff
changeset
|
35 last_point = points[0] |
ae8eb7c0f7bb
Better wall and bulkhead drawing.
Jeremy Thurgood <firxen@gmail.com>
parents:
387
diff
changeset
|
36 for point in points[1:]: |
ae8eb7c0f7bb
Better wall and bulkhead drawing.
Jeremy Thurgood <firxen@gmail.com>
parents:
387
diff
changeset
|
37 yield (last_point, point) |
ae8eb7c0f7bb
Better wall and bulkhead drawing.
Jeremy Thurgood <firxen@gmail.com>
parents:
387
diff
changeset
|
38 last_point = point |
ae8eb7c0f7bb
Better wall and bulkhead drawing.
Jeremy Thurgood <firxen@gmail.com>
parents:
387
diff
changeset
|
39 |
ae8eb7c0f7bb
Better wall and bulkhead drawing.
Jeremy Thurgood <firxen@gmail.com>
parents:
387
diff
changeset
|
40 |
ae8eb7c0f7bb
Better wall and bulkhead drawing.
Jeremy Thurgood <firxen@gmail.com>
parents:
387
diff
changeset
|
41 def extend_line(a, b, length): |
ae8eb7c0f7bb
Better wall and bulkhead drawing.
Jeremy Thurgood <firxen@gmail.com>
parents:
387
diff
changeset
|
42 offset = vec_from_angle((a - b).angle, abs(length)) |
ae8eb7c0f7bb
Better wall and bulkhead drawing.
Jeremy Thurgood <firxen@gmail.com>
parents:
387
diff
changeset
|
43 if length < 0: |
ae8eb7c0f7bb
Better wall and bulkhead drawing.
Jeremy Thurgood <firxen@gmail.com>
parents:
387
diff
changeset
|
44 offset = -offset |
ae8eb7c0f7bb
Better wall and bulkhead drawing.
Jeremy Thurgood <firxen@gmail.com>
parents:
387
diff
changeset
|
45 return (a + offset, b - offset) |
ae8eb7c0f7bb
Better wall and bulkhead drawing.
Jeremy Thurgood <firxen@gmail.com>
parents:
387
diff
changeset
|
46 |
ae8eb7c0f7bb
Better wall and bulkhead drawing.
Jeremy Thurgood <firxen@gmail.com>
parents:
387
diff
changeset
|
47 |
ae8eb7c0f7bb
Better wall and bulkhead drawing.
Jeremy Thurgood <firxen@gmail.com>
parents:
387
diff
changeset
|
48 def points_to_pygame(surface, points): |
ae8eb7c0f7bb
Better wall and bulkhead drawing.
Jeremy Thurgood <firxen@gmail.com>
parents:
387
diff
changeset
|
49 return [pymunk.pygame_util.to_pygame(p, surface) for p in points] |
ae8eb7c0f7bb
Better wall and bulkhead drawing.
Jeremy Thurgood <firxen@gmail.com>
parents:
387
diff
changeset
|
50 |
ae8eb7c0f7bb
Better wall and bulkhead drawing.
Jeremy Thurgood <firxen@gmail.com>
parents:
387
diff
changeset
|
51 |
387
ca89d566f9ef
Drop unused alpha bit from tile_surface helper
Neil Muller <drnlmuller@gmail.com>
parents:
362
diff
changeset
|
52 def tile_surface(size, tile_image): |
348
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
53 # create a surface, approriately tiled |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
54 surface = pygame.surface.Surface(size, pgl.SRCALPHA) |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
55 x_step = tile_image.get_rect().width |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
56 y_step = tile_image.get_rect().height |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
57 x_count = size[0] // x_step + 1 |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
58 y_count = size[1] / y_step + 1 |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
59 tile_rect = pygame.rect.Rect(0, 0, x_step, y_step) |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
60 for x in range(x_count): |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
61 tile_rect.x = x * x_step |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
62 for y in range(y_count): |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
63 tile_rect.y = y * y_step |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
64 surface.blit(tile_image, tile_rect) |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
65 return surface |