有哪些可以替代 `time.sleep()` 和 `pygame.time.wait()` 的方法?

5

我对Python还很陌生。我正在制作一个简单的游戏。目前,我试图在显示问题矩形和显示选项之间添加几秒钟的延迟。我该怎么做?我尝试使用time.sleep或者pygame.time.wait,但是所有这些方法都会显示黑屏,然后同时显示问题和选项。顺便说一下,我正在使用pygame :)。这是我的代码:

try:
    logname = 'c:/temp/pgzrun.log'
    fontname = 'arial.ttf'   
    import faulthandler
    import math
    faulthandler.enable()
    import time
    import os, sys, importlib
    from time import sleep 
   
    script_dir = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
    os.chdir(script_dir)
    
    import pgzrun
    import playsound
    import pygame 
    
    import random 
    from random import randint

    WIDTH = 1280
    HEIGHT = 720
    sys.setrecursionlimit(10000000)
    q1 = ["SIFS", "ba", "bo", "bi", "blo", 1]
    q2 = ["AFST", "la", "lo", "li", "lloo", 3]
    q3 = ["jaks", "fa", "fo", "fi", "asdlo", 2]
    q4 = ["afsa", "afsfga", "dfsdff", "dfdf", "safaawr", 2]
    questions = [q1, q2, q3, q4]
    question_box = Rect(500, 400, 140, 100)
  
    def draw():
        
        index = 0
        screen.fill("purple")
        screen.draw.filled_rect(question_box, "blue")
        screen.draw.textbox(str(questions[index][0]), question_box)
        screen.draw.filled_rect(answer_boxes[0], "blue")
        screen.draw.filled_rect(answer_boxes[0], "blue")
        screen.draw.filled_rect(answer_boxes[1], "blue")
        screen.draw.filled_rect(answer_boxes[2], "blue")
        screen.draw.filled_rect(answer_boxes[3], "blue")
     
    ab1 = Rect(0, 0, 140, 100)
    ab2 = Rect(0, 0, 140, 100)
    ab3 = Rect(0, 0, 140, 100)
    ab4 = Rect(0, 0, 140, 100)
    ab1.move_ip(40, 80)
    ab2.move_ip(300, 80)
    ab3.move_ip(600, 80)
    ab4.move_ip(900, 80)
    answer_boxes = [ab1, ab2, ab3, ab4]
    random.shuffle(questions)
    game_over = False
   
    pgzrun.go()
        
except:
    import traceback
    with open(logname, 'a', encoding = 'utf-8') as f:
        f.write(''.join(traceback.format_exc()) + '\n')

不,你没有使用PyGame,而是使用Pygame Zero。 - Rabbid76
那么我会让它调用一个执行time.sleep的函数? - Parul Deep Singh
你几个小时前已经问过同样的问题了,这并不是我们期望的。你需要改进原始问题。如果每个人都多次问同一个问题会发生什么?请阅读《如何提出好问题》和《如何创建最小可重现示例》。链接:https://stackoverflow.com/help/how-to-ask 和 https://stackoverflow.com/help/minimal-reproducible-example。 - Rabbid76
抱歉,我忘记删除那个问题了 :) - Parul Deep Singh
好的,谢谢你的尝试。@Arty,你能帮我吗? - Parul Deep Singh
显示剩余5条评论
1个回答

1
当您编写交互式应用程序时,会有一个事件循环。在这种情况下,您不应该使用sleep或类似的命令来阻塞程序。
相反,您应该使用定时器来触发事件。在Pygame Zero中,您可以使用clock.schedule在指定的时间后触发函数调用。
以下是我实现此类应用程序的方式:
import pgzrun

questions = ["One", "Two"]

index = 0
can_answer = False

def show_answers():
    global can_answer
    can_answer = True

def on_mouse_down(pos):
    global can_answer, index
    if can_answer:
        can_answer = False
        index = index + 1
        clock.schedule(show_answers, 1.0)
    
def draw():
    screen.fill("black")
    screen.draw.textbox(questions[index], Rect(0, 0, 200, 100))
    if can_answer:
        screen.draw.filled_rect(Rect(0, 100, 200, 50), "blue")

clock.schedule(show_answers, 1.0)
pgzrun.go()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接