平铺图像与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))

成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)

关于李兴球

李兴球的博客是Python创意编程原创博客
此条目发表在pygame, python分类目录。将固定链接加入收藏夹。