Ho iniziato a usare pygame e voglio fare un gioco semplice. Uno degli elementi di cui ho bisogno è il conto alla rovescia. Come posso fare il conto alla rovescia (es. 10 secondi) in PyGame?Timer conto alla rovescia in Pygame
risposta
In questa pagina troverete quello che stai cercando per http://www.pygame.org/docs/ref/time.html#pygame.time.get_ticks
Si scarica zecche una volta prima di iniziare il conto alla rovescia (che può essere un trigger nel gioco - l'evento chiave, a prescindere). Per esempio:
start_ticks=pygame.time.get_ticks() #starter tick
while mainloop: # mainloop
seconds=(pygame.time.get_ticks()-start_ticks)/1000 #calculate how many seconds
if seconds>10: # if more than 10 seconds close the game
break
print (seconds) #print how many seconds
Ci sono diversi modi che si possono fare questo- eccone uno. Python non ha un meccanismo per gli interrupt per quanto ne so.
import time, datetime
timer_stop = diatomite.datetime.utcnow() +datetime.timedelta(seconds=10)
while True:
if datetime.datetime.utcnow() > timer_stop:
print "timer complete"
break
Un altro modo semplice è utilizzare semplicemente il sistema di eventi di pygame.
Ecco un semplice esempio:
import pygame
pygame.init()
screen = pygame.display.set_mode((128, 128))
clock = pygame.time.Clock()
counter, text = 10, '10'.rjust(3)
pygame.time.set_timer(pygame.USEREVENT, 1000)
font = pygame.font.SysFont('Consolas', 30)
while True:
for e in pygame.event.get():
if e.type == pygame.USEREVENT:
counter -= 1
text = str(counter).rjust(3) if counter > 0 else 'boom!'
if e.type == pygame.QUIT: break
else:
screen.fill((255, 255, 255))
screen.blit(font.render(text, True, (0, 0, 0)), (32, 48))
pygame.display.flip()
clock.tick(60)
continue
break
Questo è in realtà abbastanza semplice. Grazie a Pygame per aver creato una semplice libreria!
import pygame
x=0
while x < 10:
x+=1
pygame.time.delay(1000)
Questo è tutto quello che c'è da fare! Divertiti con pygame!
pygame.time.Clock.tick
restituisce il tempo in millisecondi dall'ultimo clock.tick
chiamata (delta time, dt
), in modo da poter usare per aumentare o diminuire una variabile timer.
import pygame as pg
def main():
pg.init()
screen = pg.display.set_mode((640, 480))
font = pg.font.Font(None, 40)
gray = pg.Color('gray19')
blue = pg.Color('dodgerblue')
# The clock is used to limit the frame rate
# and returns the time since last tick.
clock = pg.time.Clock()
timer = 10 # Decrease this to count down.
dt = 0 # Delta time (time since last tick).
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
timer -= dt
if timer <= 0:
timer = 10 # Reset it to 10 or do something else.
screen.fill(gray)
txt = font.render(str(round(timer, 2)), True, blue)
screen.blit(txt, (70, 70))
pg.display.flip()
dt = clock.tick(30)/1000 #/1000 to convert to seconds.
if __name__ == '__main__':
main()
pg.quit()
Prova questo:
import time
counter = 10
while counter != 0:
print(counter)
counter -= 1
time.sleep(1)
Questo ha fondamentalmente una variabile che 1 viene sottratto da ogni volta che loop. Quindi usiamo time.sleep (1) per aspettare un secondo.
'time.sleep' di solito non è una buona soluzione perché rende il gioco non rispondente e blocca l'esecuzione di altri codici. – skrx
il ritardo non funziona per determinati scenari perché non ritardare un oggetto specifico ma l'intero gioco. –