Loading…

Pygame and game development

What is Pygame

Pygame is a library of modules for the Python language, created for the development of 2D games. Pygame can also be called a framework. In programming, the concepts of “library” and “framework” are somewhat different. But when it comes to the classification of a particular instrument, everything is not so simple.

In any case, the framework is more powerful in comparison with the library, it imposes its own specifics on the features of programming and the scope of use of the product. In terms of specifics, Pygame is a framework. However, it can hardly be called a “powerful tool.” In its volume and functionality, it is rather a library.

There is also the concept of “game engine” as a software environment for developing games. According to its purpose, Pygame can be considered a game engine. At the same time, in terms of software classification, Pygame is the API for Python to the SDL library API.

An API is an interface (mainly a set of functions and classes) for application (often higher-level) programming, which is provided, for example, by one or another library. SDL is a library that works with computer multimedia devices.

In this sense, Pygame can be compared to Tkinter, which, through its functions and classes, provides Python with access to the Tk graphics library.

Official website: https://www.pygame.org

Official documentation: https://www.pygame.org/docs/.

Game development features

Games are event-oriented, just like any GUI application. Therefore, no, but games could be written with the help of Tkinter, in particular, on its copies of the canvas. But since the main purpose of the graphical user interface library is completely different, we would have to reinvent the wheel. At the same time, a library specially designed for writing games already contains the necessary objects, which simplifies development.

For example, to determine whether two objects collide, you need to write code that checks the coincidence of coordinates. This can be a daunting task, as it is necessary to take into account areas of overlap, the shape of objects, etc. At the same time, the game engine can include a ready function for checking collisions (collisions) with the necessary configuration options.

With all this, Pygame is a fairly low-level game engine, if you can call it that. This means that much in it does not remain behind the scenes, but is given to the programmer for revision, forcing him to understand how the “gears” work. So in Pygame there is no emulation of physical phenomena. If you need to simulate motion with acceleration or along an arc, program it yourself, first taking the appropriate formula from the physics course.

Games are multimedia applications. However, unlike other applications of this group, they are characterized by complex program logic and often a lot of mathematics, although fairly simple, plus the emulation of physical phenomena. In games, the similarity of artificial intelligence is programmed. In a multiplayer game, although users play with each other, and not with AI, virtual worlds are created that exist according to the laws laid down by the developers.

In the game source code, there are three logical blocks:

  1. Tracking of events produced by the user and not only them.

  2. Changing the state of objects, according to the events.

  3. Display objects on the screen, according to their current states.

 These three stages repeat endlessly in the loop during the game is launched.

The Pygame place among other game development environments

Is the pygame library popular, or are programmers writing games on it? Although there are popular games on Pygame, in the overwhelming case – no. For programming under android and desktop, there are more functional game engines.

For creating two-dimensional browser games, indie developers (from the word independent – independent, here understood as “lone”, “not working in a team or for a company”) often use JavaScript and its gaming libraries, since JS is a web-native language. Although there are translation projects from Python to JavaScript (https://github.com/jggatc/pyjsdl).

To run python applications on Android, see https://github.com/kivy/python-for-android and https://github.com/duducosmos/pgs4a.

What is the advantage of Pygame then? It is in easy entry into the industry and prototyping. Pygame is a small library. Python itself allows you to write short and clear code. So this is a good start to get acquainted with the features of game development. More experienced programmers can use Pygame to quickly create a prototype of the game to see how things work. After that, the program is rewritten in another language. In other words, Pygame has the advantage of easy learning and quick development.

After Pygame, the life of a Python game developer does not end there. Should look towards Kivy (https://kivy.org). This is a full-fledged framework that allows you to write not only gaming applications in Python. It is more focused on mobile platform development.

The most effective method to install Pygame

Pygame is excluded in the standard Python library, that is, it isn’t provided with the establishment bundle, yet requires a separate establishment. In Ubuntu and related dissemination, this should be possible in two different ways – utilizing pip and well-suited get:

python3 -m pip install -U Pygame --user

or

sudo apt-get install python3-pygame

 

For Windows: 

py -m pip install -U pygame --user

In order to check whether everything was installed properly:

python3 -m pygame.examples.aliens

For Windows, instead of ‘python3’, you need to specify ‘py’. The game aliens will be launched and included in the examples module of the Pygame library.


Leave a Comment