view nagslang/utils.py @ 348:f0e8970ab804

Split out tiling into utility function
author Neil Muller <drnlmuller@gmail.com>
date Fri, 06 Sep 2013 15:18:40 +0200
parents e5f525c87eb9
children d0aeb893967d
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):
    # create a surface, approriately tiled
    surface = pygame.surface.Surface(size, pgl.SRCALPHA)
    surface.fill(pygame.color.THECOLORS['black'])
    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