绘制形状纹理-python pygame
我目前正在使用随机导入创建五个随机的x、y值,并使用pygame.draw.polygon()命令获取这些值并绘制一个多边形。如果我有一个纹理正方形,我想应用在这个形状的顶部,而不是只有一个rgb值,那么最有效的方法是什么?我想把生成的多边形放在下面,不需要硬编码它的形状,取一个普通的纹理方块,把所有的绿色都变成新的纹理,就好像这个形状是从纹理方块上剪下来的一样绘制形状纹理-python pygame,python,pygame,textures,transparency,mask,Python,Pygame,Textures,Transparency,Mask,我目前正在使用随机导入创建五个随机的x、y值,并使用pygame.draw.polygon()命令获取这些值并绘制一个多边形。如果我有一个纹理正方形,我想应用在这个形状的顶部,而不是只有一个rgb值,那么最有效的方法是什么?我想把生成的多边形放在下面,不需要硬编码它的形状,取一个普通的纹理方块,把所有的绿色都变成新的纹理,就好像这个形状是从纹理方块上剪下来的一样 import pygame,random from pygame import* height = 480 width = 640
import pygame,random
from pygame import*
height = 480
width = 640
#colors
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
white = (255,255,255)
black = (0,0,0)
pygame.init()
points = [ ]
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption("PlayBox")
r = random
for i in range(0,5):
x = r.randrange(0,640)
y = r.randrange(0,480)
points.append([x,y])
running = True
while running == True:
screen.fill(white)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
break
pygame.draw.polygon(screen,green,points,0)
pygame.display.update()
pygame.display.update()
当然,一个选择是自己重新实施“桶填充”算法, 并复制多边形内的像素。这将是大量的工作,而且在纯Python中完成得很慢——不过,它会让您了解图像处理的基本基础 由于Pygame已经完成了繁重的工作,但只提供纯色填充, 方法是使用pygame的结果作为纹理的剪裁遮罩。不幸的是,这可能比它应该做的更困难。我希望我的样品在这里 对其他有相同需求的人可能有用 Pygame为我们提供了一些原语来操纵曲面中的颜色平面, 但他们的水平肯定很低。另一件事是这些原语需要 numpy待安装-我不确定Window的pyagames安装程序是否包含它- 否则,运行您的项目的人必须被告知自己安装numpy 所以,我们要走的路是: 在曲面中加载所需的纹理(为了减少头痛,请使用相同大小的纹理) ,以绘制要使用纹理绘制的形状 在遮罩曲面中,使用8bpp(黑白)-该选项用作遮罩曲面的透明度贴图 质地 他们使用pygame的surfarray实用程序将所有内容组合在一起:
# coding: utf-8
import random
import pygame
SIZE = 800,600
def tile_texture(texture, size):
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):
"""
Image should be a 24 or 32bit image,
mask should be an 8 bit image with the alpha
channel to be applied
"""
texture = texture.convert_alpha()
target = pygame.surfarray.pixels_alpha(texture)
target[:] = pygame.surfarray.array2d(mask)
# surfarray objets usually lock the Surface.
# it is a good idea to dispose of them explicitly
# as soon as the work is done.
del target
return texture
def stamp(image, texture, mask):
image.blit(apply_alpha(texture, mask), (0,0))
def main():
screen = pygame.display.set_mode(SIZE)
screen.fill((255,255,255))
texture = tile_texture(pygame.image.load("texture.png"), SIZE)
mask = pygame.Surface(SIZE, depth=8)
# Create sample mask:
pygame.draw.polygon(mask, 255,
[(random.randrange(SIZE[0]), random.randrange(SIZE[1]) )
for _ in range(5)] , 0)
stamp(screen, texture, mask)
pygame.display.flip()
while not any(pygame.key.get_pressed()):
pygame.event.pump()
pygame.time.delay(30)
if __name__ == "__main__":
pygame.init()
try:
main()
finally:
pygame.quit()
实际需要的输出是什么?拍摄png图片或纹理,并将其应用于形状的顶部,同时仍保留相同的形状不确定编辑文本形状的含义,但让我尝试重新解释,我得到5个点,并制作一个多边形,以便在使用pygame.draw.polygon命令时,每次运行程序时形状都会发生变化只能使用rgb值有没有一种方法我可以采用我刚才画的形状,在形状的顶部制作一个png,就像如果png是正方形,如果是圆形,它只在圆圈内制作纹理这样更好。现在这是一个多边形,你不想使用rgb Right,我想拿一个像草一样的纹理为例,只对多边形进行纹理处理,而不是白色背景