The Event LOOP in Python

Here is a simple program that displays a drawing created by Pygame. Don’t worry about the details just now. This program draws a straight line:

import pygame

screen = pygame.display.set mode((1000, 700))

pygame.draw.line(screen, (0,0,0), 10,10), (200,200), 2)

pygame.display.update()

The variable screen is initialized, and the line is drawn using the method py- game.draw.line. This works, but the program terminates after update is called, which closes the window. The line is only on the screen for a tiny fraction of a second. There needs to be a time delay that permits the user to see the result. One way is to place the code inside an infinite loop, and that the program never ends. For example,

import pygame

screen = pygame.display.set mode((1000, 700))

while True:

pygame.draw.line(screen, (0,0,0),   (10,10), (200,200), 2)

pygame.display.update()

Here, the line is drawn and then update is repeatedly called within the loop. This means that the line is drawn and the screen is updated many times a sec­ond. That’s because the program does not end and the window stays open. This solution is unsatisfactory, as it uses CPU cycles for no productive reason, which can slow down the entire computer system. Fortunately, there is a better option. Pygame gives us the ability to wait, that is, to give up the CPU to other processes, using the time class.

Time consists of a set of time related functions, the most useful of which for the purposes here is probably Clock.tick. It waits until a specific time interval has passed since the last time tick has been called. It has one parameter, which is the number of times per second a tick can occur. Here, the parameter is 1/sec, where sec is the minimum number of seconds of delay is wanted. As a practical example, the loop above could be rewritten to use tick as follows:

import pygame import pygame.time

clock = pygame.time.Clock()

screen = pygame.display.set mode((1000, 700))

while True:

clock.tick(10)

pygame.draw.line(screen, (0,0,0),  (10,10), (200,200), 2)

pygame.display.update()

This is very typical of the main loop in a Pygame program. An instance of Clock is created (named clock) so that tick can be called, and it allows the loop to execute 10 times per second. Each call to tick ensures that no less than 1/10 of a second has passed since the previous call. We can think of it as meaning “wait until the next clock tick.” The behavior is critical for the functioning of a game, which updates the screen every fraction of a second. It is also what will allow the paint program to operate interactively.

To be clear, the use of tick allows us to release the CPU and allow other pro­cesses on the computer to use it. After at least the period specified has passed, but not necessarily exactly that time, the CPU will be given back to the program and it will resume executing.

 

Source: Parker James R. (2021), Python: An Introduction to Programming, Mercury Learning and Information; Second edition.

Leave a Reply

Your email address will not be published. Required fields are marked *