You can’t use the time.sleep()
because it will stop the entire execution of the program. Instead, always let the controller return to your event loop, and do everything you can through events. You can’t let the program ever stop, because it always has to be redesigning the screen x times per second. So whatever you’re going to do, you have to do slowly and let the loop run to continue.
A very clean way of doing it is to use the pygame.time.set_timer()
which allows you to place an event of your own in the pygame event queue after a certain time.
Since you didn’t share full code, I wrote an example, to jump with the w key:
def trata_eventos():
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K.w:
personagem.pular()
# programa o pygame para gerar o evento daqui a 5 segundos
pygame.time.set_timer(pygame.USEREVENT+1, 5000)
if event.type == pygame.USEREVENT+1:
# evento disparou! desabilita o evento:
pygame.time.set_timer(pygame.USEREVENT+1, 0)
personagem.cair()
def desenha_tela(): # desenha o quadro atual
# ... desenha o fundo, e todos os personagens em sua posicao atual ...
personagem.desenhar()
pygame.display.flip()
while True: # loop principal; cada repetição é um quadro a ser desenhado
trata_eventos()
desenha_tela()
clock.tick(60) # espera para manter 60FPS