Sunday, July 3, 2011

Game development using Python


Did you ever think how much efforts will be there to develop a video game? Most of us had played NFS like games. After Shift 2 the latest version of NFS is releasing on 15th November this year with the name NFS Run. I am a great fan of NFS games and always thrilled with their fascinating graphics.

My instructor gave me a mini project for developing a game using python. I was speechless and no idea, how to make a game. He gave me some tutorial links and I found them very interesting. And with 2 days I completed my game and named it as Race Mania. It’s a very simple game with only 2D graphics. I want to share my experiences through this game development with you.

I have to install ‘pygame’ module in my ubuntu first. It’s the python module that helps to develop games using python. I have to import his module in the program. After that I initialized some variables for colors, fonts and screen size.

If you want to define a color you must know its RGB value. For white it is 255,255,255. My game window has the width 400pixels and height 480pixels.

I use the following code to name my game so that the name 'Race Mania' will appear on the top of the window.

pygame.display.set_caption("Race Mania")

Then I want to load the background for the game, an image of road, so that my game gets a road as the background.

road = pygame.image.load("Road.jpg")

The above code loads the image file Road.jpg. The image file must be there in the same folder where pgm.py for this game exists.

Now I have to add the car image. For that purpose we use the same code explained above.
The car needs to be controlled using the keyboard.

if event.type == pygame.KEYDOWN:

The above code checks the event from key board. If a key is pressed down the followed lines of code will be executed. We can also check for whether specific keys have been pressed or not. We can set the left arrow key and right arrow key to control the car. We can move the car using these keys to either left or right.

Now my game has reached its first stage. Now I can move my car. Now I want to set more options in my game. I can set enemies, energy, hit music, score card etc. In my next post I will explain how I finished my game by including these options

Thanks

AJAY

No comments:

Post a Comment

Comments with advertisement links will not be published. Thank you.