In this article we want to learn How to Install Pygame in Linux & Windows, first of all let’s talk about Pygame.
What is Pygame ?
Pygame is free and open source library for creating games and multimedia applications using Python programming language. Using Pygame you can create different games functionalities such as creating and handling images, animations, sound effects and musics. it also provides an event based framework for input handling (keyboard, mouse and joystick events) and support for different game related tasks like collisions, game logic and game physics.
Pygame is one of the best choice for beginners who want to create 2D games, because it id easy and simple. with Pygame you can create different types of games, from simple arcade games to more advanced games with nice graphics and sound effects.
How to Install Pygame in Linux?
Open terminal, and type following command to install the Pygame library:
1 |
pip3 install pygame |
How to Install Pygame in Windows?
You can install Pygame in Window using pip command, open your terminal or command prompt and write this command.
1 |
pip install pygame |
Now let’s create a simple example on Pygame, this is simple example of how to create a window using Pygame:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import pygame # Initialize Pygame pygame.init() # Set the window size window_size = (400, 300) # Create the window screen = pygame.display.set_mode(window_size) # Run game loop running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Update screen pygame.display.update() # Quit Pygame pygame.quit() |
This code creates a window with size of 400 x 300 pixels. the game loop checks for the QUIT event, and it is waiting for the user to close the window, and sets the runing flag to False to exit the loop and quit the game. update function is called to update the screen with any changes made in the game loop.
Run the code and this will be the result
FAQs:
Is pygame already in Python?
No, Pygame is not included with Python by default. Pygame is a separate library that provides functionality for creating games and multimedia applications in Python. if you want to use Pygame and you want to create games in Python, then you need to install Pygame and you can use pip for the that.
What popular games use pygame?
Pygame has been used to create different popular games across different genres. Some well-known games developed with Pygame are:
- A Virus Named TOM: A puzzle game where players control a virus named Tom trying to infect a computer system.
- Darwinia: A real-time strategy game that combines elements of strategy, action and puzzles.
- flOw: An experimental game where players control an aquatic microorganism navigating through different stages of evolution.
- Frets on Fire: A music game inspired by Guitar Hero, where players use their keyboard to play along with songs.
These are just a few games, there are different projects that they are using Pygame library.
Subscribe and Get Free Video Courses & Articles in your Email