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.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
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
Post a Comment