VOOZH about

URL: https://www.geeksforgeeks.org/python/8-bit-game-using-pygame/

⇱ 8-bit game using pygame - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

8-bit game using pygame

Last Updated : 28 Apr, 2025

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.
 

Installation


Before initializing 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 pygame library.
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. 
 

Output:
 


 

Comment
Article Tags: