简体   繁体   English

如何让 Pygame 在 for 循环中的每个循环之前等待几毫秒而不停止其他东西?

[英]How can I make Pygame wait a few milliseconds before every loop in a for loop without stopping other stuff?

I have a for loop in Pygame that is trying to slowly progress through a string, like how text scrolls in RPGs.我在 Pygame 中有一个 for 循环,它试图通过一个字符串缓慢前进,就像文本在 RPG 中滚动一样。 I want it to wait around 7 milliseconds before displaying the next character in the string, but I don't know how to make the loop wait that long without stopping other stuff.我希望它在显示字符串中的下一个字符之前等待大约 7 毫秒,但我不知道如何在不停止其他内容的情况下使循环等待那么长时间。 Please note that I am very new to pygame and python in general.请注意,我对 pygame 和 python 很陌生。 Here is my code:这是我的代码:

mainText = pygame.font.Font(mainFont, 40)
finalMessage = ""

for letter in msg:
    finalMessage = finalMessage + letter
    renderMainText = mainText.render(finalMessage, True, white)
    screen.blit(renderMainText, (100, 100))
renderMainText = mainText.render(finalMessage, True, white)

Do I need to do threading?我需要做线程吗? Asyncrio?异步?

You don't need the for loop at all.您根本不需要for循环。 You have an application loop, so use it.你有一个应用程序循环,所以使用它。 The number of milliseconds since pygame.init() can be retrieved bypygame.time.get_ticks() .自的毫秒数pygame.init()可以通过检索pygame.time.get_ticks() See pygame.time module.参见pygame.time模块。

next_letter_time = 0
next_letter = 0

run = True
while run:
    current_time = pygame.time.get_ticks()

    # [...]

    if next_letter < len(msg):
        if current_time > next_letter_time:
            next_letter_time = current_time + 7000 # 7000 milliseconds = 7 
            finalMessage = finalMessage + msg[next_letter]
            next_letter += 1
            renderMainText = mainText.render(finalMessage, True, white)

Minimal example:最小的例子:

import pygame

pygame.init()
window = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()

white = (255, 255, 255)
mainText = pygame.font.SysFont(None, 50)
renderMainText = None
finalMessage = ""
msg = "test text"
next_letter_time = 0
next_letter = 0

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

    current_time = pygame.time.get_ticks()
    if next_letter < len(msg):
        if current_time > next_letter_time:
            next_letter_time = current_time + 500
            finalMessage = finalMessage + msg[next_letter]
            next_letter += 1
            renderMainText = mainText.render(finalMessage, True, white)

    window.fill(0)
    if renderMainText:
        window.blit(renderMainText, (100, 100))
    pygame.display.flip()

use this用这个

@coroutine
def my_func():
    from time import sleep
    mainText = pygame.font.Font(mainFont, 40)
    finalMessage = ""
    
    for letter in msg:
        finalMessage = finalMessage + letter
        renderMainText = mainText.render(finalMessage, True, white)
        screen.blit(renderMainText, (100, 100))
        yield from sleep(0.007)
    renderMainText = mainText.render(finalMessage, True, white)

async(my_func)

yield from is according to python 3.4 for more different versions check https://docs.python.org/3/ your function will run independently without interrupting other tasks after async(my_func) yield from 是根据 python 3.4 的更多不同版本检查https://docs.python.org/3/你的函数将独立运行而不会在async(my_func)之后中断其他任务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM