【问题标题】:For a pygame.Surface element, is there a way I use .fill() to change a specific colour?对于 pygame.Surface 元素,有没有办法使用 .fill() 来更改特定颜色?
【发布时间】:2020-06-25 11:57:33
【问题描述】:

我正在制作一个游戏并且有一个带有 alpha 颜色粉红色的fish sprite,我想将粉红色更改为其他颜色,所以在这里我尝试将其更改为橙色,但它却将其变为红色,因为我认为它以某种方式混合?有没有办法只填充 alpha 像素或指定要更改的颜色?

谢谢,一切顺利

self.image = pygame.image.load(os.path.join(game_folder,"boxfish.png")).convert()
self.image.set_colorkey(Colour.pink)
self.rect = self.image.get_rect()
self.image.fill(Colour.orange,special_flags = 3)

【问题讨论】:

  • 你能在问题上添加鱼的图片吗?您是要为整个背景着色,还是只想为鱼“内部”的一个区域着色?
  • 我希望鱼中的粉红色是我喜欢的任何颜色,绿色、蓝色等

标签: python pygame sprite


【解决方案1】:

一个非常快速的方法就是使用transform.threshold

或者你可以使用这个功能

def change_color(img, oldcolor, newcolor):
    for x in range(img.get_width()):
        for y in range(img.get_height()):
            pixel_color = img.get_at((x, y))  # Preserve the alpha value.
            if oldcolor == pixel_color:
                img.set_at((x, y), newcolor)  # Set the color of the pixel.

【讨论】:

  • transform.threshold 听起来是个不错的方法。你有一个方便的例子吗?我无法让它快速工作。
  • 知道了:pygame.transform.threshold( pink_fish_image, new_image, PINK_COLOUR, set_color=desired_colour, inverse_set=True )。但是您需要制作图像的新/副本,或目的地的另一个表面。好建议!它帮助我学到了一些新东西。
  • 感谢您指出transform.threshold()。像金斯利一样,我不知道这种方法,它看起来很方便。我的问题是为什么您实际上没有在答案中使用它?由于这些库通常是用 C 编码的,因此它可能比您实际回答的 python 例程快得多。我可以建议您更新您的答案以至少包含一个使用它的版本吗?
  • 非常感谢,太好了!
【解决方案2】:

由于整个精灵“颜色”都被粉红色填充,解决这个问题的一个巧妙方法是使精灵主色透明,然后将其覆盖到所需色调的相同大小的矩形上。

先制作一条透明背景的鱼(我用TheGIMP编辑)。


clear_fish.png

然后在你的精灵类中(或者你想实现它),创建一个相同大小的pygame.Surface,用所需的颜色填充它。然后blit() 彩色表面上的透明鱼图像。

fish_image = pygame.image.load( 'clear_fish.png' )  # un-coloured fish
fish_rect  = fish_image.get_rect()

# Apply some coloured scales to the fish
fish_scales= pygame.Surface( ( fish_rect.width, fish_rect.height ) )
fish_scales.fill( colour )
fish_scales.blit( fish_image, (0,0) )
fish_image = fish_scales               # use the coloured fish

参考:完整示例代码~

import pygame
import random

# Window size
WINDOW_WIDTH    = 600
WINDOW_HEIGHT   = 600
WINDOW_SURFACE  = pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE

DARK_BLUE = (   3,   5,  54)


### initialisation
pygame.init()
pygame.mixer.init()
window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ), WINDOW_SURFACE )
pygame.display.set_caption("1-Fish, 2-Fish, Pink Fish, Clear Fish")

class FishSprite( pygame.sprite.Sprite ):
    def __init__( self, colour ):
        pygame.sprite.Sprite.__init__( self )
        self.image = pygame.image.load( 'clear_fish.png' )
        self.rect  = self.image.get_rect()

        # Apply some coloured scales to the fish
        fish_scales= pygame.Surface( ( self.rect.width, self.rect.height ) )
        fish_scales.fill( colour )
        fish_scales.blit( self.image, (0,0) )
        self.image = fish_scales                # use the coloured fish

        # Start position is randomly across the screen
        self.rect.center = ( random.randint(0, WINDOW_WIDTH), random.randint(0,WINDOW_HEIGHT) )

    def update(self):
        # Just move a bit
        self.rect.x += random.randrange( -1, 2 )
        self.rect.y += random.randrange( -1, 2 )


### Sprites
fish_sprites = pygame.sprite.Group()
fish_sprites.add( FishSprite( ( 255, 200,  20 ) ) )  # add some fish
fish_sprites.add( FishSprite( ( 255,  20, 200 ) ) )
fish_sprites.add( FishSprite( (  55,  00, 200 ) ) )
fish_sprites.add( FishSprite( (  20, 200,  20 ) ) )

### Main Loop
clock = pygame.time.Clock()
done = False
while not done:

    # Handle user-input
    for event in pygame.event.get():
        if ( event.type == pygame.QUIT ):
            done = True
        elif ( event.type == pygame.MOUSEBUTTONUP ):
            # On mouse-click add a fish
            mouse_pos     = pygame.mouse.get_pos()
            random_red    = random.randint( 50, 250 )
            random_green  = random.randint( 50, 250 )
            random_blue   = random.randint( 50, 250 )
            random_colour = ( random_red, random_green, random_blue )
            fish_sprites.add( FishSprite( random_colour ) ) 

    # Update the window, but not more than 60fps
    fish_sprites.update()
    window.fill( DARK_BLUE )
    fish_sprites.draw( window )
    pygame.display.flip()

    # Clamp FPS
    clock.tick_busy_loop(60)


pygame.quit()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-02
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多