Skip to main content

Introducing Pygame

Hello Everyone!
Let's get into the most interesting topic. We are going to see how we design games using python programming language,
Let me introduce you to 'pygame'. Pygame is an open-source and free python library which is used for creating multimedia applications like games built on the top of an excellent SDL library.
Pygame is highly portable and can be run on nearly any platform and operating system.

First of all,We need to download the pygame library which can be downloaded from the official Pygame website.
Several versions of pygame compatible for the various versions of python are available.
We need to select the appropriate version of pygame for our version of python and download and install it.

Here i use Python-2.7. So i downloaded pygame-1.9.1

For using pygame library in a python program, like importing any other module, we must import pygame using the command.
             import pygame
You can also check if you have installed pygame correctly using this command. Open a python interpreter window. To open a python interpreter, just open command prompt and type python.
Then >>> will be displayed which indicates that python is in interpreter mode.
Interpreter mode is where each single line of code is executed immediately as it is written.
So when >>> appears,type 'import pygame'.
If the module is correctly installed, the interpreter will come to the next line.


If it is not installed correctly,it will display the error as : 'ImportError: No module named pygame'
Now all the functions of pygame can be used and implemented in the program.

So now we have successfully installed pygame in our system and we are ready to start coding.

Creating a Basic framework :

Now we can write our first code in pygame. We are just going to create our game window that pops up and exits.

Let me write the code first and explain it to you.Always save the program in the folder containing Python(For example, inside C:\Python27 ).

import pygame
pygame.init()
display=pygame.display.set_mode((500,500))
pygame.display.update()
pygame.quit()
quit()

After writing this code in IDLE, save it in the location of Python with a name say 'p1.py'. Then open the command prompt and then move to the directory of the program and then type 'python p1.py'
A blank black window pops up and then exits immediately.

So,this is our first pygame program.In the first line, we just import python pygame module which contains all the functionalities.
The next line 'pygame.init()' is used to initialize the pygame. It actually returns a tuple of the number of successes and failures but we don't need that to be stored in any variable.So we leave that as it is.
pygame.init() function initializes the pygame module.

Next we need a surface. pygame.display is used to define a surface over which over game runs,
pygame.display.set_mode() returns a surface object which we store it in a variable named display.
The pygame.displaty.set_mode() takes a tuple as a parameter which is used to define the width and height of the game window. It is given as (width,height) which is a tuple. If we give the values without brackets(), then pygame takes it as two seperate values and throws out an error. The display width and height is set as a tuple.

The next line pygame.display.update() is used to update the surface window. It is used in all the programs to update the window to display the changing content dynamically.
There is another function similar to pygame.display.update() which is pygame.display.flip().
pygame.display.flip() is used to update the whole surface window whereas pygame.display.update() can be used to update only a particular portion of window we specify. But without giving any parameters to pygame.display.update(),it works in the same manner as pygame.display.flip() and updates the whole surface window.

pygame.exit() is used to close the pygame window. In other words,it is used to uninitialize the initialization of pygame.

quit() command is to quit python and exit the program.

This is how a basic framework is built in pygame and we can develop our code as our application grows.

                                   >> Next : Handling Events >>

Comments

Popular posts from this blog

Python functions and modules

Functions are a block of code that performs the task specified. They either just perform some computation and display the result or returns the result to the main function. In python, functions are defined with the keyword 'def'. The syntax for defining a function in python is : def function_name :       #Block of statements The keyword def is followed by the name of the function. It is a good practise to name the functions meaningfully representing the task done by it. The function name is followed by a colon (:). In python, Comments are represented by the symbol #. So here #Block of statements is commented and hence it will not be executed by the compiler. # is used to comment one line of code. ''' .......''' is used to comment a block of code. For example, #this is a commented line of code. ''' this is a block of commented code Everything in this code will not be looked into by the compiler This is helpful in comment...

Moving Objects

Hi everyone ! In this post, let us see how to move objects on the game screen. In the previous post, we learnt how to draw objects and fill colors. Now we will see how to move those objects from one place to another. To move objects in a game, we need some event to occur. Let us make the object move on pressing the arrow buttons. import pygame pygame.init() white = (255,255,255) black = (0,0,0) red = (255,0,0) green = (0,255,0) blue = (0,0,255) display=pygame.display.set_mode((500,500)) game_loop=False rect_x = 100 rect_y = 100 while not game_loop:     for event in pygame.event.get():         if event.type == pygame.QUIT:             game_loop = True         if event.type == pygame.KEYDOWN :             if event.key == pygame.K_LEFT :                 rect_x -= 10             if event.key...

Drawing shapes And Filling colours

Till now, we have written a python program to open a blank black screen and the QUIT event is handled. In this post, let's see how to add colours to the game surface and draw shapes onto the screen. Filling colours : By default, pygame has no predefined colours. We have to define the colours by giving the RGB(red,green,blue) value of the colour.   import pygame pygame.init() white = (255,255,255) black = (0,0,0) red = (255,0,0) green = (0,255,0) blue = (0,0,255) display=pygame.display.set_mode((500,500)) game_loop=False while not game_loop:     for event in pygame.event.get():         if event.type==pygame.QUIT:             game_loop=True     display.fill(blue)     pygame.display.update() pygame.quit() quit() Here i have defined the basic colours by giving the rgb value of those colors. The values are given as a tuple here. Since white is a mixture o...