Generic selectors
Exact matches only
Search in title
Search in content
wb_sunny

Python Pygame: An Easy Introduction

Pygame Intro Feature Img

Hey there fellow learner! Today you will be learning about the Python Pygame.

Python Pygame is usually used to build games and other graphics. One can control all the logic and graphics. There are no worries about the background complexities related to audio and video.

Getting Started with Python Pygame

Before jumping to major things let’s look at a simple code given below.

import pygame 
from pygame.locals import * 
pygame.init() 
game_display = pygame.display.set_mode((800, 600)) 
pygame.display.set_caption('My First Game') 
def event_handler(): 
    for event in pygame.event.get(): 
        if (event.type == QUIT): 
            pygame.quit() 
            quit() 
while True: 
    event_handler() 
    pygame.display.update()
  • import pygame and from pygame. locals import * – It allows access to all the pygame functionalities and inner modules
  • pygame.init() – Initialize pygame to provide easy access to functions and automatically start up all the pygame modules that need to be initialized.
  • pygame. display.set_mode((width, height)) – It displays a window of a particular height and width.
  • pygame. display.set_caption(‘My First Game’) – Adding the name of the game on the top of the title screen.
  • Function to handle the events – One needs to define a function to handle the events happening on the screen. For now, we take into consideration one event i.e. closing the window on pressing ‘X’ on the window.
  • pygame. display.update() – It is used to make the necessary updates on the display.

The picture below shows the result of the code mentioned above. Just a basic black screen. Yep!

Initial Window Python Pygame
Initial Window Pygame

Adding Objects to the Python Pygame Output

The above screen is absolutely boring. Just a black screen and nothing else. Let’s start adding few elements to the screen.

1. Inserting an Image

First, it is required to change the background color to white and then loading the image from the directory. Then, upload the image at a certain position. The code for the same is shown below:

game_display.fill('white')
img = pygame.image.load('img1.png')
game_display.blit(img, (0, 0))

The screen below shows the output of the code typed above.

Insert Image Python Pygame
Insert Image Pygame

2. Inserting Shapes

In the Pygame library, one can draw specific pixels, lines, circles, rectangles, and any polygon by simply specifying the points to draw between.

Recommended read – How to use Python Turtle for drawing objects?

Inserting Rectangle – To draw a rectangle, one needs to use pygame.draw.rect() which takes a number of parameters including screen name, the color of the rectangle, and dimensions of the rectangle ( x,y, width, height).

The code below displays a red rectangle on the screen.

pygame.draw.rect(game_display, 'red', (50, 20, 120, 100))

Inserting Circles – Inserting circles require parameters such as Screen name, color, and coordinates of the center and the radius of the circle.

pygame.draw.circle(game_display, 'yellow', (150,170),40)

The output of the two shapes drawn on screen is as follows.

Insert Shape Python Pygame
Insert Shape Pygame

Refer to the Pygame.draw official documentation to learn all the different shapes that you can draw using this module.

Creating a simple scenery using Python Pygame

The code below displays a simple scenery on the screen. Try it out on your own to get the same results!

import pygame 
from pygame.locals import * 
pygame.init() 
game_display = pygame.display.set_mode((800, 250)) 
pygame.display.set_caption('My First Game')

pygame.draw.rect(game_display, 'lightblue', (0, 0, 800, 200))
pygame.draw.circle(game_display, 'yellow', (400,160),40)
pygame.draw.polygon(game_display, 'brown',((0,200),(50,100),(100,200)))
pygame.draw.polygon(game_display, 'brown',((100,200),(150,100),(200,200)))
pygame.draw.polygon(game_display, 'brown',((200,200),(250,100),(300,200)))
pygame.draw.polygon(game_display, 'brown',((300,200),(350,100),(400,200)))
pygame.draw.polygon(game_display, 'brown',((400,200),(450,100),(500,200)))
pygame.draw.polygon(game_display, 'brown',((500,200),(550,100),(600,200)))
pygame.draw.polygon(game_display, 'brown',((600,200),(650,100),(700,200)))
pygame.draw.polygon(game_display, 'brown',((700,200),(750,100),(800,200)))
pygame.draw.rect(game_display, 'lightgreen', (0,200, 800, 50))

def event_handler(): 
    for event in pygame.event.get(): 
        if (event.type == QUIT): 
            pygame.quit() 
            quit() 
while True: 
    event_handler() 
    pygame.display.update()

The results of the code above are as shown below:

Scenery Using Pygame
Scenery Using Pygame

Conclusion

In this tutorial, you learned some basic concepts of pygame and how to create simple shapes and objects using the same. Keep Learning!

Leave a Reply

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