Making apps with Pygame
In this article, we will see how we can make a simple app with Pygame.
Pygame is a library for python that helps us to build multimedia applications (Apps) and games. This library works as a wrapper for Simple DirectMedia Layer (SDL).
- Create the app class
- Add text class.
- Add Screen class
- Set Different windows.
What are Classes?
A class is a template-like entity for creating objects. These class objects consist of varied behavior associated with them. In Pygame, we have different types of classes available they are App class, Text Class, and Scene Class. These in general have their own behavior and properties and can be used for varied purposes.
App class is the basis for a game template or any application that we are building. This class can also be called the base or the formation of an application or game. It allows various functionality like resizable, fullscreen, and no frame mode in Pygame thus it becomes the necessary class while declaring or building any app or game via Pygame in python.
Steps needed to form a base App class:
- Importing the Pygame module.
- Now, we initialize the App class.
- Declare the main class and run the app class.
While building our application sometimes we want to display some messages for that, we required Text class. It is the template in which we can build different types of text formats in our window. This class mainly instantiates text objects of our window. So we can say if we want to play with our text then go for Text class.
Steps needed to form a Text class:
- Import the Pygame module and all the libraries from the Pygame.
- Now declare the text class and its functions like font_set, rendering, and previously used app class (As app class is the base class for all the other multimedia classes).
- Finally, declare the main class for calling the run function of the app class that will run the test class feature in it.
In games we have different screens, they might be the Intro scene, Game over the scene, or Level scene. In all these, we see different backgrounds or scenes. This all can be done with the help of the Scene class.
Steps needed to form a Scene class:
- The first step is to declare the scene class. By doing this, we actually create a container in which we can store all the necessary features required for our game.
- Now we have to append multiple scenes to the applications scene list a container.
- After appending, we will add the scene id and the color for the multiple scenes. We will add a scene id so that we can easily call each scene according to the scene id.
- Now it will flip each node and finally display the screen.
- Representation of the scene is followed by their unique id given above.
Press the space bar to change the scene
Different Windows modes
There are three different modes of a window in Pygame in which an active window can be displayed they are:
- Fullscreen mode(As its name suggests it is a window with its full resizable capability)
- Resizable mode(resize is allowed in this type of mode)
- No frame mode(In this type of mode no title bar is present in the window)
Steps needed to set up these modes:
- First, we declare screen size and display mode flags inside the initialized app class method.
- Now we will create a screen surface for it
- Creating surface object
# for FULLSCREEN mode display_surface = pygame_screen.display.set_mode((500,250), FULLSCREEN)
# for RESIZABLE mode display_surface = pygame_screen.set_mode((500, 250), RESIZABLE)
# for NOFRAME mode display_surface = pygame_screen.display.set_mode((500, 250), NOFRAME)
- Now run the main loop to execute the QUIT and Update command for the screen
Code for the above modes:
Node act as a container for graphical user Interface elements and objects. Node placement in Pygame requires four parameters that are:
- size: this helps to find the current size of the node on the screen
- pos: this helps to find the current position of the node
- dir: dir is a short name for direction. It itself consists of 3 parameters that are vertical, horizontal, and diagonal.
- gap: This refers to the space given in between two nodes in a Pygame.
Please Login to comment...