如何在图片上使用 pygame set_alpha() [英] how to use pygame set_alpha() on a picture

查看:97
本文介绍了如何在图片上使用 pygame set_alpha()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 pygame 和 python 用于我正在构建的项目,并且我正在构建游戏首次打开时的启动画面.我有一个 .png 想要在启动画面中显示,并决定将它从黑色淡入淡出.我发现做到这一点的最好方法是使用设置的 alpha 对图像进行 blitting.我制作了这段代码,但它运行得很慢(程序挂起 30 秒)并且没有给出 alpha.仅在屏幕上显示图片.我做错了什么?

I am using pygame and python for a project I am building, and I am building a splashscreen for when the game first opens. I have a .png that I want to show for the splashscreen, and decided to fade it in and out from black. the best way I found to do this was by blitting the image with a set alpha. I made this code, but it runs really slowly (the program hangs for 30 seconds) and doesn't give an alpha. Only displays the picture onscreen. What am i doing wrong?

screen = pygame.display.set_mode([1066,600])

#Drawable surface
background = pygame.Surface(screen.get_size())

#Used for converting color maps
background = background.convert()

#Splashscreen

#image fades in
for i in range (225):
    background.fill((0,0,0))
    image = pygame.image.load("logo.png")
    image.set_alpha(i)
    logoimage = screen.blit(image,(0,0))
    pygame.display.flip()

pygame.time.delay(2000)

#image fades out

#goes on to display main menu

推荐答案

您可能遇到的另一个问题(除了猴子所说的)是您可能需要使用 surface.convert()将图像转换为可以更改 alpha 的形式.您可以执行以下任一操作.

Another problem that you might be having (besides what monkey said) is that you might need to use surface.convert() which converts the image into a form where the alpha can be changed. You can do either of the following.

image = pygame.image.load("logo.png")
image = image.convert()

image = pygame.image.load("logo.png").convert()

我发现,虽然 surface.convert_alpha() 应该做几乎相同的事情,但它通常不起作用.试试这个测试代码来检查.

I have found that, although surface.convert_alpha() should do pretty much the same thing, it doesn't usually work. Try this test code to check.

import pygame, sys
pygame.init()
window=pygame.display.set_mode((1500, 800))
background=pygame.Surface((window.get_rect().width, window.get_rect().height))
background.fill((0, 0, 0))
image=pygame.image.load('InsertImageHere.png')
image=image.convert()
image2=pygame.image.load('InsertImage2Here.png')
image2=image2.convert_alpha()
rect=image.get_rect()
rect2=image2.get_rect()
rect2.left=rect.width+1
i=1
while True:
    for event in pygame.event.get():
        if event.type==12:
            pygame.quit()
            sys.exit()
    image.set_alpha(i)
    image2.set_alpha(i)
    window.fill((255, 255, 255))
    window.blit(background, background.get_rect())
    window.blit(image, rect)
    window.blit(image2, rect2)
    pygame.time.delay(20)
    i+=1
    if i==255:
        i=1
    pygame.display.update()

在我的测试中,图像 1 正确淡入,但图像 2 一直保持黑暗.你应该自己试试;您的计算机的工作方式可能有所不同.

In my testings, image 1 faded in properly, but image 2 stayed dark the whole time. You should try it for yourself; your computer might work differently.

如果 surface.convert_alpha() 对你有用,你应该使用它,否则,按照我之前说的做.这应该可以解决您的问题.

If surface.convert_alpha() does work for you, you should use it, otherwise, do what I said before. This should solve your problem.

您还应该注意到,我使用了 pygame.time.delay(20) 而不是您之前使用的 2000.如果您以 1 的增量增加 alpha,2000 会有点太长.

You should also note that I used pygame.time.delay(20) rather than 2000 like you had before. 2000 would be a bit too long if you are increasing the alpha in incraments of one.

这篇关于如何在图片上使用 pygame set_alpha()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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