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

Introduction to Python

Introduction         Most of us would be very interested in game designing even before knowing what is it about. We get admired by the way the games we play are designed and that becomes the cause for many of us to have the wish to become a game designer. So this is the platform where I am going to share with everyone what I have learned. When it comes to game designing, everyone knows that there is some serious coding stuff done behind and behind every successful game in the market, there would have been coders and designers working very hard for nearly more than 5 years. So we need to learn  programming languages with great interest to get into game designing. Python is a latest language which is more convenient and easy to learn and work with.It is easy to learn it as it has built in capability for many features. We can perform the operations with a single line of code which requires several lines of code to be written in langu...

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...