Pygame is a python library that can be used specifically to design and build games. Pygame supports only 2d games that are built using different sprites. Pygame is not particularly best for designing games as it is very complex to use doesn’t have a proper GUI like unity but it definitely builds logic for further complex projects.
We’ll be creating a simple game with the following rules:-
- The player can only move vertically.
- Other than player block there will be two other blocks.
- One of them will be the enemy block and one of them will be score block.
- If the player collides with the enemy block then the game over screen pops up, if the player collides with the score block the score is incremented and it is compulsory to collect all score blocks.
We’ll be using various techniques such as the use of functions, random variables, various
pygame functions etc.
pygame library we need to install it. To install it type the below command in the terminal.
pip install pygame
After installing the pygame library we need to write the following lines to initialize the pygame library:-
import pygame pygame.init()
These lines are pretty self explanatory. The
pygame.init() function initiates the
Then we need to initialize the screen where we want to place our blocks. This can be done by writing following lines:-
res = (720, 720) screen = pygame.display.set_mode(res)
The tuple res holds two values that will define the resolution of our game. Then we need to define another variable screen that will actually act as our workbench. This can be done by using
pygame.display.set_mode((arg, arg)). The tuple (arg, arg) can be stored into a variable res to reduce processor load.
Now we need an actual screen to pop up when we run the code this can be done by a for and while loop in following way:-
while True: for ev in pygame.event.get(): if ev.type==pygame.QUIT: pygame.quit()
The while loop used here run till the condition is true. We define a variable ev that in
pygame.event. Now if the user clicks on the cross button of the window the condition is changed to false and the while loop ends killing the current window.
Below is the implementation.
- How to create Buttons in a game using PyGame?
- Building and visualizing Sudoku Game Using Pygame
- How to add color breezing effect using pygame?
- Python - Drawing design using arrow keys in PyGame
- Bubble sort visualizer using PyGame
- Tic Tac Toe GUI In Python using PyGame
- Binary Search Visualization using Pygame in Python
- Python | Display images with PyGame
- Python | Display text to PyGame window
- Python | Drawing different shapes on PyGame window
- Introduction to pygame
- Python | Making an object jump in PyGame
- Python | Playing audio file in Pygame
- Python - Moving an object in PyGame
- How to initialize all the imported modules in PyGame?
- Collision Detection in PyGame
- Adding Boundary to an Object in Pygame
- Interesting Facts about PYGAME
- Creating start Menu in Pygame
- Python implementation of automatic Tic Tac Toe game using random number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.