我有一个例子,可以处理每个像素的透明度,还可以半透明显示。 fill
函数只是循环遍历表面上的像素,并将它们设置为新的颜色,但保留它们的 alpha 值。建议不要在每个帧与许多表面上执行此操作。(按f、g、h键可更改颜色。)
import sys
import pygame as pg
def fill(surface, color):
"""Fill all pixels of the surface with color, preserve transparency."""
w, h = surface.get_size()
r, g, b, _ = color
for x in range(w):
for y in range(h):
a = surface.get_at((x, y))[3]
surface.set_at((x, y), pg.Color(r, g, b, a))
def main():
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
# Uncomment this for a non-translucent surface.
# surface = pg.Surface((100, 150), pg.SRCALPHA)
# pg.draw.circle(surface, pg.Color(40, 240, 120), (50, 50), 50)
surface = pg.image.load('bullet2.png').convert_alpha()
surface = pg.transform.rotozoom(surface, 0, 2)
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
if event.type == pg.KEYDOWN:
if event.key == pg.K_f:
fill(surface, pg.Color(240, 200, 40))
if event.key == pg.K_g:
fill(surface, pg.Color(250, 10, 40))
if event.key == pg.K_h:
fill(surface, pg.Color(40, 240, 120))
screen.fill(pg.Color('lightskyblue4'))
pg.draw.rect(screen, pg.Color(40, 50, 50), (210, 210, 50, 90))
screen.blit(surface, (200, 200))
pg.display.flip()
clock.tick(30)
if __name__ == '__main__':
pg.init()
main()
pg.quit()
sys.exit()
import pygame, sys
from pygame.locals import *
pygame.init()
def load_image(name):
image = pygame.image.load(name).convert()
return image
def resize(obj, w, h):
global scale
return pygame.transform.scale(obj, (int(w * scale), int(h * scale)))
pink = (255, 0, 160)
red = (255, 0, 0)
peach = (255, 118, 95)
blue = (0, 0, 255)
blue_1 = (38, 0, 160)
dark_yellow = (255, 174, 0)
green = (38, 137, 0)
orange = (255, 81, 0)
colour = [pink, red, peach, blue, blue_1, dark_yellow, green, orange, green]
clock = pygame.time.Clock()
scale = 4
screen = pygame.display.set_mode((292 * scale, 240 * scale),0, 32)
banner = load_image("banner.png") #292x35
zero = load_image("zero.png") #5x7
c = 0
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
rgb = colour[c]
c = c + 1
if c > 7:
c = 0
pygame.draw.line(banner, rgb, (53, 21), (53, 27), 5) #line the same size as zero image
banner.blit(zero, (51, 21)) #blit image with transparency over line
large_banner = resize(banner, 292, 35)
screen.blit(large_banner, (0, 0))
clock.tick(120)
pygame.display.flip()
pygame.quit()
sys.exit()
这段代码用于降低所有颜色的亮度
def drop(surface):
w, h = surface.get_size()
for x in range(w):
for y in range(h):
r = surface.get_at((x, y))[0]
if r>150:
r-=50
g = surface.get_at((x, y))[1]
if g>150:
g-=50
b = surface.get_at((x, y))[2]
if b>150:
b-=50
a = surface.get_at((x, y))[3]
surface.set_at((x, y), pygame.Color(r, g, b, a))
img = pygame.image.load("image.png").convert_alpha()
drop(img)
我解决这个问题的方法与其他人似乎有些不同,但我一直在尝试弄清楚这个问题,这里列出的答案并没有给我我想要的东西。我慢慢地根据我找到的东西来实现我的目标。
首先,我只是想将我的动画帧的颜色更改为从列表中选择的随机颜色。我发现的方法使用了pygame.PixelArray()和.replace()来交换一种颜色为另一种颜色:
self.fire_frame_list = glob.glob("pictures\\Boom_Pow\\Colour_Flame\\*.png")
self.fire_frame_list.sort()
self.fire_frame_anim = [pygame.PixelArray(pygame.image.load(path)) for path in self.fire_frame_list]
...
self.fire_img = self.fire_frame_anim[self.frame_index]
self.fire_img.replace(self.colour, self.new_colour)
...
self.colour = self.new_colour
self.new_colour = randRGB()
为了解决这个问题,我必须确保所有的动画帧在创建时都更改了它们的颜色。
self.fire_frame_list = glob.glob("pictures\\Boom_Pow\\Colour_Flame\\*.png")
self.fire_frame_list.sort()
self.fire_frame_anim = [pygame.PixelArray(pygame.image.load(path)) for path in self.fire_frame_list]
for img in self.fire_frame_anim:
img.replace((255,255,255), self.new_colour)
一段时间以来,这段代码一直运行得很完美,直到我尝试使用半透明图像。我确保在我的图像上使用了.convert_alpha(),以保持我的基础颜色为白色,并使alpha值成为更改后的值。这个方法是有效的,但是对于我的PixelArrays却不起作用。
虽然打印出每个RGBA值列出的像素列表显示了正确的值,但.replace()只会更改完全不透明的像素的颜色,其余部分仍然是白色。经过多次实验,我意识到需要使用与检查.convert_alpha()是否给出所需值时相同的方法创建我的像素alpha值列表。
self.img = pygame.image.load("pictures\\Boom_Pow\\Bubbloid.png").convert_alpha()
self.pix = {self.img.get_at((i, j))[3] for j in range(10) for i in range(10)}
self.img = pygame.PixelArray(self.img)
for a in self.pix:
self.img.replace((255, 255, 255, a), self.colour + (a,))
这比所有答案描述的都要简单得多。
如果你阅读Surface.fill文档,它说你可以使用special_flags
来改变混合模式。这是自pygame 1.8发布于2008年以来的情况。
这是我从Open Game Art获得的一个小图形:
如果你尝试不同的special_flags
,你可以创建不同的效果。MIN函数可能是你想要的。特别是当你的基础图像是白色/鲜艳色彩时。def some_loading_function() -> pygame.Surface:
# just pretend that we're loading an image here
graphic = some_loading_function().convert_alpha()
red = (255, 0, 0, 255)
graphic.fill(red, special_flags=pygame.BLEND_RGBA_MIN)
MIN函数将填充颜色与原始像素颜色进行比较,并保留每个通道的最小值。使用上面的示例:
如果您想将整个可见区域设置为相同的颜色,则首先使用MAX函数(基本上是MIN的相反)将每个可见像素替换为白色,然后使用MIN填充所需的颜色:
graphic.fill((255, 255, 255, 0), special_flags=pygame.BLEND_RGBA_MAX)
graphic.fill((0, 200, 100, 255), special_flags=pygame.BLEND_RGBA_MIN)