pygame.time
pygame module for monitoring time
— get the time in milliseconds
— pause the program for an amount of time
— pause the program for an amount of time
— repeatedly create an event on the event queue
— create an object to help track time

PYGAME中的时间以毫秒(1/1000秒)表示。大多数平台的时间分辨率有限,约为10毫秒。此分辨率(以毫秒为单位)在 TIMER_RESOLUTION 常量。

pygame.time.get_ticks()
get the time in milliseconds
get_ticks() -> milliseconds

返回以下时间的毫秒数 pygame.init() 被召唤了。在初始化PYGAME之前,该值始终为0。

pygame.time.wait()
pause the program for an amount of time
wait(milliseconds) -> time

将暂停给定的毫秒数。此函数使进程休眠,以便与其他程序共享处理器。即使等待几毫秒的程序也会消耗很少的处理器时间。它的精确度比 pygame.time.delay() 功能。

它返回实际使用的毫秒数。

pygame.time.delay()
pause the program for an amount of time
delay(milliseconds) -> time

将暂停给定的毫秒数。此函数将使用处理器(而不是休眠),以使延迟比 pygame.time.wait() 。

它返回实际使用的毫秒数。

pygame.time.set_timer()
repeatedly create an event on the event queue
set_timer(event, millis) -> None
set_timer(event, millis, loops=0) -> None

将事件设置为每隔给定毫秒数在事件队列中显示一次。第一个事件将不会出现,直到该时间量过去。

这个 event 属性可以是 pygame.event.Event 对象或表示事件的整数类型。

loops 是一个整数,表示发布的事件数。如果为0(默认),则事件将继续发布,除非显式停止。

若要禁用此类事件的计时器,请使用相同的事件参数再次调用该函数 millis 参数设置为0。

还值得一提的是,特定事件类型只能放在计时器上一次。换句话说,同一事件类型不能有两个计时器。为特定事件设置事件计时器会丢弃该事件类型的旧计时器。

loops 取代了 once 参数,并且这不会破坏向后兼容性

New in pygame 2.0.0.dev3: 一旦添加了论点。

Changed in pygame 2.0.1: 事件参数支持 pygame.event.Event 对象

New in pygame 2.0.1: 添加了循环参数以替换一次参数

pygame.time.Clock
create an object to help track time
Clock() -> Clock
— update the clock
— update the clock
— time used in the previous tick
— actual time used in the previous tick
— compute the clock framerate

创建可用于跟踪时间量的新时钟对象。时钟还提供了几个功能来帮助控制游戏的帧速率。

tick()
update the clock
tick(framerate=0) -> milliseconds

此方法应每帧调用一次。它将计算自上次调用以来已过了多少毫秒。

如果您传递可选的Framerate参数,该函数将延迟以使游戏的运行速度低于每秒给定的滴答数。这可用于帮助限制游戏的运行时速度。通过呼叫 Clock.tick(40) 每帧一次,该程序的运行速度永远不会超过每秒40帧。

请注意,此函数使用的SDL_Delay函数并不是在所有平台上都准确,但占用的CPU不多。如果您想要一个准确的计时器,并且不介意占用CPU,请使用TICK_BUSY_LOOP。

tick_busy_loop()
update the clock
tick_busy_loop(framerate=0) -> milliseconds

此方法应每帧调用一次。它将计算自上次调用以来已过了多少毫秒。

如果您传递可选的Framerate参数,该函数将延迟以使游戏的运行速度低于每秒给定的滴答数。这可用于帮助限制游戏的运行时速度。通过呼叫 Clock.tick_busy_loop(40) 每帧一次,该程序的运行速度永远不会超过每秒40帧。

请注意,此函数使用 pygame.time.delay()pause the program for an amount of time ,它在繁忙的循环中使用大量的CPU以确保计时更准确。

New in pygame 1.8.

get_time()
time used in the previous tick
get_time() -> milliseconds

前两次调用之间经过的毫秒数 Clock.tick() 。

get_rawtime()
actual time used in the previous tick
get_rawtime() -> milliseconds

类似于 Clock.get_time() ,但不包括在以下时间使用的任何时间 Clock.tick() 正在推迟限制帧速率。

get_fps()
compute the clock framerate
get_fps() -> float

计算游戏的帧速率(以每秒的帧为单位)。它是通过将最后十个调用平均到 Clock.tick() 。




Edit on GitHub