- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Allowing resizing window in PyGame
Introduction
Pygame is the module used for game development in Python; it is considered to be one of the most effective modules for this purpose. The development of video games can not only be profitable in today's market but also serve as a medium for educational and promotional purposes. Creating games requires knowledge of mathematics, logic, physics, artificial intelligence, and a lot of other subjects, yet it can be really enjoyable.
We will discuss in detail what is Pygame, how to implement a normal PyGame window and how to allow users to resize the window with a working example.
PyGame
Pygame is a collection of Python modules that can be used to program video games on a variety of different platforms. It is made up of graphics and sound libraries for computers that were developed specifically for use with the Python programming language.
Pete Shinners is the official author of Pygame, which was created to replace PySDL.Pygame is an excellent choice for developing client-side apps that have the possibility of being compiled as a standalone executable.
Installation of PyGame
Sometimes the error occurs while executing the program. One of the reasons could be that PyGame is not installed. To install PyGame you need to run the following code in the terminal window or you can run the code in Jupyter Notebook to install PyGame.
pip install pygame
If we run the code in Jupyter notebook or Jupyter Lab it will give the following output which says that the PyGame package.
Collecting pygame Downloading pygame-2.1.2-cp39-cp39-win_amd64.whl (8.4 MB) ---------------------------------------- 8.4/8.4 MB 7.4 MB/s eta 0:00:00 Installing collected packages: pygame Successfully installed pygame-2.1.2 Note: you can need to restart the kernel to use updated packages.
Pre-requisites
You need to have some knowledge of Python language as the code that we will be writing is in python language.
You need to have software that can execute python code and its libraries.
Normal PyGame window (Not resizeable)
Algorithm
Step 1 − First and most important step is to import pygame
Step 2 − Set the title for the window and add the content to the window.
Step 3 − Run pygame.
Step 4 − Quit pygame after closing the window else the window will not get closed.
Program example for normal pygame window
import pygame # We are creating a screen with size 350*350 which is not resizeable scr = pygame.display.set_mode((350, 350)) # Now set the title for the window pygame.display.set_caption('Not_resizable_window') # Here we run the window running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # quit pygame after closing the window else the window will not get closed. pygame.quit()
Output
It is a non-resizable pygame window or we can say a normal pygame window. If we click on the cross button, we will quit pygame because of pygame.quit() which is there in our code.
Resizeable PyGame window
Algorithm
Step 1 − First and most important step is to import pygame
Step 2 − Use the pygame.display.set mode() function to create a screen, and then make it possible to resize the screen using pygame. MODIFIABLE in size.
Step 3 − Set the title for the window and add the content to the window.
Step 4 − Run pygame.
Step 5 − Quit pygame after closing the window else the window will not get closed.
Program example for resizable pygame window
import pygame # We are creating a screen with size 550*550 which is resizeable scr = pygame.display.set_mode((550, 550),pygame.RESIZABLE) # Now set the title for the window pygame.display.set_caption('Resizable_window') # Here we run the window running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # quit pygame after closing the window else the window will not get closed. pygame.quit()
Output
The only difference between both the code is that we have added a property while declaring the size of the window which will allow the user to resize the pygame window according to their preferences.
scr = pygame.display.set_mode((550, 550), pygame.RESIZABLE)
pygame.RESIZABLE allows us to resize the window.
Conclusion
In this article, we have learned about what is pygame in python and some history behind it. We have learned about how to install pygame and what are the prerequisites before starting with the program. We have also practiced a normal pygame window and a resizable pygame window.