我试图为我的最终项目制作节奏游戏。我正在使用pygame,并且希望程序根据我正在播放的音乐绘制形状,稍等片刻,然后绘制另一个形状。我的程序是否可以在绘制每种形状之间等待一秒钟,但程序的其余部分是否仍在运行? (不是pygame.time.delay())
我尝试了http://fredericiana.com/2014/11/14/settimeout-python-delay/,它要么无效,要么无法正确实现
def spawnShapesGameOne(gameInPlay, gameInPlayOne,drawShapesOne):
if gameInPlay == True:
if drawShapesOne == True:
pygame.draw.rect(surface, GREEN,(w*.23, h*.25, w*.05,w*.05))
#Wait one second
pygame.draw.rect(surface, GREEN,(w*.73, h*.25, w*.05,w*.05))
#Wait one second
pygame.draw.rect(surface, GREEN,(w*.73, h*.65, w*.05,w*.05))
#Wait one second
pygame.draw.rect(surface, GREEN,(w*.23, h*.65, w*.05,w*.05))
答案 0 :(得分:2)
这很简单,您通常可以使用时间模块来执行此非阻塞延迟/计划:
import time
timer1sec = 0
def draw_myshape():
# your drawing code here
global timer1sec
timer1sec = time.time() # reset our timer
# main pygame loop
while True:
if time.time() - timer1sec >= 1: # if one seconds passed
draw_myshape()
# rest of pygame code here