简体   繁体   中英

Is it possible to have my program draw a shape with pygame, wait a second, and then draw another? But, I still want the rest of the program to run

I trying to make a rhythm game for my final project. I'm using pygame, and I want my program to draw a shape, wait a second, and then draw another, in accordance with the music I'm playing. Is it possible for my program to wait a second between drawing each shape, but have the rest of the program still runs? (not pygame.time.delay())

I've tried http://fredericiana.com/2014/11/14/settimeout-python-delay/ which either didn't work, or I failed to implement it correctly

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))

you can do this non-blocking delay / schedule in general by using time module or any timer:

Note: I moved reset timer to main loop

import time
timer1sec = 0

def draw_myshape():
    # your drawing code here     

# main pygame loop
while True:
    if time.time() - timer1sec >= 1:   # if one seconds passed
        draw_myshape()
        timer1sec = time.time()  # reset our timer

    # rest of pygame code here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM