首页 > 解决方案 > 如何在 Python 中运行代码之前添加延迟

问题描述

我只是想知道如何在运行代码之前添加延迟,例如:我制作了一个程序,但想要添加 5 秒延迟,因此用户必须在程序启动前等待 5 秒:

import time
import pygame
import sys
import os

pygame.init()


screen = pygame.display.set_mode((250,150))
pygame.display.set_caption('Message from Admin')
font = pygame.font.SysFont("Lucida Console", 30)
label = font.render("You moron", 1, (1, 1, 1))#JUST TO TROLL
while True:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()
            time.sleep(0.10)
            screen = pygame.display.set_mode((250,150))
            pygame.display.set_caption('Error')
    
    screen.fill((255,255,255))
    screen.blit(label, (50,50))
    
    
    
    pygame.display.update()

标签: python-3.xpygame

解决方案


推荐阅读