annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
29
58505d3482b6 Text on the menu screen
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
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
58505d3482b6 Text on the menu screen
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
6
58505d3482b6 Text on the menu screen
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
7
58505d3482b6 Text on the menu screen
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
8 def convert_colour(colour):
58505d3482b6 Text on the menu screen
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
9 if isinstance(colour, pygame.Color):
58505d3482b6 Text on the menu screen
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
10 return colour
58505d3482b6 Text on the menu screen
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
11 if isinstance(colour, tuple):
58505d3482b6 Text on the menu screen
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
12 return pygame.Color(*colour)
58505d3482b6 Text on the menu screen
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
13 if isinstance(colour, basestring):
58505d3482b6 Text on the menu screen
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
14 return pygame.Color(colour)
58505d3482b6 Text on the menu screen
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
15 raise ValueError()
334
a3f1b2f0e3fb Physics-related cleanup.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
16
a3f1b2f0e3fb Physics-related cleanup.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
17
a3f1b2f0e3fb Physics-related cleanup.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
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
a3f1b2f0e3fb Physics-related cleanup.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
20 vec.angle = angle
a3f1b2f0e3fb Physics-related cleanup.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
21 return vec
a3f1b2f0e3fb Physics-related cleanup.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
22
a3f1b2f0e3fb Physics-related cleanup.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
23
a3f1b2f0e3fb Physics-related cleanup.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
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
e5f525c87eb9 Robustness fix
Neil Muller <drnlmuller@gmail.com>
parents: 334
diff changeset
27 if vec.length != 0:
e5f525c87eb9 Robustness fix
Neil Muller <drnlmuller@gmail.com>
parents: 334
diff changeset
28 vec.length = length
334
a3f1b2f0e3fb Physics-related cleanup.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
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