view 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
line wrap: on
line source

import pygame
import pygame.locals as pgl

from pymunk.vec2d import Vec2d


def convert_colour(colour):
    if isinstance(colour, pygame.Color):
        return colour
    if isinstance(colour, tuple):
        return pygame.Color(*colour)
    if isinstance(colour, basestring):
        return pygame.Color(colour)
    raise ValueError()


def vec_from_angle(angle, length=1):
    vec = Vec2d(length, 0)
    vec.angle = angle
    return vec


def vec_with_length(coords, length=1):
    vec = Vec2d(coords)
    # Don't crash if we're created a zero length vector
    if vec.length != 0:
        vec.length = length
    return vec


def tile_surface(size, tile_image, alpha=255):
    # create a surface, approriately tiled
    surface = pygame.surface.Surface(size, pgl.SRCALPHA)
    surface.fill(pygame.color.Color(0, 0, 0, alpha))
    x_step = tile_image.get_rect().width
    y_step = tile_image.get_rect().height
    x_count = size[0] // x_step + 1
    y_count = size[1] / y_step + 1
    tile_rect = pygame.rect.Rect(0, 0, x_step, y_step)
    for x in range(x_count):
        tile_rect.x = x * x_step
        for y in range(y_count):
            tile_rect.y = y * y_step
            surface.blit(tile_image, tile_rect)
    return surface