簡體   English   中英

如何在一段時間內使圖像透明?

[英]How do you make an image transparent over a period of time?

我目前正在嘗試在玩家死於游戲時顯示死亡屏幕,但我不希望它突然彈出,而是逐漸變得越來越不透明,直到不透明度為256。我目前在下面的代碼行中進行顯示圖像,但我想知道如何在幾秒鍾的時間內逐漸增加不透明度。

screen.blit(pygame.image.load("Images/dead.jpg"), (0, 0))

我試圖添加一個for循環並嘗試使用convert_alpha,但是我想不出一種方法,將不勝感激。

這就是我要做的。 我已經在評論中解釋了所有內容:

# Allow the image to have its alpha value changed
image = pygame.image.load("Images/dead.jpg").convert()

# Set the transparency to full
image_alpha = 0

# Decide how many frames you want the image to fade in over
fade_frame_number = 60
# And the frames per second
FPS = 30
FPS_Clock = pygame.time.Clock()

while True:
    window.fill((255, 255, 255))               # Fill the window with a white bg
    if image_alpha < 255:
        image_alpha += 255 / fade_frame_number # Change the transparency variable
    image.set_alpha(image_alpha)               # Set the image's alpha value
    window.blit(image, (0, 0))                 # Display the image
    pygame.display.update()                    # Update the screen
    FPS_Clock.tick(FPS)                        # Wait for the next frame

在此, window是顯示表面。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM