python 存储图片 alpha_python – 如何在图片上使用pygame set_alpha()

在Python的pygame库中处理图片时,为了改变图像的透明度,需要将图像转换为可调整alpha的形式。通过调用`image.convert()`或`image.convert_alpha()`方法可以实现这一目的。然而,`convert_alpha()`在某些情况下可能不工作,此时可以使用`convert()`代替。代码示例展示了如何在循环中逐步改变图像的透明度,以观察效果。注意,适当调整`pygame.time.delay()`的参数以控制动画速度。
摘要由CSDN通过智能技术生成

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

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

image = image.convert()

要么

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

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

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(

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值