[英]How do I use the clock/ticks function in Pygame to spawn another object after a certain amount of time?
[英]How do I delay a function by X amount of ticks?
好的,所以我有一個按下鍵時會被調用的鈎子,例如空格鍵。 我想做的是,當發生這種情況時,我想調用一個函數。 下一個滴答,我想調用其他函數。
import pygame
def func1():
do_things
def func2():
do_other_things
while True:
for event in pygame.event.get():
if event.type == pygame.K_SPACE:
func1()
#wait(1, func2)
顯然,#wait被替換為,但是可能會延遲1滴答。
有沒有辦法做到這一點? 我猜想它要么是我錯過的基本功能,要么是令人難以置信的復雜。
func2timer = -1
while True:
if func2timer >= 0:
func2timer -= 1
if func2timer == 0:
func2()
for event in pygame.event.get():
if event.type == pygame.K_SPACE:
func1()
func2timer = 1
(如果您想等待更多的滴答聲,可以將計時器調整為大於1)
使用變量來標記是否在時間到時調用func2
:
shouldDoFunc2 = false
while True:
if shouldDoFunc2:
func2()
shouldDoFunc= false
for event in pygame.event.get():
if event.type == pygame.K_SPACE:
func1()
shouldDoFunc2()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.