Pygame – Game Module for Python
Python is a language which is easy to study and program. Development of games in Python is a easy job, as it provide a specialized game module named pygame. With usual Python modules we can develop only textual games. For the development of graphical games the pygame module is needed.
Let us see the common usage of this module with the help of some small examples.
There are two things to be imported for the use of pygame in graphical game development.
The first step to do is to set up up pygame.
Next step is to set up the window for the game. An object is created, for further use, deployment of some images to the surface can be made using this object
Color is given as a tuple with three elements. As it is tedious step to type the full tuple every time, we can create a constant which holds this tuple and can be used whenever needed.
If we want to add a text to the window, first we have to select a font style and a font size.
None gives the default font style and 48 gives the size. In-order to set up the text
An object of the text, textRect, is made.
To fill the background with a specific color
Geometrical figures can be drawn easily, a blue filled circle can be drawn as
In-order to print the text we have created to the window we have to use the keyword blit.
Until now nothing will be displayed on the screen. It will take time to draw something to screen. Thus if we try to draw on the surface each time we create a new object it will create a big delay. In-order to avoid this delay all the updates are written to the screen after a update keyword is used.
A game loop runs until the user explicitly tells to terminate. All the events occurred during a single iteration can be obtained using
If an explicit signal to quit occurs the window is shutdown.