Pygame.time.get_ticks
This function returns a time in milliseconds that has been running. For example, we could write the following code:
# importing pygame module
import pygame
# pygame initialization
pygame.init()
# change variable creation
change = 10
while change > 5:
# we will create osci variable to store the time
osci = pygame.time.get_ticks()
# we will print the osci
print(osci)
# decrement the value of var by one
change = change-1
# we will pause the script by .5 sec
pygame.time.wait(500)

You can also try this code with Online Python Compiler
Run Code
Output:
1383
1884
2387
2888
3390
Every iteration's time is printed, including when we paused the script in each iteration.
Pygame.time.delay
This function will stop the program from running temporarily; it uses CPU to improve the accuracy of the delay; it might seem pygame.time.delay and this function are the same, but they are not exactly. Pygame.time.delay uses sleeping while this uses CPU. As both the functions are nearly the same, by just changing the name, the sample code can be written the same way as the pygame.time.wait function:
# importing pygame
import pygame
# importing sys
import sys
# initializing pygame
pygame.init()
# creating display
canva = pygame.display.set_mode((500, 500))
# Creating image surface
picture = pygame.image.load('cn_logo.png')
# displaying image surface on display surface
canva.blit(picture, (100, 100))
# we will make the script wait for 1-second
pygame.time.wait(1000)
# bounding condition
while True:
# loop to keep an eye on events that are going on
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# we will update the display
pygame.display.flip()

You can also try this code with Online Python Compiler
Run Code
Output:

Pygame.time.Clock
This function makes a clock object that can keep track of time. Important points to be noted are
tick(): This method should be invoked once per frame. It will calculate the number of milliseconds since the previous call. If you pass the optional framerate argument, the function will delay the game, so it runs slower than the ticks per second specified. For example, if we give 20 as a parameter, the program will never run faster than 20 frames per second.
get_time(): It calculates the time difference between two ticks() in milliseconds.
get_fps(): It displays information about the clock frame rate. It returns a floating-point value as the output.
To explain this function, a simple program could be:
#Importing pygame
import pygame
#initializing pygame
pygame.init()
#assigning change variable to initial value
change = 8
#initializing display
display = pygame.time.Clock()
#running loop defining constrains
while change > 4:
display.tick(1)
#printing results
print(display.get_fps())
print(display.get_time())
change = change-1

You can also try this code with Online Python Compiler
Run Code
Output:
0.0
1007
0.0
1000
0.0
1001
0.0
1022
Frequently Asked Questions
What is Pygame?
Pygame is a collection of cross-platform Python modules for making video games. It offers sound and graphics libraries that can be utilized with the Python programming language. When using Pygame, we may need to conduct actions that require the use of time. For example, determining how long our program has been running, halting the program for a while, etc.
How does Pygame control time?
pygame.time.Clock function makes a clock object that can be used to keep track of time. The following are the numerous methods of the clock object: tick(): Once per frame, this method should be invoked. It will calculate the number of milliseconds since the previous call.
What is the purpose of pygame.time.clock?
This function makes a clock object that can be used to keep track of time.It contains various functionalities such as tick(),get_time() and get_fps().
Conclusion
In this article, we have extensively discussed the Pygame-Time; further, we have discussed its functions and uses in Pygame-Time.
After reading about the Pygame-Time, are you not feeling excited to read/explore more articles on the topic of Pygame? Don't worry; Coding Ninjas has you covered. To learn, see Operating System, Unix File System, File System Routing, and File Input/Output.
Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and Algorithms, Competitive Programming, JavaScript, System Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc., you must look at the problems, interview experiences, and interview bundle for placement preparations.
Nevertheless, you may consider our paid courses to give your career an edge over others!
Do upvote our blogs if you find them helpful and engaging!
Happy Learning!