Pygame set_alpha 不适用于尝试的背景淡入淡出 [英] Pygame set_alpha not working with attempted background fading

查看:74
本文介绍了Pygame set_alpha 不适用于尝试的背景淡入淡出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试创建一个简短的代码,用于一个可以淡入和淡出黑色的项目,但由于某种原因,只有淡入功能在工作,而淡出功能或多或少被跳过.通过给他们参数,我确认问题出在第二个函数中,并且透明度根本没有改变.这是我的代码-

导入pygame屏幕 = pygame.display.set_mode((800,600))image = pygame.image.load_extended('Map.png').convert_alpha()图像 = pygame.transform.scale(image,(530,300))image.set_alpha(0)x = 0y = 255定义淡入(x):为真:screen.blit(图像,(0,0))pygame.display.update()image.set_alpha(x)pygame.time.delay(100)如果 x <255:x += 5别的:pygame.time.delay(500)x = 0淡出(y)定义淡出(y):为真:screen.blit(图像,(0,0))pygame.display.update()image.set_alpha(y)pygame.time.delay(100)如果 y >0:y -= 5别的:pygame.time.delay(500)y = 255淡入(x)为真:淡入(x)

有人知道问题可能出在哪里吗?

解决方案

当您在屏幕上绘制透明表面时,该表面会与屏幕的当前内容混合.因此,您需要在使用 screen.fill(0) 绘制渐变背景之前清除屏幕.


不要试图以递归方式或在应用程序循环中使用循环来控制应用程序.不要延迟游戏.delay 导致游戏停止响应.

使用应用程序循环并使用

导入pygamepygame.init()屏幕 = pygame.display.set_mode((400,300))时钟 = pygame.time.Clock()尝试:image = pygame.image.load_extended('Map.png').convert_alpha()图像 = pygame.transform.scale(image,(530,300))除了:图像 = pygame.Surface(screen.get_size())pygame.draw.circle(image, (255, 0, 0), screen.get_rect().center, min(*screen.get_size())//2 - 20)阿尔法 = 0alpha_change = 1运行 = 真运行时:时钟滴答(60)对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:运行 = 错误alpha += alpha_change如果不是 0 <= alpha <= 255:alpha_change *= -1alpha = max(0, min(alpha, 255))屏幕填充(0)alpha_image = image.copy()alpha_image.set_alpha(alpha)screen.blit(alpha_image, (0, 0))pygame.display.flip()pygame.quit()出口()

I've been trying to create a short code to use for a project that can fade in and from black, but for some reason only the function that fades in is working while the fade out function is being skipped more or less. By giving them parameters I confirmed that the problem is in the second function and that the transparency isn't changing at all. Here's my code-

import pygame
screen = pygame.display.set_mode((800,600))
image = pygame.image.load_extended('Map.png').convert_alpha()
image = pygame.transform.scale(image,(530,300))
image.set_alpha(0)
x = 0
y = 255
def fade_in(x):
  while True:
    screen.blit(image,(0,0))
    pygame.display.update()
    image.set_alpha(x)
    pygame.time.delay(100)
    if x < 255:
      x += 5
    else:
      pygame.time.delay(500)
      x = 0
      fade_out(y)
def fade_out(y):
  while True:
    screen.blit(image,(0,0))
    pygame.display.update()
    image.set_alpha(y)
    pygame.time.delay(100)
    if y > 0:
      y -= 5
    else:
      pygame.time.delay(500)
      y = 255
      fade_in(x)
while True:
  fade_in(x)

Does anyone have an idea of what the problem might be?

解决方案

When you draw a transparent surface on the screen, the surface is blended with the current contents of the screen. Hence you need to clear the screen before drawing the fading background with screen.fill(0).


Do not try to control the application recursively or with loops within the application loop. Do not delay the game. delay causes the game to stop responding.

Use the application loop and use pygame.time.Clock to control the frames per second and thus the game speed.

The method tick() of a pygame.time.Clock object, delays the game in that way, that every iteration of the loop consumes the same period of time. See pygame.time.Clock.tick():

This method should be called once per frame.

That means that the loop:

clock = pygame.time.Clock()
run = True
while run:
   clock.tick(60)


Example:

import pygame

pygame.init()
screen = pygame.display.set_mode((400,300))
clock = pygame.time.Clock()

try:
    image = pygame.image.load_extended('Map.png').convert_alpha()
    image = pygame.transform.scale(image,(530,300))
except:
    image = pygame.Surface(screen.get_size())
    pygame.draw.circle(image, (255, 0, 0), screen.get_rect().center, min(*screen.get_size()) // 2 - 20)

alpha = 0
alpha_change = 1

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False          

    alpha += alpha_change
    if not 0 <= alpha <= 255:
        alpha_change *= -1
    alpha = max(0, min(alpha, 255))   

    screen.fill(0)

    alpha_image = image.copy()
    alpha_image.set_alpha(alpha)    
    screen.blit(alpha_image, (0, 0))
    
    pygame.display.flip()

pygame.quit()
exit()

这篇关于Pygame set_alpha 不适用于尝试的背景淡入淡出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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