pygame.time.wait
This function is used to pause the running of the program for few seconds. it takes time in milliseconds as parameter. For example to demonstrate this function we will write a simple program to make w3wiki logo appear on screen only after 5 seconds. The code for this will be:
# 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 milliseconds
pygame.time.wait(5000)
# creating a running loop
while True:
# creating a loop to check events that are occurring
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# updating the display
pygame.display.flip()
Output:
The output of this will be that the script will wait for 5 seconds and then update the display to show w3wiki logo.
It is slightly less accurate than pygame.time.delay which we will discuss later in this article because it uses sleeping but the other one uses the processor.
Pygame – Time
While using pygame we sometimes need to perform certain operations that include the usage of time. Like finding how much time our program has been running, pausing the program for an amount of time, etc. For operations of this kind, we need to use the time methods of pygame. In this article, we will be discussing the various methods that can be used for performing these operations.
The function that we will discuss are:-
- pygame.time.wait
- pygame.time.get_ticks
- pygame.time.delay
- pygame.time.Clock