source: nagslang/utils.py@ 698:020e73192fe2

Last change on this file since 698:020e73192fe2 was 488:ae8eb7c0f7bb, checked in by Jeremy Thurgood <firxen@…>, 8 years ago

Better wall and bulkhead drawing.

File size: 1.6 KB
Line 
1import pygame
2import pygame.locals as pgl
3
4import pymunk
5import pymunk.pygame_util
6
7
8def 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()
16
17
18def vec_from_angle(angle, length=1):
19 vec = pymunk.Vec2d(length, 0)
20 vec.angle = angle
21 return vec
22
23
24def vec_with_length(coords, length=1):
25 vec = pymunk.Vec2d(coords)
26 # Don't crash if we've created a zero length vector
27 if vec.length != 0:
28 vec.length = length
29 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]
50
51
52def tile_surface(size, tile_image):
53 # create a surface, approriately tiled
54 surface = pygame.surface.Surface(size, pgl.SRCALPHA)
55 x_step = tile_image.get_rect().width
56 y_step = tile_image.get_rect().height
57 x_count = size[0] // x_step + 1
58 y_count = size[1] / y_step + 1
59 tile_rect = pygame.rect.Rect(0, 0, x_step, y_step)
60 for x in range(x_count):
61 tile_rect.x = x * x_step
62 for y in range(y_count):
63 tile_rect.y = y * y_step
64 surface.blit(tile_image, tile_rect)
65 return surface
Note: See TracBrowser for help on using the repository browser.