Friday, June 24, 2011

Shoot the Bonkars (Simple 2-D FPS Game)







My aim with this tutorial is to share the experience i had of designing a 2-D FPS inPython. I will try and be as precise as i can and the the same time not go into theunnecessary details.Devel's Requirements Windows platform with python 2.4/2.5/2.6(I used Python 2.5) Pygame : this provides you with the basic functions to handle events like mouse click, key press. (I used Pygame 1.9.1) Text Editor: I used Notepad++. Basics Knowledge of coding in Python(FOR,WHILE,IF,Function Calling).

    Now (asuming you have all the Devel's Requirement mentioned above) we proceed to using Pygame to create a 2-D FPS.

    Opening A window

    1.)First thing that we need to do is create an open window in which the game would run.
    The following code opens a window in Python
    #!/usr/bin/python -tt import pygame
    from pygame.locals import *def main():  
    screen = pygame.display.set_mode((800, 600))
    if __name__ == '__main__':   
    main()

    CODE DESCRIPTION

    import pygame # this imports pygame in the code so that we can use the subroutines it provides(like those for images,sound etc)
    from pygame.locals import * # this imports a number of functions and constants into the top-level namespace. It isn’t essential to do this in order to use Pygame, but it is convenient because we don’t have to precede frequently used values with the pygame namespace.
    def main() # this creates the main function
    screen=...(800,600) # this is the only line in the above program that gives result . it opens a screen of size (800x600) as given.
    if __name......main() # this calls the main function.

    The screen closes as soon as the program terminates. Whats next required is a way to keep that window open until we want to close it. This can be doen by adding a while loop after displaying the screen. The while loop must be inatialise to true to keep the screen open and exit be made when we click on the close button of the screen.

    2.)The code given below does the required job. Add it after the screen=...(800,600) line
    while True:
      for event in pygame.event.get():
       if event.type == QUIT:    
          exit()

    CODE DESCRIPTION
    while True: # this line starts a continuous while loop to maintain the screen open.
    for .....event.get(): # this line iterates through all events in the code.
    if .....==QUIT # this line checks for the event QUIT (click on close button).
    exit() # this close the while and makes the exit.

    3.)Next thing we are going to add is image.
    Adding any image to the screen is a 3-step process.
    i)- Load the image hard drive as surface containing the image data and convert the image to the same format as our display.
    ii)-Load this converted surface on the screen at a desired position (x,y)
    iii)Flip the display

    the code performs the task stated above:
    background = pygame.image.load("image1.png").convert() # Does task i
    screen.blit(background, (x, y)) # Does task ii)
    pygame.display.flip() # does task iii)

    obviously the last 2 line (task needs to be inside the while loop if you are performing operations )
    if you rather prefer to color your screen background with some color, Type the code below
    screen.fill((RR,GG,BB))
    obviously (RR,GG,BB) is the color code.

    4.)using the above method i planted 4-images (small 50x50) on screen at random positions and made them move here and there at constant speed. the images also bounced back(changed there direction on colliding with any of the 4 walls or boundary).

    5.)Now you have got your bonkars . All you need now is a bullet to shoot and a cannon to shoot from. For this have the cannon's position fixed say at bottom center of the screen (at 400,550 on a 800x600 screen).Paste the picture of a cross-wire on the mouse position .Get the mouse position with command "pygame.mouse.get_pos()" and then write an equation to propagate the bullet from cannon's position to the mouse position at the shoot-time.

    6)If u want only 1 bullet then avoid shooting bullets until the previous one's co-ordinate has left the screen. Otherwise if you want more than one bullets keep an array of flags that indicates whether a bullet is in use or not together with its co-ordinates. If the co-ordinatesof the bullets have left the screen the particular bullets flag is again set as "not in use". If all bullets are in use then you may flash "overheat" as a signal to the user to stop for some time.


    using these ideas i developed my First Game in Python. the code for it is available on the link below............https://sourceforge.net/projects/shootthebonkars/

    No comments:

    Post a Comment