📜  Pygame – 时间

📅  最后修改于: 2022-05-13 01:55:28.974000             🧑  作者: Mango

Pygame – 时间

在使用 pygame 时,我们有时需要执行某些操作,包括时间的使用。比如查看我们的程序已经运行了多少时间,程序暂停了多少时间等等。对于这种操作,我们需要使用pygame的time方法。在本文中,我们将讨论可用于执行这些操作的各种方法。

我们将讨论的函数是:-

  • pygame.time.wait
  • pygame.time.get_ticks
  • pygame.time.delay
  • pygame.time.Clock

pygame.time.wait

该函数用于暂停程序运行几秒钟。它需要以毫秒为单位的时间作为参数。例如,为了演示此函数,我们将编写一个简单的程序,使 geeksforgeeks 徽标仅在 5 秒后出现在屏幕上。代码如下:

Python
# importing pygame module
import pygame
 
# importing sys module
import sys
 
# initialising pygame
pygame.init()
 
# creating display
display = pygame.display.set_mode((500, 500))
 
# Creating the image surface
image = pygame.image.load('gfg_logo.png')
 
# putting our image surface on display surface
display.blit(image,(100,100))
 
# making the script wait for 5000 seconds
pygame.time.wait(5000)
 
# creating a running loop
while True:
   
      # creating a loop to check events that are occuring
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    # updating the display
    pygame.display.flip()


Python
# importing pygame module
import pygame
 
# initialising pygame
pygame.init()
 
# creating a variable
i=0
while i<5:
      
    # storing the time in ticks variable
    ticks=pygame.time.get_ticks()
     
    # printing the variable ticks
    print(ticks)
     
    # increasing the value of i by 1
    i=i+1
     
    # pausing the script for 1 second
    pygame.time.wait(1000)


Python
# importing pygame module
import pygame
 
# importing sys module
import sys
 
# initialising pygame
pygame.init()
 
# creating display
display = pygame.display.set_mode((500, 500))
 
# Creating the image surface
image = pygame.image.load('gfg_logo.png')
 
# putting our image surface on display surface
display.blit(image,(100,100))
 
# making the script wait for 5000 seconds
pygame.time.delay(5000)
 
# creating a running loop
while True:
   
      # creating a loop to check events that are occuring
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    # updating the display
    pygame.display.flip()


Python
# importing the pygame module
import pygame
 
# initialising the pygame
pygame.init()
 
# declaring a variable i with value 0
i=0
 
# creating a clock object
clock=pygame.time.Clock()
 
# creating a loop for 5 iterations
while i<5:
       
    # setting fps of programm to max 1 per seconf
    clock.tick(1)
     
    # printing time used in the previous tick
    print(clock.get_time())
     
    # printing compute the clock framerate
    print(clock.get_fps())
    i=i+1



输出:



输出结果是脚本将等待 5 秒,然后更新显示以显示 geeksforgeeks 徽标。

它比 pygame.time.delay 稍微差一点,我们将在本文后面讨论,因为它使用睡眠,而另一个使用处理器。

pygame.time.get_ticks

这个函数给出了一个以毫秒为单位运行的时间。比如我们要写一个简单的代码来演示这个例子,可以是:

Python

# importing pygame module
import pygame
 
# initialising pygame
pygame.init()
 
# creating a variable
i=0
while i<5:
      
    # storing the time in ticks variable
    ticks=pygame.time.get_ticks()
     
    # printing the variable ticks
    print(ticks)
     
    # increasing the value of i by 1
    i=i+1
     
    # pausing the script for 1 second
    pygame.time.wait(1000)


输出:



每次迭代都会打印时间,包括我们在每次迭代中暂停脚本的时间。

pygame.time.delay

该函数的工作原理与 pygame.time.wait函数相同,不同之处在于该函数将使用处理器(而不是休眠)以使延迟更准确。示例代码可以写成与 pygame.time.wait函数相同的代码,只需替换名称即可:

Python

# importing pygame module
import pygame
 
# importing sys module
import sys
 
# initialising pygame
pygame.init()
 
# creating display
display = pygame.display.set_mode((500, 500))
 
# Creating the image surface
image = pygame.image.load('gfg_logo.png')
 
# putting our image surface on display surface
display.blit(image,(100,100))
 
# making the script wait for 5000 seconds
pygame.time.delay(5000)
 
# creating a running loop
while True:
   
      # creating a loop to check events that are occuring
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    # updating the display
    pygame.display.flip()

输出:

pygame.time.Clock

此函数用于创建可用于跟踪时间的时钟对象。时钟对象的各种方法如下:

演示此函数的简单程序可以是:

Python

# importing the pygame module
import pygame
 
# initialising the pygame
pygame.init()
 
# declaring a variable i with value 0
i=0
 
# creating a clock object
clock=pygame.time.Clock()
 
# creating a loop for 5 iterations
while i<5:
       
    # setting fps of programm to max 1 per seconf
    clock.tick(1)
     
    # printing time used in the previous tick
    print(clock.get_time())
     
    # printing compute the clock framerate
    print(clock.get_fps())
    i=i+1


输出:

由于我们在 tick 方法中传递了 1,它将最大 fps 设置为 1。这导致每帧之间的时间接近 1000 毫秒。