Table of contents
1.
Introduction
2.
Pygame.time.wait
3.
Pygame.time.get_ticks 
4.
Pygame.time.delay
5.
Pygame.time.Clock
6.
Frequently Asked Questions
6.1.
What is Pygame?
6.2.
How does Pygame control time?
6.3.
What is the purpose of pygame.time.clock?
7.
Conclusion 
Last Updated: Mar 27, 2024

Pygame Time

Author Tarun Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, we will learn about Pygame-Time. But first, we need to know what Pygame is? 

So, Pygame is a collection of cross-platform Python modules used for making video games. It gives sound and graphics libraries to 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. We need to use Pygame- time methods for actions like this. 

We shall explore the following functions:

  1. pygame.time.wait
  2. pygame.time.get_ticks
  3. pygame.time.delay
  4. pygame.time.Clock

Pygame.time.wait

This function stops the program from running for a desired amount of time. We have to pass the argument time in milliseconds. We'll create a simple program that displays the Coding Ninja's logo on the screen after 10 seconds to demonstrate this function.

//code

# pygame module import
import pygame

# sys module import
import sys

# initializing pygame
pygame.init()

# display creation
canvas = pygame.display.set_mode((400, 400))

# image surface creation
picture = pygame.image.load('cn_logo.png')

# putting our picture surface on display surface
canvas.blit(picture, (200, 200))

# setting 10000 milliseconds waiting time
pygame.time.wait(10000)

# running loop creation
while True:

 # loop creation to check events that are occurring
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
            
    # display updation
    pygame.display.flip()
You can also try this code with Online Python Compiler
Run Code

 

Output:

pygame.time.wait will cause the script to wait 10 seconds before updating the display to show the Coding Ninja logo. It's a little less precise than pygame.time.

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 SystemUnix File SystemFile System Routing, and File Input/Output.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem 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 problemsinterview 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!

Live masterclass