如何在 Python 中一次运行多个 while 循环 [英] How to run multiple while loops at a time in Python

查看:160
本文介绍了如何在 Python 中一次运行多个 while 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为一个项目编写一个简单的 Pygame 程序,该程序仅显示一些面孔并以文本到语音的形式进行对话,但最后有一个 while 循环,该循环是代码运行所必需的,但会阻塞程序运行所需的另一个 while 循环.我尝试添加的 while 循环使用 time.sleep(),因此如果我尝试将它与需要不断运行的第一个块放在同一块中,程序就会崩溃.我确定我可能正在查看一些明显的东西,但任何帮助将不胜感激,谢谢!

I'm trying to work on a simple Pygame program for a project that simply displays some faces and talks in a text to speech voice, but there is a while loop at the end that is necessary for the code to run but blocks another while loop that I need for the program from running. The while loop I'm trying to add uses time.sleep(), so if I try to put it into the same block as the first one which needs to be constantly running the program crashes. I'm sure I'm probably looking over something obvious but any help would be appreciated, thanks!

代码如下:

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))

推荐答案

不,事情不是这样的.您有一个应用程序循环,因此请使用它.time.sleep, pygame.time.wait()pygame.time.delay 不是典型应用程序中等待或延迟某事的方式.等待时游戏没有响应.使用 pygame.time.get_ticks() 测量时间.

No, that's not the way it goes. You have an application loop so use it. time.sleep, pygame.time.wait() or pygame.time.delay is not the way to wait or delay something in a typical application. The game does not respond while you wait. Use pygame.time.get_ticks() to measure the time.

在pygame中可以通过调用<获取系统时间code>pygame.time.get_ticks(),它返回自 pygame.init() 被调用.请参阅 pygame.time 模块.

In pygame the system time can be obtained by calling pygame.time.get_ticks(), which returns the number of milliseconds since pygame.init() was called. See pygame.time module.

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

Calculate the point in time when the text needs to be changed. Get a new random text and render the text Surface after the current time is greater than the calculated point of time. Calculate a new random point in time which is greater than the current time:

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()的时间单位是毫秒.

The time is multiplied by 1000 (randint(5, 10) * 1000), since the time unit of pygame.time.get_ticks() is milliseconds.

实际上,您也可以使用计时器事件.请参阅如何使用 PyGame 计时器事件?.
但是,在您的情况下,时间间隔不是恒定的.在动态变化的间隔上使用计时器事件有点奇怪.我更愿意将计时器事件用于需要以恒定间隔执行的操作.但这只是我的意见.

Actually, you can also use a timer event. See How do I use a PyGame timer event?.
In your case, however, the time interval is not constant. It's a bit odd to use a timer event on a dynamically changing interval. I would prefer to use a timer event for an action that needs to be performed at constant intervals. But that's just my opinion.

完整示例:

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()

这篇关于如何在 Python 中一次运行多个 while 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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