【问题标题】:How to run multiple while loops at a time in Python如何在Python中一次运行多个while循环[重复]
【发布时间】:2021-03-23 13:48:50
【问题描述】:

我正在尝试为一个项目编写一个简单的 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))

【问题讨论】:

  • 您必须将while 循环分离为一个方法,然后在Thread 上运行该方法。请参阅此link 了解Thread 的工作原理

标签: python pygame


【解决方案1】:

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

在pygame中可以通过调用pygame.time.get_ticks()来获取系统时间,它返回自调用pygame.init()以来的毫秒数。请参阅pygame.time 模块。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    • 2016-09-07
    • 1970-01-01
    相关资源
    最近更新 更多