繁体   English   中英

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

[英]How to run multiple while loops at a time in Pygame

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

不,事情不是这样的。 你有一个应用程序循环,所以使用它。 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()的时间单位是毫秒。

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


完整示例:

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

暂无
暂无

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

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