如何使滚动文本,而不抑制游戏的功能?皮格姆

2024-04-26 12:18:56 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我正在为a级课程制作的一个游戏的片段。我正在做一个介绍剪贴画,其中我希望使文本显示一个字母在一个时间(卷轴,口袋妖怪风格)。但是,我当前的解决方案要求在每行文本中使用for循环。这对于视觉效果很好,但是它会阻止用户与窗口进行交互。我想添加一个跳过按钮,但不能这样做,因为这个问题。我尝试使用更多的if语句,但是代码变得混乱、有bug,效率也低很多。有更简单、更有效的解决方法吗?你知道吗

screen.blit(introImage4,(0,16))
if flags["IntroStage3"] == True:
    for i in range(len(introText[0])):
        introTextImage1 = myFont.render(introText[0][i], True, white)
        screen.blit(introTextImage1,(50 + (myFont.size(introText[0][:i])[0]), 50))
        pygame.display.update()
        clock.tick(textFPS)
    for i in range(len(introText[1])):
        introTextImage2 = myFont.render(introText[1][i], True, white)
        screen.blit(introTextImage2,(50 + (myFont.size(introText[1][:i])[0]), 100))
        pygame.display.update()
        clock.tick(textFPS)
    for i in range(len(introText[2])):
        introTextImage3 = myFont.render(introText[2][i], True, white)
        screen.blit(introTextImage3,(50 + (myFont.size(introText[2][:i])[0]), 150))
        pygame.display.update()
        clock.tick(textFPS)
    flags["IntroStage4"] = True
    flags["IntroStage3"] = False

if flags["IntroStage4"] == True:
    introTextImage1 = myFont.render(introText[0], True, white)
    introTextImage2 = myFont.render(introText[1], True, white)
    introTextImage3 = myFont.render(introText[2], True, white)
    screen.blit(introTextImage1,(50, 50))
    screen.blit(introTextImage2,(50, 100))
    screen.blit(introTextImage3,(50, 150))
    flags["IntroStage5"] = True

Tags: intrueforifrangerenderscreenflags
1条回答
网友
1楼 · 发布于 2024-04-26 12:18:56

这里的问题是,在for循环完成之前,事件处理程序无法运行新的检查。你知道吗

解决方案是为文本编写一个动画函数。您可以通过添加一个变量来实现这一点,该变量包含屏幕上显示的文本,然后您可以将该变量的值更改为您希望基于某个时间相关值滚动的完整文本的另一部分。你知道吗

此时间相关值可以是自触发滚动文本的事件以来经过的时间。你知道吗

更清楚一点,举个例子:

“我只想在一个大的文本框中显示两秒钟的文本,但我只想显示一秒钟:

text = "Alice has a big basket of fruit"
def animate_text(text,t): #here t is current unix time minus the unix time whenthat the event that triggered the text scrolling
    text_batches=text.split(' ')
    return text_batches[t//2] if t//2 <= len(text_batches) else return False

因此,现在我们已经将文本拆分为多个批,而不是在主循环中嵌套循环,您可以根据动画开始后经过的时间来blit批

while 1!=0:

    # game loopy stuff here

    blit(animate_text(text,time.time()-time_when_animation_started))

现在这一切都有点混乱和伪代码,它不能完美地处理你的特殊情况,但你应该在这里得到的想法

相关问题 更多 >