Pygame - 如何 blit 图像但更改其颜色? [英] Pygame - How do I blit an image but change its color?

查看:85
本文介绍了Pygame - 如何 blit 图像但更改其颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个随机标志生成器的程序.我为旗帜制作了一个白色的背景",以及白色的不同符号等.它们是白色的原因是我可以使用 RGB 值对其进行着色.问题是,我不确定该怎么做(如果可能的话),那么是否可以在 pygame 中对图像进行 blit 并为其赋予 RGB 值?例如(这可能是不正确的,但最后 2 个 blit 中的第二个元组代表 RGB):

I have a program for a random flag generator. I have made a white "background" for a flag, and different symbols etc. which are white. The reason they're white is so I can color it using an RGB value. The problem is, I'm not sure how to do that(if it's even possible), so is it possible to blit an image and give it an RGB value in pygame? For example(this is probably incorrect, but the 2nd tuple in the last 2 blits represents RGB):

surface=pygame.display.set_mode((100,200))
background=pygame.image.load("Background.png")
flagback=pygame.image.load("back.png")
symbol=pygame.image.load("symbol.png")
surface.blit(background,(0,0))
surface.blit(flagback,(0,0),(100,0,0))
surface.blit(symbol,(0,0),(0,100,0))

推荐答案

您可以使用 pygame.Surface.fill 用纯色填充表面的方法,例如:

You can use the pygame.Surface.fill method to fill a surface with a solid color, e.g.:

background.fill((0, 100, 200))

上面的线条将填充整个图像,甚至是透明像素.

The line above will fill the whole image, even the transparent pixels.

您还可以传递 special_flags 参数以使用不同的混合模式::>

You can also pass a special_flags argument to use a different blending mode:

background.fill((0, 100, 200), special_flags=pygame.BLEND_MULT)

这个不会影响完全透明的像素.

This one doesn't affect fully transparent pixels.

如果您只想绘制一些形状,例如矩形、圆形或多边形,请使用 pygame.draw(或 <代码>pygame.gfxdraw) 模块.

If you just want to draw some shapes like rects, circles or polygons, use the functions in the pygame.draw (or pygame.gfxdraw) module.

这篇关于Pygame - 如何 blit 图像但更改其颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆