source: nagslang/utils.py @ 387:ca89d566f9ef

Last change on this file since 387:ca89d566f9ef was 387:ca89d566f9ef, checked in by Neil Muller <drnlmuller@…>, 7 years ago

Drop unused alpha bit from tile_surface helper

File size: 1.1 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    x_step = tile_image.get_rect().width
35    y_step = tile_image.get_rect().height
36    x_count = size[0] // x_step + 1
37    y_count = size[1] / y_step + 1
38    tile_rect = pygame.rect.Rect(0, 0, x_step, y_step)
39    for x in range(x_count):
40        tile_rect.x = x * x_step
41        for y in range(y_count):
42            tile_rect.y = y * y_step
43            surface.blit(tile_image, tile_rect)
44    return surface
Note: See TracBrowser for help on using the repository browser.