# Clock

# schedule(callback, delay)

设置定时器,延迟 delay 秒调用 callback 函数。

from cpgzero import *
game_end = False
def game_over():
    global game_end
    game_end = True

clock.schedule(game_over, 60)

# schedule_interval(callback, interval)

每隔 interval 秒,调用一次 callback 函数。

from cpgzero import *
time = 30
def countdown():
    global time
    if time <= 0:
        clock.unschedule(countdown)
    else:
        time -= 1
    
clock.schedule_interval(countdown, 1)

# schedule_unique(callback, delay)

先取消 callback 的定时器,然后重新设置定时器,延迟 delay 秒调用 callback 函数。

from cpgzero import *
def game_over():
    print("game_over")
clock.schedule(game_over, 60) # 定时器取消
clock.schedule_interval(game_over, 1) # 定时器取消
clock.schedule_unique(game_over, 5) # 调用

# unschedule(callback)

取消 callback 的定时器。

from cpgzero import *
def game_over():
    print("game_over")
clock.schedule(game_over, 60) # 定时器取消
clock.schedule_interval(game_over, 1) # 定时器取消
clock.unschedule(game_over)