Showing posts with label Race Mania. Show all posts
Showing posts with label Race Mania. Show all posts

Tuesday, September 27, 2011

Race Mania

I always have a craze of computer games with stunning graphics and my favourite is NFS Most Wanted. I always wondered the graphics they are integrating in the game. 

Anyway I tried to develop a game using python! I developed it in 2 days and has a small interface. Basically it is a car racing game and don't  over estimate it. I just want to familiar with the pygame module in python and the game has very basic graphics.

Pygame is a python module which can be installed and then imported into python program. It has basic graphics methods to draw objects and update the screen. 

You can download the source code and run the application in your Linux. I have already included a better READ ME file with the source code. Read it before using the code. Cliack on the below tab to download the source code.Download!!

Some screenshots are given below,



















If you have any doubts with the codes, you can also read my posts regarding the source code of the Race Mania. Links for my Race Mania posts are,



Please let me know your experiences, experiments, suggestions and comments.
 
Thanks

AJAY

Tuesday, July 5, 2011

Display methodes in Pygame


In this post I want to explain the important functions of pygame.display functions and the blit function. It helps to draw or update the screen in order to give a feel of a game is happening.

pygame.display.flip()

This function updates the entire game screen(canvas). Its used in my game program to draw the car, enemies and the energy symbols. We should take care of the use of pgame.display.flip() so that our objects will correctly display and update(move) on the screen. If we use two screens then it will swap both in a small time interval.

pygame.display.update()

It updates the portions of the screen. This function is like an optimized version of pygame.display.flip(). It allows only a portion of the screen to updated, instead of the entire area. If no argument is passed it updates the entire Surface area like pygame.display.flip().You can pass the function a single rectangle, or a sequence of rectangles. It is more efficient to pass many rectangles at once than to call update multiple times with single or a partial list of rectangles. 

blit()

It is used to actually render the objects in the screen. We need to render our objects first. Then we are using the pygame.display.flip() and pygame.display.update() to make a feel of moving the objects.
Blitting is the word used for rendering the objects in the screen.

screen.blit(car, [x_coord,y_coord])

The above code is used to render the car object at the specified position.
Another variety blit used in my code is,

screen.blit(scoreboard.render("Score " + str(score) , 4, (255,255,255)) , [40,40])

It renders the score board of the game at the position 40, 40 with the font color white(i.e.,255,255,255). A flip is also used with it to lively update the score.
Hope this post will help you to get the ideas about how we draw and update the objects in the game screen.

Thanks

AJAY

Sunday, July 3, 2011

Adding more options in Game


In my previous post I have explained you how I set up my game window. I had set the car also. Now I want to add enemies. For that I want to add the enemy cars first. Using the statement,

enemies_im=pygame.image.load("enemies.gif")

I also added the energy symbols also in the above method. Now I want to display them in the screen. For that I use a random function and that’s why I imported random.

for i in range(15):
    x=random.randrange(0,400)
    y=random.randrange(0,440)
    enemies.append([x,y])

The above code creates a list that has the enemy cars in random positions. I also set another list of energy symbols in the same way.

screen.blit(road, [0,0])

The above code is used to display the road image in the position x=0 and y=0 in the screen.

screen.blit(car, [x_coord,y_coord])

This code is used to display the car in the middle bottom of the screen.

if enemies[i][1] > 480:
            y=random.randrange(-480,-10)
            enemies[i][1]=y
            x=random.randrange(0,400)
            enemies[i][0]=x

This code helps to redraw the enemies/energy from the top of the screen when they hit the car. So that the user gets a nice feeling that he hit the objects.

Now we want to calculate the score by recognizing the hits. The below code is used for that.

if carrect.collidepoint(enemies[i][0], enemies[i][1]):
            score = score – 3

We can also play some music in background or play music when objects hits.

enemyhit = pygame.mixer.Sound("hit.wav")
enemyhit.play()

The above code is used for that purpose.

We can also display the scoreboard that updates lively.

screen.blit(scoreboard.render("Score " + str(score) , 4, (255,255,255)) , [40,40])

The above code is used for that. The score is printed in white color (that’s why the RGB code 255,255,255 is passed as a parameter). The 40,40 is the position where the score is to be displayed.

We can further include some more actions to reduce the bugs. We can include a sys.exit() to close the window when the user click on the close button of game. We can also set the conditions where the user wins or loses the game.

You can download the source code and can run/edit the code. Click here to download.



















Thanks

AJAY

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