如何在Python中一次运行多个while循环

2024-04-24 23:26:46 发布

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

我正在尝试为一个项目开发一个简单的Pygame程序,它只是以文本到语音的方式显示一些人脸和对话,但在最后有一个while循环,这是代码运行所必需的,但会阻止程序运行所需的另一个while循环。我试图添加的while循环使用time.sleep(),因此,如果我试图将其放入第一个需要持续运行的块中,程序将崩溃。我相信我可能正在看一些明显的东西,但任何帮助都将不胜感激,谢谢

代码如下:

from random import randint
from time import sleep
import pygame
import pygame.freetype
import time
import random
run = True
pygame.init()

#faces
face = ['^-^', '^v^', '◠◡◠', "'v'", '⁀◡⁀']
talkingFace = ['^o^', '^▽^', '◠▽◠', "'▽'", '⁀ᗢ⁀']
currentFace = random.choice(face)

#background
screen = pygame.display.set_mode((800,600))
screen.fill((0,0,0))

#font and size
myFont = pygame.font.Font('unifont.ttf', 100)

#face render
faceDisplay = myFont.render(str(currentFace), 1, (0,255,0))

#center and draw face
text_rect = faceDisplay.get_rect(center=(800/2, 600/2))
screen.blit(faceDisplay, text_rect)

#prevent crashes
while run:
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            run = False
    pygame.display.flip()

#loop i'm trying to add
while run:
    faceDisplay = myFont.render(str(currentFace), 1, (0,255,0))
    screen.blit(faceDisplay, text_rect)
    time.sleep(randint(5, 10))

Tags: runtextrectimporttimesleeprandomrender
1条回答
网友
1楼 · 发布于 2024-04-24 23:26:46

不,事情不是这样的。您有一个应用程序循环,所以请使用它time.sleep^{}^{}不是典型应用程序中等待或延迟某件事情的方式。您等待时,游戏没有响应。使用^{}测量时间

在pygame中,系统时间可以通过调用^{}获得,它返回自调用^{}以来的毫秒数。见^{}模块

计算需要更改文本的时间点。获取新的随机文本,并在当前时间大于计算时间点后渲染文本曲面。计算大于当前时间的新随机时间点:

next_render_time = 0

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

    # [...]

    if current_time >= next_render_time:
        currentFace = random.choice(face)
        faceDisplay = myFont.render(str(currentFace), 1, (0,255,0))
        next_render_time = current_time + randint(5, 10) * 1000

    screen.fill((0,0,0))
    screen.blit(faceDisplay, text_rect)
    pygame.display.flip()

时间乘以1000(randint(5, 10) * 1000),因为pygame.time.get_ticks()的时间单位是毫秒

实际上,您还可以使用计时器事件。见How do I use a PyGame timer event?
但是,在您的情况下,时间间隔不是恒定的。在动态变化的时间间隔上使用计时器事件有点奇怪。对于需要以固定间隔执行的操作,我更愿意使用计时器事件。但这只是我的意见


完整示例:

from random import randint
from time import sleep
import pygame
import pygame.freetype
import time
import random
run = True
pygame.init()

#faces
face = ['^-^', '^v^', '◠◡◠', "'v'", '⁀◡⁀']
talkingFace = ['^o^', '^▽^', '◠▽◠', "'▽'", '⁀ᗢ⁀']
currentFace = random.choice(face)

#background
screen = pygame.display.set_mode((800,600))

#font and size
myFont = pygame.font.Font('unifont.ttf', 100)

#face render
faceDisplay = myFont.render(str(currentFace), 1, (0,255,0))

#center and draw face
text_rect = faceDisplay.get_rect(center=(800/2, 600/2))

next_render_time = 0

#prevent crashes
while run:
    current_time = pygame.time.get_ticks()
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            run = False
 
    if current_time >= next_render_time:
        currentFace = random.choice(face)
        faceDisplay = myFont.render(str(currentFace), 1, (0,255,0))
        next_render_time = current_time + randint(5, 10) * 1000

    screen.fill((0,0,0))
    screen.blit(faceDisplay, text_rect)
    pygame.display.flip()

相关问题 更多 >