Explanation of Pygame Time Time Control

  • 2021-12-13 08:25:30
  • OfStack

pygame. time Time Control Module is a module which is used frequently in Pygame. Its main function is to manage time and game frame rate (i.e. FPS).

Time plays a very important role in game development, such as the time spent releasing a certain skill, or the duration of playing animation and sound, which need time to manage. Another important function of time module is to control the game frame rate (i.e. FPS), which is a key indicator to evaluate whether the game picture is smooth or not. In 1 case, the FPS of the computer can reach the speed of 60 frames/s, which is enough for us to use. If the number of frames is less than 30, the game screen will become stuck.

Note that in Pygame, time is in milliseconds (1 second = 1000 milliseconds), which will make the game design more refined.

1) Game pauses

The Pygame. time module provides the following common methods, as shown in the following table:

方法 说明
pygame.time.get_ticks()  以毫秒为单位获取时间
pygame.time.wait() 使程序暂停1段时间
pygame.time.set_timer() 创建1个定时器,即每隔1段时间,去执行1些动作
pygame.time.Clock() 创建1个时钟对象来帮我们确定游戏要以多大的帧数运行

Here's a simple set of examples:


import pygame
pygame.init()
screen = pygame.display.set_mode((500,500))
pygame.display.set_caption('c Language Chinese Network ')
#  Gets the time in milliseconds 
t = pygame.time.get_ticks() # The time refers to the pygame After initialization, the calculation starts until the function is called 
t1 =pygame.time.wait(3000) # Pause the game 3000 Milliseconds 
print(t1)
# Suspend t1 Time, load the picture 
image_surface = pygame.image.load("C:/Users/Administrator/Desktop/c-net.png")
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
    screen.blit(image_surface,(0,0))
    pygame.display.update()

The above program will not load the picture until it is paused for 3 seconds

2) Set up the game FPS

To set the frame rate of the game (FPS), you can use the Clock () method, which provides the following common methods:

方法 说明
pygame.time.Clock.tick() 更新clock对象
pygame.time.Clock.get_time() 获取上1个tick中的时间
pygame.time.Clock.get_fps() 计算clock对象的帧率

Let's look at a set of simple application examples:


import pygame
pygame.init()
screen = pygame.display.set_mode((500,300))
pygame.display.set_caption('c Language Chinese Network ')
#  Gets the time in milliseconds 
t = pygame.time.get_ticks() # The time refers to the pygame After initialization, the calculation starts until the function is called 
t1 =pygame.time.delay(3000) # Pause the game 3000 Milliseconds 
print(t1)
# Suspend t1 Time, load the picture 
image_surface = pygame.image.load("C:/Users/Administrator/Desktop/c-net.png")
# Create a clock object that controls the game's FPS ) 
clock = pygame.time.Clock()
while True:
    # Through the clock object, specify the cycle frequency, cycle per second 60 Times 
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
    screen.blit(image_surface,(0,0))
    pygame.display.update()

Note: The effect of FPS (Game Frame Rate) can only be shown in dynamic graphics, but both static and dynamic graphics have the same usage rules.


Related articles: