Make games with Pygame

Make games with Pygame

Learn the basics of pygame to build games with python

Introduction

Pygame is a cross-platform library to build games with python. It has computer graphics and sound libraries to make developing games easy with python. We can install pygame with pip. Pygame is used to make only simple and light games. If you want to build large and amazing we must go with a game engine like unity or unreal.

pip install pygame

Basics

Creating a blank screen

The following code can be used to create and maintain a blank screen:

import pygame as pp
from sys import exit
pp.init()
screen = pp.display.set_mode((800, 600))
pp.display.set_caption("Demo")

The above code is used to initialize the components of pygame and create a screen with width 800 and height 600 and name "Demo", but the screen created will be gone in a second. To prevent this we must update the screen continuously using a for loop.

while True:
    for event in pp.event.get():
        if event.type== pp.QUIT:
            exit()
    pp.display.update()

The event implies the event provided by pygame such as KEYUP KEYDOWN EXIT etc... We are then checking for the QUIT event to close the screen.

Drawing and writing

We have now created a screen, we can now write to it. First let's change color to our screen.

screen.fill((255,255,0))

The colors are in RGB format. The color given above is yellow. Then let us write hello pygame in our screen. First we have to decide our font and fontsize.

font = pp.font.Font('freesansbold.ttf', 32)

Then we have to render the text on the screen,

text = font.render('hello pygame', True, green, blue)

The first parameter represent the text, the 2nd parameter represent anti aliasing which makes our text look smoother. Then we have text color and background color. Wait.. we have not really written it to screen, to do this we have to use the following code inside the while loop,

   screen.blit(text,(400,300))

This will display the text in the center of the screen. This is the base for animation too. We can change the x and y continuously to make animations. We can also use the get_rect() function and use it instead of using coordinates, then use the blit option with the rectangle.

rect=text.get_rect(center=(400,300))
screen.blit(text,rect)

A rectangle has 9 position such as top left, top right following until bottom right. This helps to place the text in position easily

We can also draw any shape in pygame. To draw a simple rectangle we can use the the draw.rect() function.

pp.draw.rect(surface,color,pp.rect(width,height,x,y))

Collisions

We can check collisions between two rectangles in pygame. To do this we need two rectangles.

rect = pygame.Rect((20, 50), (50, 100))

This creates a rectangle at 20 in x axis and 50 in y axis with width 50 and height 100. Then we can create new rectangle by loading an image.

img1=pp.image.load("imagepath.jpg").convert_alpha()
rect2=img1.get_rect(topleft=(10,60))

The convert_alpha() option is used to make image in desired pixel format for blitting to screen. Then the collision can be checked.

if img1.colliderect(rect2):
   collision

We can also use the collidepoint((x,y)) to check for collision with the specified point.

Clock

Pygame provides a clock to check for time. We can create a clock by,

clock=pygame.time.Clock()

Then we can get the time in milliseconds using

pp.time.get_ticks()

We can also delay the code execution. The below code delays the time by 3 second.

pp.time.delay(3000)

Conclusion

This is a basic introduction to building games with pygame, explore more to have lot of fun.

Did you find this article valuable?

Support hari's blog by becoming a sponsor. Any amount is appreciated!