平铺图像与8位图mask做alpha透明度示例

"""
    平铺图像与8位图mask做alpha透明度示例
"""

__author__ = "李兴球"
__date__ = "2019/8/30"

import random
import pygame

def tile_texture(texture, size):
    """texture:图像,作为纹理,size:尺寸,要比texture大"""
    result = pygame.Surface(size, depth=32)
    for x in range(0, size[0], texture.get_width()):
        for y in range(0, size[1], texture.get_height()):
            result.blit(texture,(x,y))
    return result


def apply_alpha(texture, mask):
    """
    texture是24位或32位的图形,mask是有alpha的8位图
    """
    texture = texture.convert_alpha()
    target = pygame.surfarray.pixels_alpha(texture)
    
    target[:] = pygame.surfarray.array2d(mask) # 修改每个像素透明度
    # surfarray 会锁定图层,当工作完成后把target删除 
    del target
    return texture

def stamp(image, texture, mask):
    """在image上渲染带mask的texture"""
    image.fill((0,120,128))
    image.blit(apply_alpha(texture, mask), (0,0))

You must log in to read the rest of this article. Please log in or register as a user.