[英]For a pygame.Surface element, is there a way I use .fill() to change a specific colour?
I'm making a game and have a fish sprite with alpha colour pink, I want to change the colour pink on to something else, so fo instance here I tried to change it to orange but instead it made it red because I think it's blending somehow?我正在制作一个游戏,并且有一个带有 alpha 颜色粉红色的鱼精灵,我想将粉红色更改为其他颜色,所以在这里我尝试将其更改为橙色,但它改为红色,因为我认为它正在混合不知何故? Is there a way I can fill in just the alpha pixels or specify a colour to change?
有没有办法可以只填充 alpha 像素或指定要更改的颜色?
Thanks, all the best谢谢,万事如意
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)
A very quick way of doing it is just to use transform.threshold .一个非常快速的方法就是使用transform.threshold 。
Or you can use this function或者你可以使用这个 function
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.
Since the entire sprite "colour" is filled with pink, a neat way to solve this issue is to make the sprites main colour transparent, and then overlay it onto a same-sized rectangle of the desired hue.由于整个精灵“颜色”都被粉红色填充,解决这个问题的一种巧妙方法是使精灵主色透明,然后将其覆盖到所需色调的相同大小的矩形上。
First make a fish with a transparent background (I used TheGIMP to edit it).首先制作一条透明背景的鱼(我使用TheGIMP编辑它)。
Then in your sprite class (or however you want to implement it), create a pygame.Surface
the same size, filling it with the desired colour.然后在你的精灵 class (或者你想实现它)中,创建一个
pygame.Surface
相同大小,用所需的颜色填充它。 Then blit()
the clear-fish image on top of the coloured 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
Ref: Full Example Code ~参考:完整示例代码~
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.