【问题标题】:When i use time.sleep it pauses it early当我使用 time.sleep 时,它会提前暂停
【发布时间】:2021-05-09 22:54:36
【问题描述】:

time.sleep 位于末尾,但不知何故,代码首先以 time.sleep 开头。我希望它选择一个随机名称,然后等待 30 秒。 30 秒后,我希望它从 0 到 100 中选择一个随机数。这是给两个玩家的。我也希望它先显示 2 个名称,然后等待 30 秒,然后在播放器 1 和播放器 2 文本下方显示 2 个随机数

import pygame
from pygame import *
import random
import time
import delay
pygame.init()
Red = (247, 12, 12)
White = (255,255,255)
Blue = (0, 157, 255)
Person_One = random.randint(0,19)
Person_Two = random.randint(0,19)
Number1 = random.randint(0,100)
Number2 = random.randint(0,100)
mmm =  ["Donald", "Jacey","Baxter","Dusan","Nathaniel","Hayden","Yusuf","Hayley","Andre","Rafif","Jeremey","Mark","Tia","Malu","Dorian","Jarius","Sammar","Peter","Rafif","Jasmin"]
Playerone = mmm[Person_One]
Playertwo = mmm[Person_Two]
X = 400
timer = 0
Y = 300
display_surface = pygame.display.set_mode((X, Y))
font = pygame.font.Font('freesansbold.ttf', 20)
text = font.render("Player one is:", True, Red, Blue)
textRect = text.get_rect()
textRect.center = (X // 4.4, Y // 3.6)
text1 = font.render(Playerone, True, Red, Blue)
text1Rect = text1.get_rect()
text1Rect.center = (X // 4.4, Y // 3)
text2 = font.render("Player two is:", True, Red, Blue)
text2Rect = text2.get_rect()
text2Rect.center = (X // 1.5, Y // 3.6)
text3 = font.render(Playertwo, True, Red, Blue)
text3Rect = text3.get_rect()
text3Rect.center = (X // 1.5, Y // 3)
aa = True
while True:
 
    # completely fill the surface object
    # with white color
    display_surface.fill(White)
 
    # copying the text surface object
    # to the display surface object
    # at the center coordinate.
    display_surface.blit(text, textRect)
    display_surface.blit(text1, text1Rect)
    display_surface.blit(text2, text2Rect)
    display_surface.blit(text3, text3Rect)
    aa = False
    Number2=str(Number2)
    text3 = font.render(Number2, True, Red, Blue)
    text3Rect = text3.get_rect()
    text3Rect.center = (X // 1.5, Y // 3)
    
    display_surface.blit(text3, text3Rect)
    time.sleep(20)
    pygame.display.update()

【问题讨论】:

    标签: python time pygame


    【解决方案1】:

    在调用 display.update 之前,程序处于休眠状态。只有在调用它时才会显示事物,因此它会等待 20 秒。将 display.update 移到 sleep 调用上方。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-16
      • 2015-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多