Introduction of the Project
Are you a coder bored of writing the same piece of code again and again for different projects? Want to make your final file easy to interpret and solve bugs? Then let’s create modules and reuse a piece of code. Code Reuse and Modularity in python with Pygame module will allow you to break your code into smaller functions or modules that can be reused as per the requirement.
So today, We will learn how we can create modules and reuse our code in other different files. Here, we will use PyGame Module for the display of text.
1. Python Interpreter is required to run the code; you can use VSCode or any python IDE.
2. PyGame Module needs to be installed.
Steps For Code Reuse And Modularity In Python With Pygame Module
Step 1: Install PyGame if you haven’t it in your system.
Paste the below line of command in your command prompt and press enter.
pip install pygame
Step 2: The below code is of the created module. Name it “module.py”.
# Defination to display text using PyGame def displaytext(t): # Import pygame in this module import pygame # Initialise PyGame pygame.init() # Initialising values to the colors white = (255, 255, 255) yellow = (255, 255, 143) brown = (165, 42, 42) # Assigning values to variable w = 1200 h = 400 # Background surface dimensions w x h backgroundsurface = pygame.display.set_mode((w, h)) # Name of the window pygame.display.set_caption('Display Text') # Type and size of the font font = pygame.font.Font('freesansbold.ttf', 24) # Text surface object, to draw text on it text = font.render(t, True, white, brown) # To create a rectangular object for the text surface object textRect = text.get_rect() # To set the center of the rectangular object textRect.center = (w // 2, h // 2) while True: # To fill the background surface with red color backgroundsurface.fill(yellow) # To copy display the surface object at the center backgroundsurface.blit(text, textRect) # Iterate over the list of Event objects that were returned by pygame.event.get() method for event in pygame.event.get(): # If the event object type is QUIT, if event.type == pygame.QUIT: # quit the pygame library pygame.quit() # quit the program quit() # Draws the surface object to the screen pygame.display.update()
Step 3: Paste the below code into a new python file, where we can import the above-created module. Place this file in the same directory as the above one. Name it as program.py
# Import the previously created python file by its name import module # Reuse the previously created module module.displaytext('This is a demonstration of Code Reuse and Modularity In Python With Pygame Module')
Explanation Of The Code
In the module.py file,
1. We have defined a definition for displaying text using the PyGame module.
2. In it, at first, we imported and then initialized the PyGame module.
3. Then, we initialized color codes for RGB colors and the height and width of the window.
4. After this, we have given a name to the window using the set_caption function and declared the type and size of the font to be used in the display text.
5. We are drawing the text on the text surface using the render function.
6. We are using a rectangle shape to display the text in it and fill the background with green color.
7. Then, we transfer the block of the text using the blit function.
8. If there is no text to be displayed, we are quitting the PyGame along with the program.
In the program.py file,
1. We are importing the module.py file using the import module.
2. Then, we are first calling the module name and then the method name using “.” in between.
After importing our created module into our main file, the output will look like the below image.
Things to Remember
- Save both the files in the same directory.
- Write the name of the first file in lower case to avoid errors.
Now, you can run the code for code reuse and modularity in python with the Pygame module. I hope you found it helpful. Go ahead and give it a shot on your own.
Cisco Ramon is an American software engineer who has experience in several popular and commercially successful programming languages and development tools.