【问题标题】:What alternatives are there to `time.sleep()` and `pygame.time.wait()`?`time.sleep()` 和 `pygame.time.wait()` 有哪些替代方法?
【发布时间】:2021-03-25 17:50:12
【问题描述】:

我对 python 很陌生。我正在做一个简单的游戏。到目前为止,我想做的是在显示问题矩形和显示选项之间添加几秒钟的延迟。我该怎么做?我尝试使用time.sleeppygame.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
  • 哦,好的 :) 感谢您的反馈!
  • 您不能在draw 中设置“睡眠”,因为显示仅在draw 之后更新一次。场景不断地被重绘,这意味着draw被不断地调用。您必须在不同的框架中显示问题。
  • 那么我要做的是让它调用一个执行 time.sleep 的函数?
  • 没有。我告诉过你完全使用“睡眠”。您需要一个状态变量或计数器。在draw 中更改状态或增加计数器。根据状态变量或计数器提出问题。

标签: python pygame pgzero


【解决方案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()

【讨论】:

    猜你喜欢
    • 2014-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-13
    • 2011-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多