Getting started with PyGame

PyGame is a free-to-use and open-source set of Python Modules. And as the name suggests, it can be used to build games. You can code the games and then use specific commands to change it into an executable file that you can share with your friends to show them the work you have been doing. It includes computer graphics and sound libraries designed to be used with the Python programming language. PyGame 2.0.1 is the latest version at the time of writing this article.

Setting Up Pygame

By default, Python doesn't come with PyGame as an in-built Library. So we have to install it using the command prompt. Open a command prompt and type the following command:

pip install pygame --upgrade

If you already have PyGame installed, use the following command to check the version:

pip show pygame

If your Pygame is not updated to the latest version, use the following command:

pip install pygame --upgrade

Now that you have installed the latest version of PyGame, let's get started with some codes...

Initializing PyGame Window

# program to initialize an empty PyGame Window
# importing pygame and sys libraries
import pygame, sys

#intializing all modules of pygame library
pygame.init()

#setting screen size of the PyGame Window
screen = pygame.display.set_mode((800,600))

#setting name of the PyGame Window
pygame.display.set_caption("PyGame Window")

#infinite loop of the game
while True:
  #loop to iterate through all the events happening in the window
    for event in pygame.event.get():
      #condition to check QUIT event
        if event.type == pygame.QUIT:
            #break out of the loop and closing the window
            sys.exit()

Running through the Code:

We start by importing the PyGame and sys library into the program. init() is used to initialize all the modules of the PyGame library. We set the size of the window using set_mode( (x,y) ), where (x,y) is a tuple, and save it in the variable screen which can be accessed later. set_caption() is used to name the window. A while loop is initiated and inside it, a for loop that iterates through all the events that are taking place. An if condition is added to look for QUIT event and when the particular event is found, we break the loop by ending the program using sys.exit().

Output:

1.jfif

Writing Text on PyGame Window

Now that we have a PyGame let's add some texts to the Window.

#importing pygame and sys libraries
import pygame, sys

#intializing all the modules of pygame library
pygame.init()

#setting up variables
width = 800
height = 600
white = (255,255,255)

#setting the window size
screen = pygame.display.set_mode((width,height))

#setting window name
pygame.display.set_caption("PyGame Window")

#setting font variable with font style and size 
font = pygame.font.SysFont('Arial', 35)

#assigning a string with other arguments.
text = font.render('This Text Will Appear In PyGame Window', True, white)

#infinite loop
while True:

    #calling the text variable with position of the text
    screen.blit(text, [150,150])

    #updating the string on the screen
    pygame.display.update()

    #loop to iterate through all the events
    for event in pygame.event.get():

        #condition to find QUIT event
        if event.type == pygame.QUIT:

            #exiting the program
            sys.exit()

Running through the Code:

We now add a variable 'white' with RGB coefficient of white color as PyGame only accepts RGB Color Coding. We set the font and size of the text in the 'font' variable from the SysFont module. render() is used to assign the text styles to the string. Finally, blit() is used to show the string with all the styles on it followed by update(), which updates the screen with the new String.

Output:

2.jfif

Drawing on PyGame Window

PyGame has various modules to draw shapes whose dimensions can be given as arguments. PyGame has modules to draw Rectangles, Squares, Circles, Ellipses, Lines, Arcs, and any Polygon directly using the draw() function. We will be looking at a few basic examples for this part of the article, if you want to read more about shapes that can be drawn using the draw() function, refer to this page.

Lines

#importing pygame and sys libraries
import pygame, sys

#intializing all the modules of pygame library
pygame.init()

#setting up variables
width = 800
height = 600
white = (255,255,255)

#setting the window size
screen = pygame.display.set_mode((width,height))

#setting window name
pygame.display.set_caption("PyGame Window")


#infinite loop
while True:

    #drawing a line
    pygame.draw.line(screen,white,(50,50),(750,550),5)

    #updating the string on the screen
    pygame.display.update()

    #loop to iterate through all the events
    for event in pygame.event.get():

        #condition to find QUIT event
        if event.type == pygame.QUIT:

            #exiting the program
            sys.exit()

Running through the code:

We start by initializing a PyGame window with desired size and name using set_mode() and set_caption(). Next, in the while loop, we use the draw() function followed by the shape wanted with arguments.

For drawing a line, we use the line() function which takes 5 arguments. First is the screen where the shape is to be displayed, second the color of the shape, third and fourth arguments are the start and end position of the line and finally the last argument is the width of the line.

Output:

3.jfif

Rectangles

#importing pygame and sys libraries
import pygame, sys

#intializing all the modules of pygame library
pygame.init()

#setting up variables
width = 800
height = 600
white = (255,255,255)

#setting the window size
screen = pygame.display.set_mode((width,height))

#setting window name
pygame.display.set_caption("PyGame Window")


#infinite loop
while True:

    #drawing a rectangle
    pygame.draw.rect(screen,white, [200,200,400,200],2)

    #updating the string on the screen
    pygame.display.update()

    #loop to iterate through all the events
    for event in pygame.event.get():

        #condition to find QUIT event
        if event.type == pygame.QUIT:

            #exiting the program
            sys.exit()

Running through the code:

To draw a rectangle we use rect() which takes 4 arguments to draw a rectangle. Similar to the line, the first two arguments need to draw a rectangle is a screen and the color of the rectangle. The third argument comprises 4 parameters, the first being the position of the rectangle and the latter two being the width and height of the rectangle. The last argument in the function is for the thickness of the rectangle.

Output:

4.jfif

Circle

#importing pygame and sys libraries
import pygame, sys

#intializing all the modules of pygame library
pygame.init()

#setting up variables
width = 800
height = 600
white = (255,255,255)

#setting the window size
screen = pygame.display.set_mode((width,height))

#setting window name
pygame.display.set_caption("PyGame Window")


#infinite loop
while True:

    #drawing a circle
    pygame.draw.circle(screen,white,(400,300),200,5)

    #updating the string on the screen
    pygame.display.update()

    #loop to iterate through all the events
    for event in pygame.event.get():

        #condition to find QUIT event
        if event.type == pygame.QUIT:

            #exiting the program
            sys.exit()

Running through the code:

To draw a circle we use the circle() function which takes 5 arguments to draw a circle. Similar to Rectangle and Line, the first two arguments are the screen where the circle is to be shown and the color of the circle. The third argument is used to determine the position of the circle. The fourth argument is the radius of the circle and the last argument is used to set the thickness of the circle. If you set thickness as 0, the circle generated will be filled.

Output:

5.jfif

These are some of the basic shapes that you can draw using PyGame, if you find this interesting and want to explore more about them you can check out the official document of PyGame draw() Module .