gpt4 book ai didi

python - 如何使用 PyGame 淡出和返回屏幕?

转载 作者:行者123 更新时间:2023-12-02 09:20:49 28 4
gpt4 key购买 nike

所以我试图在使用 PyGame 完成一个关卡后淡出屏幕并重新显示。我的问题是只有 fadeout() 有效,而 fadein() 无效。当调用 fadein() 时,屏幕变黑几秒钟,然后突然显示下一个级别。我找不到问题,有什么想法吗?

def fadeout():
fadeout = pg.Surface((screen_width, screen_height))
fadeout = fadeout.convert()
fadeout.fill(black)
for i in range(255):
fadeout.set_alpha(i)
screen.blit(fadeout, (0, 0))
pg.display.update()


def fadein():
fadein = pg.Surface((screen_width, screen_height))
fadein = fadein.convert()
fadein.fill(black)
for i in range(255):
fadein.set_alpha(255-i)
screen.blit(fadein, (0, 0))
pg.display.update()

最佳答案

您的问题是您淡入黑屏,因此看不到任何效果。顶部绘制了黑色半透明 Surface 的黑色屏幕仍然是黑色 Surface

您应该渲染关卡的第一帧,并将该Surface传输到屏幕上,然后再将fadein表面传输到屏幕上。

<小时/>

这是我一起编写的一个简单示例。按一个键从一个场景切换到下一个场景。

import pygame
import random
from itertools import cycle

class Cloud(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.Surface((50, 20))
self.image.set_colorkey((11, 12, 13))
self.image.fill((11, 12, 13))
pygame.draw.ellipse(self.image, pygame.Color('white'), self.image.get_rect())
self.rect = self.image.get_rect(topleft=(x,y))

def update(self, dt, events):
self.rect.move_ip(dt/10, 0)
if self.rect.left >= pygame.display.get_surface().get_rect().width:
self.rect.right = 0

class DayScene:
def __init__(self):
self.clouds = pygame.sprite.Group(Cloud(0, 30), Cloud(100, 40), Cloud(400, 50))

def draw(self, screen):
screen.fill(pygame.Color('lightblue'))
self.clouds.draw(screen)

def update(self, dt, events):
self.clouds.update(dt, events)

class NightScene:
def __init__(self):
sr = pygame.display.get_surface().get_rect()
self.sky = pygame.Surface(sr.size)
self.sky.fill((50,0,50))
for x in random.sample(range(sr.width), 50):
pygame.draw.circle(self.sky, (200, 200, 0), (x, random.randint(0, sr.height)), 1)
self.clouds = pygame.sprite.Group(Cloud(70, 70), Cloud(60, 40), Cloud(0, 50), Cloud(140, 10), Cloud(100, 20))

def draw(self, screen):
screen.blit(self.sky, (0, 0))
self.clouds.draw(screen)

def update(self, dt, events):
self.clouds.update(dt, events)

class Fader:

def __init__(self, scenes):
self.scenes = cycle(scenes)
self.scene = next(self.scenes)
self.fading = None
self.alpha = 0
sr = pygame.display.get_surface().get_rect()
self.veil = pygame.Surface(sr.size)
self.veil.fill((0, 0, 0))

def next(self):
if not self.fading:
self.fading = 'OUT'
self.alpha = 0

def draw(self, screen):
self.scene.draw(screen)
if self.fading:
self.veil.set_alpha(self.alpha)
screen.blit(self.veil, (0, 0))

def update(self, dt, events):
self.scene.update(dt, events)

if self.fading == 'OUT':
self.alpha += 8
if self.alpha >= 255:
self.fading = 'IN'
self.scene = next(self.scenes)
else:
self.alpha -= 8
if self.alpha <= 0:
self.fading = None

def main():
screen_width, screen_height = 300, 300
screen = pygame.display.set_mode((screen_width, screen_height))
clock = pygame.time.Clock()
dt = 0
fader = Fader([DayScene(), NightScene()])

while True:
events = pygame.event.get()
for e in events:
if e.type == pygame.QUIT:
return
if e.type == pygame.KEYDOWN:
fader.next()

fader.draw(screen)
fader.update(dt, events)

pygame.display.flip()
dt = clock.tick(30)

main()

enter image description here

通过将每个场景抽象为它自己的类并将场景更改委托(delegate)给 Fader 类,我们能够让场景继续(或添加一个简单的 if语句来防止这种情况)并在淡入淡出时处理事件。

关于python - 如何使用 PyGame 淡出和返回屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58540537/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com