Mercurial > nagslang
annotate nagslang/utils.py @ 398:e36e6101b766
Restarting the level restores health
author | Stefano Rivera <stefano@rivera.za.net> |
---|---|
date | Sat, 07 Sep 2013 01:12:21 +0200 |
parents | ca89d566f9ef |
children | ae8eb7c0f7bb |
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 |
387
ca89d566f9ef
Drop unused alpha bit from tile_surface helper
Neil Muller <drnlmuller@gmail.com>
parents:
362
diff
changeset
|
31 def tile_surface(size, tile_image): |
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) |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
34 x_step = tile_image.get_rect().width |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
35 y_step = tile_image.get_rect().height |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
36 x_count = size[0] // x_step + 1 |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
37 y_count = size[1] / y_step + 1 |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
38 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
|
39 for x in range(x_count): |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
40 tile_rect.x = x * x_step |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
41 for y in range(y_count): |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
42 tile_rect.y = y * y_step |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
43 surface.blit(tile_image, tile_rect) |
f0e8970ab804
Split out tiling into utility function
Neil Muller <drnlmuller@gmail.com>
parents:
343
diff
changeset
|
44 return surface |