Mercurial > nagslang
annotate nagslang/utils.py @ 362:d0aeb893967d
Transparent moonlight
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Fri, 06 Sep 2013 20:12:07 +0200 |
parents | f0e8970ab804 |
children | ca89d566f9ef |
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 |
334 | 4 from pymunk.vec2d import Vec2d |
29 | 5 |
6 | |
7 def convert_colour(colour): | |
8 if isinstance(colour, pygame.Color): | |
9 return colour | |
10 if isinstance(colour, tuple): | |
11 return pygame.Color(*colour) | |
12 if isinstance(colour, basestring): | |
13 return pygame.Color(colour) | |
14 raise ValueError() | |
334 | 15 |
16 | |
17 def vec_from_angle(angle, length=1): | |
18 vec = Vec2d(length, 0) | |
19 vec.angle = angle | |
20 return vec | |
21 | |
22 | |
23 def vec_with_length(coords, length=1): | |
24 vec = Vec2d(coords) | |
343 | 25 # Don't crash if we're created a zero length vector |
26 if vec.length != 0: | |
27 vec.length = length | |
334 | 28 return vec |
348
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
29 |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
30 |
362 | 31 def tile_surface(size, tile_image, alpha=255): |
348
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
32 # create a surface, approriately tiled |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
33 surface = pygame.surface.Surface(size, pgl.SRCALPHA) |
362 | 34 surface.fill(pygame.color.Color(0, 0, 0, alpha)) |
348
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
35 x_step = tile_image.get_rect().width |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
36 y_step = tile_image.get_rect().height |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
37 x_count = size[0] // x_step + 1 |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
38 y_count = size[1] / y_step + 1 |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
39 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
|
40 for x in range(x_count): |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
41 tile_rect.x = x * x_step |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
42 for y in range(y_count): |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
43 tile_rect.y = y * y_step |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
44 surface.blit(tile_image, tile_rect) |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
45 return surface |