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
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
334
a3f1b2f0e3fb Physics-related cleanup.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
4 from pymunk.vec2d import Vec2d
29
58505d3482b6 Text on the menu screen
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
5
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 def convert_colour(colour):
58505d3482b6 Text on the menu screen
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
8 if isinstance(colour, pygame.Color):
58505d3482b6 Text on the menu screen
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
9 return colour
58505d3482b6 Text on the menu screen
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
10 if isinstance(colour, tuple):
58505d3482b6 Text on the menu screen
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
11 return pygame.Color(*colour)
58505d3482b6 Text on the menu screen
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
12 if isinstance(colour, basestring):
58505d3482b6 Text on the menu screen
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
13 return pygame.Color(colour)
58505d3482b6 Text on the menu screen
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
14 raise ValueError()
334
a3f1b2f0e3fb Physics-related cleanup.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
15
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 def vec_from_angle(angle, length=1):
a3f1b2f0e3fb Physics-related cleanup.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
18 vec = Vec2d(length, 0)
a3f1b2f0e3fb Physics-related cleanup.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
19 vec.angle = angle
a3f1b2f0e3fb Physics-related cleanup.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
20 return vec
a3f1b2f0e3fb Physics-related cleanup.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
21
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 def vec_with_length(coords, length=1):
a3f1b2f0e3fb Physics-related cleanup.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
24 vec = Vec2d(coords)
343
e5f525c87eb9 Robustness fix
Neil Muller <drnlmuller@gmail.com>
parents: 334
diff changeset
25 # Don't crash if we're created a zero length vector
e5f525c87eb9 Robustness fix
Neil Muller <drnlmuller@gmail.com>
parents: 334
diff changeset
26 if vec.length != 0:
e5f525c87eb9 Robustness fix
Neil Muller <drnlmuller@gmail.com>
parents: 334
diff changeset
27 vec.length = length
334
a3f1b2f0e3fb Physics-related cleanup.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
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
d0aeb893967d Transparent moonlight
Neil Muller <drnlmuller@gmail.com>
parents: 348
diff changeset
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
d0aeb893967d Transparent moonlight
Neil Muller <drnlmuller@gmail.com>
parents: 348
diff changeset
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