试图让部分精灵改变颜色,但整个精灵会改变 [英] Trying to make sections of sprite change colour, but whole sprite changes instead

查看:68
本文介绍了试图让部分精灵改变颜色,但整个精灵会改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的游戏中有一些精灵需要特定的部分才能改变颜色.

我的过程我试图拥有一个纯白色的精灵图像,该图像在不需要颜色的任何地方都是透明的.我在它上面闪烁一个彩色方块,然后在主精灵的顶部,但是主精灵然后到处改变颜色,但同时尊重主精灵的透明度.最让我困惑的部分是,当我将蒙版彩色图像放在主屏幕上时,它看起来确实是正确的.

# 加载主精灵和遮罩精灵self.image = pygame.image.load("Enemy.png").convert_alpha()self.mask = pygame.image.load("EnemyMask.png").convert_alpha()# 创建整个精灵大小的彩色图像self.coloured_image = pygame.Surface([self.width, self.height])self.coloured_image.fill(self.colour)# 用遮罩图像的透明度遮罩彩色图像,这部分有效self.masked = self.mask.copy()self.masked.blit(self.coloured_image, (0, 0), None, pygame.BLEND_RGBA_MULT)# 将蒙版图像放在主精灵的顶部self.image.blit(self.masked, (0, 0), None, pygame.BLEND_MULT)

Enemy.png

EnemyMask.png(白色所以看不见)


最小示例:

精灵:

掩码:

导入pygamedef changColor(image, maskImage, newColor):彩色图像 = pygame.Surface(image.get_size())colouredImage.fill(newColor)蒙面 = maskImage.copy()masked.set_colorkey((0, 0, 0))masked.blit(colouredImage, (0, 0), None, pygame.BLEND_RGBA_MULT)finalImage = image.copy()finalImage.blit(屏蔽,(0, 0),无)返回最终图像pygame.init()窗口 = pygame.display.set_mode((404, 84))image = pygame.image.load('avatar64.png').convert_alpha()maskImage = pygame.image.load('avatar64mask.png').convert_alpha()颜色 = []对于范围 (0, 360, 60) 中的色调:颜色.附加(pygame.Color(0))颜色 [-1].hsla = (色调, 100, 50, 100)图像 = [changColor(image, maskImage, c) for c in colour]时钟 = pygame.time.Clock()下一个颜色时间 = 0运行 = 真运行时:时钟滴答(60)对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:运行 = 错误window.fill((255, 255, 255))对于我,枚举中的图像(图像):window.blit(图像, (10 + i * 64, 10))pygame.display.flip()pygame.quit()出口()

I have a few sprites in my game that need specific parts to be able to change colour.

My process I am trying to to have a pure white sprite image that is transparent everywhere the colour does not need to be. I am blitting a coloured square on top of that, and then that on top of the main sprite, however the main sprite then changes colour everywhere, but while respecting the main sprite transparency. The part that confuses me most is that the masked colour image does look correct when I put it on the main screen.

# Load main sprite and mask sprite
        self.image = pygame.image.load("Enemy.png").convert_alpha()
        self.mask = pygame.image.load("EnemyMask.png").convert_alpha()

# Create coloured image the size of the entire sprite
        self.coloured_image = pygame.Surface([self.width, self.height])
        self.coloured_image.fill(self.colour)

# Mask off the coloured image with the transparency of the masked image, this part works
        self.masked = self.mask.copy()
        self.masked.blit(self.coloured_image, (0, 0), None, pygame.BLEND_RGBA_MULT)

# Put the masked image on top of the main sprite
        self.image.blit(self.masked, (0, 0), None, pygame.BLEND_MULT)

Enemy.png

EnemyMask.png (It's white so can't be seen)

Masked colour Masked Colour

Final Failed Sprite Failed Sprite

Can't post images, not enough reputation

I get no error, but only the white part of the shield is supposed to be green

解决方案

self.image is the loaded image, where you want to change specific regions by a certain color and self.mask is a mask which defines the regions.

And you create an image masked, which contains the regions which are specified in mask tinted in a specific color.

So all you've to do is to .blit the tinted mask (masked) on the image without any special_flags set:

self.image.blit(self.masked, (0, 0))

See the example, where the red rectangle is changed to a blue rectangle:


Minimal example:

Sprite:

Mask:

import pygame

def changColor(image, maskImage, newColor):
    colouredImage = pygame.Surface(image.get_size())
    colouredImage.fill(newColor)
    
    masked = maskImage.copy()
    masked.set_colorkey((0, 0, 0))
    masked.blit(colouredImage, (0, 0), None, pygame.BLEND_RGBA_MULT)

    finalImage = image.copy()
    finalImage.blit(masked, (0, 0), None)

    return finalImage

pygame.init()
window = pygame.display.set_mode((404, 84))

image = pygame.image.load('avatar64.png').convert_alpha()
maskImage = pygame.image.load('avatar64mask.png').convert_alpha()

colors = []
for hue in range (0, 360, 60):
    colors.append(pygame.Color(0))
    colors[-1].hsla = (hue, 100, 50, 100)

images = [changColor(image, maskImage, c) for c in colors]

clock = pygame.time.Clock()
nextColorTime = 0
run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    window.fill((255, 255, 255))
    for i, image in enumerate(images):
        window.blit(image, (10 + i * 64, 10))
    pygame.display.flip()

pygame.quit()
exit()

这篇关于试图让部分精灵改变颜色,但整个精灵会改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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