Journey into PyGame — The beginning

For those python fans, Pygame is indeed a great python’s game engine which you all should really consider if you want to write game using the python language. Although some of you might disagree with my thought but if we remove PyGame from the list of Python’s gaming engine then the only one left for us to consider will be pyglet which is not that well documented as compared to PyGame and thus makes our game development process even harder as compared to using the PyGame engine.

In this article I am going to show you how to install and run your first pygame program with a few lines of python script, ok, without wasting any time let get started.

I have installed python 3.5.2 in my computer and thus I will need to install the PyGame version which matches my python version. In order to install PyGame which matches Python 3.x you will need to visit this PyGame download page. Although my computer’s os belongs to windows 64bit but it seems like the 64bit PyGame version is not working due to some unknown reason (either one of the module is missing or the platform is not supported), due to that I have selected the 32bit version instead because it really doesn’t matter either we are installing the 32bit or the 64bit in our 64bit os as long as the os is 64bit then we can either select the 32bit or the 64bit version, however the reverse is not true because if we install the 64bit PyGame in our 32bit pc then we will not be able to use it at all.

I have downloaded this file pygame-1.9.2b1-cp35-cp35m-win32.whl from the above website. The cp35 in the filename means python 3.5 and win32 means it is for 32bit of windows os. After I have downloaded this file I will need to install it either with pip or pip3 or pip3.5. Browse to the folder where you have downloaded the whl file in the windows command prompt then type in this command : pip3 install pygame-1.9.2b1-cp35-cp35m-win32.whl to extract and then install the PyGame module into Python’s site package folder.

Next create a new project in NetBeans 8.1 IDE and then create a new python module with any name you like and enter below script into it. If you want to know how to use PyGame in your python program then you can visit the PyGame official homepage for further detail.

#!/usr/bin/env python

import pygame
from pygame.locals import *
from sys import exit

background_image = 'terain7.png'

pygame.init()
screen = pygame.display.set_mode((640, 480), 0, 32)
pygame.display.set_caption("Pygame Demo")
background = pygame.image.load(background_image).convert()

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
    screen.blit(background, (0,0))
    pygame.display.update()

You will also need to provide a background image (terain7.png) in the same folder as your gaming module in order for the display to show that image. Now run the script and you should see the below pop up with any background image which you have included together with your gaming module.

Pygame
Pygame

Pygame engine starts slowly which is a common problem for all the python programs but once it is ready then it will operate smoothly!