source: nagslang/utils.py@ 348:f0e8970ab804

Last change on this file since 348:f0e8970ab804 was 348:f0e8970ab804, checked in by Neil Muller <drnlmuller@…>, 8 years ago

Split out tiling into utility function

File size: 1.2 KB
Line 
1import pygame
2import pygame.locals as pgl
3
4from pymunk.vec2d import Vec2d
5
6
7def 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()
15
16
17def vec_from_angle(angle, length=1):
18 vec = Vec2d(length, 0)
19 vec.angle = angle
20 return vec
21
22
23def vec_with_length(coords, length=1):
24 vec = Vec2d(coords)
25 # Don't crash if we're created a zero length vector
26 if vec.length != 0:
27 vec.length = length
28 return vec
29
30
31def tile_surface(size, tile_image):
32 # create a surface, approriately tiled
33 surface = pygame.surface.Surface(size, pgl.SRCALPHA)
34 surface.fill(pygame.color.THECOLORS['black'])
35 x_step = tile_image.get_rect().width
36 y_step = tile_image.get_rect().height
37 x_count = size[0] // x_step + 1
38 y_count = size[1] / y_step + 1
39 tile_rect = pygame.rect.Rect(0, 0, x_step, y_step)
40 for x in range(x_count):
41 tile_rect.x = x * x_step
42 for y in range(y_count):
43 tile_rect.y = y * y_step
44 surface.blit(tile_image, tile_rect)
45 return surface
Note: See TracBrowser for help on using the repository browser.