PyGame: A Game In Python

Pygame is a library of python programming language. It is an open-source library for making multimedia applications like games that are built on the top of the SDL library. Pygame requires Python language to be installed in the system. Pygame is highly portable and runs nearly on all platforms and operating systems. Millions of peoples have downloaded Pygame itself, which is a whole lot of bits flying across interwebs. Pygame.org website welcomes all game, video, image, audio, and multimedia applications. Once you have finished getting the start, you could add a fresh project or learn Pygame by reading the docs.

Introduction to PyGame- first step

Before we start here, you must have some basic knowledge of Python. Those who are not aware of the basics must do so- take a tutorial.
For getting started you just need a simple code editor, notepad++, Visual Studio Code anything is more than enough. But you can also go with some powerful IDE, for example my favourite, JetBrains PyCharm. The community edition is also very very powerful and it is absolutely free.

Creating our Game Window

Now let’s do a little coding, and create our very first game window. Excited?. Yes. So let’s begin.
  • Create a file named game.py anywhere in your computer. (But it is a good practice to organize your files by keeping them in separate folder, so I would tell that create a folder for your game project).
  • Inside the file game.py write the following code:
import pygame 
pygame.init()
game = pygame.display.set_mode((600,400)) 
pygame.display.set_caption(“My First Game”)
input(“Enter anything…”)

  • Now, you can run your code

  • import pygame: The pygame module we need to import.
  • pygame.init(): This function initializes all pygame modules that are imported.
  • pygame.display.set_mode(): In this line we are setting our game window’s width and height.
  • pygame.display.set_caption(): This method taking a string, and it is the title of our game’s window.
  • input(): Using this function just to hold the window, else when you will run it window will displayed for a fraction of second and then it will close.

Display images with PyGame

In this we initialize the pygame module and then define the mode and the caption for the image. Next we load the image and define the coordinates. The screen.blit function paints the screen while the while loop keeps listening the end of the game is clicked.
import pygame
pygame.init()
w = 300;
h = 300
screen = pygame.display.set_mode((w, h))
pygame.display.set_caption(‘Image’)
TPImage = pygame.image.load(“E:\\Python3\\image.png”).convert()
# coordinates of the image
x = 10;
y = 20;
screen.blit(TPImage, (x, y))
# paint screen one time
pygame.display.flip()
running = True
while (running): # loop listening for end of game
   for event in pygame.event.get():
      if event.type == pygame.QUIT:
         running = False
# loop over, quite pygame
pygame.quit()
Now here you come at the end of this article and hope article was helpful. Like everything else, this is also about practice. The more you do, the better you get.
As you add more and more features slowly, you will be able to make more complex games. So we hope you get to make more games.

Leave a Reply

Your email address will not be published. Required fields are marked *