Graphics in Python-Pygame

Using any modern graphics library is a useful exercise in coding. The library provides facilities that the programmer needs, but there is an implicit contract – the programmer has to use the library according to rules devised by its creator. In the case of Pygame, a set of initializations is needed, and it asks that you create an event loop that repeatedly looks for key presses and mouse clicks (events) many times per second. A program cannot predict when a mouse click will happen, so it must be ready at all times to receive one. In the case of Pygame, it breaks up every second into many parts (30 by default) and checks during each interval whether a mouse or keyboard action has taken place. If so, it attempts to alert the programmer by setting a flag that is related to the event. This has consequenc­es for the main program, mainly that it be a loop that repeatedly handles these events. In computer science, this action is called polling.

An important aspect of Pygame is that it creates a window and manages it. In C++, this can be a complicated proposition, because the operating system does not generally do this for you. Each window on the screen is something managed by a program, which has to figure out how much of the window is visible based on everything else that is on the screen.


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 *