Sunday 10 February 2013

Android Game Development Tutorial [ part 2 ]


Hi Friends,
            Welcome you all in the Android Game Development [Part 2].

            Let’s take a quick review of what we have learnt in the Android Game Development [Part 1].
1)    We have created a Basic Game.
2)    We have learnt, how to handle event handling.
3)    We have learnt, how to implement Background Scrolling.
4)    We have learnt, how to handle the X,Y Co-ordinate according to the game object movement.

Let’s have a look what we are going to learn in this Part.
1)    Emotions.
2)    Game Level.
3)    Collision Detection.

With the help of all these things, we will create a one level full game.

Let’s start,
1)    Emotions:-
We will add emotions in our game.
How we are adding it?
We will change the object emotions according to the event.
Suppose when the object jumps, we will show object’s jump emotions. When the object dies , we will change object’s emotions. When the object wins, we will change object’s emotions.

With the help of these emotions our game will look more interactive.

2)    Game Level :-
We will create a game level. We are going to add some bricks here and there in the game to create a game level.

3)    Collision Detection:-
Collision Detection is the very important part of game development.
Suppose object A is moving in the forward direction, and object B  coming into same way, then after some time the situation will arise, when object A collide with the object B.

Take an example of our game, suppose our object collide with the brick object. What actions we have to take. We should stop the game speed. We should not allow the game main object to cross the brick object. The only way to cross the brick object is you have to jump and cross the brick.

With the help of collision detection we will create our game more realistic.

How we are checking the collision detection.
In our game we are handling it with the help of rectangles.

Let’s assume in our game we are only having one brick and our game main object.
First we have to create two rectangle exactly that fits into our both the object. Then we have to check the X1 and X2 of both the rectangle. Don’t forgot to update the X,Y of the rectangles when you move the object or in case of Background Scrolling.

Collision happen in two ways, First is the Horizontal Collision and Second is the vertical Collision.

In the case of Horizontal Collision we only need to consider the X co-ordinates, but in case of Vertical we have to consider the Y co-ordinate also. Special case may also arise when you face Horizontal and Vertical both the collision at the same time.


Let’s have a look of the files you need for today’s tutorial:-
1)    GameMain.java
2)    HandlingXY.java
3)    Bricks.java

/************************************************/
GameMain.java
/************************************************/
/*

import java.applet.Applet;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Shobhit Upadhyaya
 */
public class GameMain extends Applet  implements Runnable, KeyListener{

    /**
     * Initialization method that will be called after the applet is loaded into
     * the browser.
     */
    
    static public int image3X=-1000;
    static public int image1X=0;
    static public int image2X=1000;

    private Image image, character, bg1, bg2, bg3;
    private Graphics gObject;
    private URL base;

    static public int bX=500;
    static public int bY=220;

    static int rX1=0, rY1=0, rX2=45, rY2=35;

    private boolean showYouWonImage=false;
    private static boolean isStarted=false;
    private static int isGameOver=0;
    static boolean youWon=false;
    static public boolean isDead=false;
    
    private static Bricks bk1,bk2,bk3,bk4,bk5,bk6;
    private static Bricks ch_bk1;
    private static Bricks ch_bk2,ch_bk3;
    private static Bricks ch_bk4, ch_bk5;
    private static Bricks final_bk;
    
    static boolean initial_img = true;
        
    private Image currentObject, character1, characterDown, characterJumped, background;
    private Image dead_character, base_brick,common_brick,hidden_brick,final_brick,game_background2;
    private Image start_game,game_over,you_won;


    HandlingXY mainObject;
        
    public void init() {
        // TODO start asynchronous download of heavy resources
        
                setSize(1000, 480);
  setBackground(Color.WHITE);
  setFocusable(true);
  addKeyListener(this);
  Frame frame = (Frame) this.getParent().getParent();
  frame.setTitle("[ Game Development Part2 ]");
  try {
   base = getCodeBase();
  } catch (Exception e) {
   // TODO: handle exception
  }

                 
                dead_character = getImage(base, "images/dead.png");                 
                start_game=getImage(base,"images/start_game.png");
                game_over=getImage(base,"images/game_over.png");
                you_won=getImage(base,"images/you_won.png");
                
                base_brick=getImage(base,"images/base_brick.png");
                common_brick=getImage(base,"images/common_brick.png");
                hidden_brick=getImage(base,"images/hidden_brick.png");
                final_brick=getImage(base,"images/final_brick.png");
                
                character = getImage(base, "images/small_image1.png");
                character1 = getImage(base, "images/small_normal.png");
  characterDown = getImage(base,"images/small_down.png");
  characterJumped = getImage(base, "images/small_up.png");
  currentObject = character;

                background = getImage(base, "images/game_background.png");
                game_background2 = getImage(base,"images/game_background2.png");
                
                
               

    }
    
    @Override
 public void start() {
                bk1 = new Bricks(30,372,250,50);    //perfect            
                bk2 = new Bricks(250,272,150,100);
                bk3 = new Bricks(375,172,150,100);               
                bk4 = new Bricks(700,300,250,50);
                bk5 = new Bricks(900,200,350,50);                
                bk6 = new Bricks(1200,100,100,50);
                ch_bk1 = new Bricks(0,0,1800,450);
                ch_bk2 = new Bricks(-1000,0,1000,450);
  mainObject = new HandlingXY();
  Thread thread = new Thread(this);
  thread.start();
 }

 @Override
 public void stop() {
  // TODO Auto-generated method stub
 }

 @Override
 public void destroy() {
  // TODO Auto-generated method stub
 }

 @Override
 public void run() {
  while (true) {
                    
                    if(isStarted)
                    {
                    
                       mainObject.updateXY();
                       if (mainObject.isJumped())
                       {
    currentObject = characterJumped;
                       }
                       else if (mainObject.isJumped() == false && mainObject.isDucked() == false)
                       {
                               if(initial_img == true){
                                    currentObject = character;
                                }
                                else
                                    currentObject = character1;
   }
                        rX1=mainObject.getCenterX() -55; 
                        rY1=mainObject.getCenterY() -45; 
                       
                     }
                    repaint();
                    try {
                            Thread.sleep(17);
                    } catch (InterruptedException e) {
                    }
  }
 }

 @Override
 public void update(Graphics g) {
            
            
  if (image == null) {
   image = createImage(this.getWidth(), this.getHeight());
   gObject = image.getGraphics();
  }
                
                gObject.setColor(getBackground());
  gObject.fillRect(0, 0, getWidth(), getHeight());
  gObject.setColor(getForeground());
  
  paint(gObject);

  g.drawImage(image, 0, 0, this);

 }

 @Override
 public void paint(Graphics g) {
               
                if(isStarted==false)
                {
                    g.drawImage(start_game,0,0, this);//perfect
                }
                else if(isGameOver>=10)
                {
                    g.drawImage(game_over,0,0, this);//perfect
                   // System.out.println("drawing game_over image");
               
                }
               else if(showYouWonImage==true)
                {
                    try {
            
                        g.drawImage(you_won,0,0, this);//perfect

                    } catch (Exception ex) {
                    }
                }
                else
                {
                    g.setColor(Color.black);

                    /************* Drawing Rectangles for Collision Detection ******************/
                   /*
                    g.drawRect (bk2.getX1(),bk2.getY1(),bk2.getX2(),bk2.getY2());
                    g.drawRect (bk3.getX1(),bk3.getY1(),bk3.getX2(),bk3.getY2()); 
                    g.drawRect (bk4.getX1(),bk4.getY1(),bk4.getX2(),bk4.getY2());
                    
                    if(mainObject.getHidden())
                        g.drawRect (bk5.getX1(),bk5.getY1(),bk5.getX2(),bk5.getY2()); 
                    
                    g.drawRect (bk6.getX1(),bk6.getY1(),bk6.getX2(),bk6.getY2());
                    */   
            
                    
                    g.drawImage(background,ch_bk1.getX1(),ch_bk1.getY1(),this);
                    g.drawImage(game_background2,ch_bk2.getX1(),ch_bk2.getY1(),this);

                    g.drawImage(base_brick,bk1.getX1(),bk1.getY1(),this);
                    g.drawImage(common_brick,bk2.getX1(),bk2.getY1(),this);
                    g.drawImage(common_brick,bk3.getX1(),bk3.getY1(),this);
                    g.drawImage(base_brick,bk4.getX1(),bk4.getY1(),this);
    
                    if(mainObject.getHidden())
                        g.drawImage(hidden_brick,bk5.getX1(),bk5.getY1(),this);
                    
                    g.drawImage(final_brick,bk6.getX1(),bk6.getY1(),this);


                    if(isDead)
                    {
                        currentObject = dead_character;
                        g.drawImage(currentObject, mainObject.getCenterX() - 61, mainObject.getCenterY() - 63, this);//perfect
                        if(isDead && isGameOver<10 data-blogger-escaped-if="" data-blogger-escaped-isgameover="" data-blogger-escaped-thread.sleep="" data-blogger-escaped-try="">=10)
                                    Thread.sleep(2000);
                            } catch (Exception ex) {
                            }
                        }
                        
                    }
                    else if(youWon && ch_bk2.getX1()<=0)
                    {
                    try {
                             g.drawImage(you_won,0,0, this);
                             showYouWonImage=true;
                        }    catch (Exception ex) 
                        {
                        }
                    
                    }
                    else 
                        g.drawImage(currentObject, 
                                        mainObject.getCenterX() - 61, 
                                        mainObject.getCenterY() - 63, this);
                   
                    
                }// isStarted else part

 }

 @Override
 public void keyPressed(KeyEvent e) {

  switch (e.getKeyCode()) {
                    case KeyEvent.VK_ENTER:
                    {
                        System.out.println("Enter has been pressed");
                        
                        if( (isStarted==true && isGameOver>=10) || youWon)
                        { 
                            isStarted=false;
                            isGameOver=0;
                            isDead=false;
                            bk1 = new Bricks(30,372,250,50);    //perfect            
                            bk2 = new Bricks(250,272,150,100);
                            bk3 = new Bricks(375,172,150,100);               
                            bk4 = new Bricks(700,300,250,50);
                            bk5 = new Bricks(900,200,350,50);                
                            bk6 = new Bricks(1200,100,100,50);
                            ch_bk1 = new Bricks(0,0,1800,450);
                            ch_bk2 = new Bricks(-1000,0,1000,450);
                            mainObject.resetAll();
                            youWon=false;
                            showYouWonImage=false;
                
                        }
                        
                        else                        
                            isStarted=true;
                        
                        
                        
                        break;
                    }
  case KeyEvent.VK_UP:
   System.out.println("Move up");
   break;

  case KeyEvent.VK_DOWN:
   currentObject = characterDown;
   if (mainObject.isJumped() == false){
    mainObject.setDucked(true);
    mainObject.setSpeedX(0);
   }
   break;

  case KeyEvent.VK_LEFT:
                        if(initial_img)
                            initial_img=false;
   mainObject.moveLeft();
   mainObject.setMovingLeft(true);
   break;

  case KeyEvent.VK_RIGHT:
                        if(initial_img)
                            initial_img=false;
   mainObject.moveRight();
   mainObject.setMovingRight(true);
   break;

  case KeyEvent.VK_SPACE:
                    if(initial_img)
                            initial_img=false;
   mainObject.jump();
   break;

  }
 }

 @Override
 public void keyReleased(KeyEvent e) {
  switch (e.getKeyCode()) {
  case KeyEvent.VK_UP:
   System.out.println("Stop moving up");
   break;

  case KeyEvent.VK_DOWN:
   System.out.println("Stop moving down");
                        currentObject = character;
   mainObject.setDucked(false);
   break;

  case KeyEvent.VK_LEFT:
   mainObject.stopLeft();
   break;

  case KeyEvent.VK_RIGHT:
   mainObject.stopRight();
   break;

  case KeyEvent.VK_SPACE:
   System.out.println("Stop jumping");
   break;

  }

 }

 @Override
 public void keyTyped(KeyEvent e) {
  // TODO Auto-generated method stub

 }
    // TODO overwrite start(), stop() and destroy() methods
        public static Bricks getBricks1()
        {
            return bk1;
        }
        public static Bricks getBricks2()
        {
            return bk2;
        }
        public static Bricks getBricks3()
        {
            return bk3;
        }
        public static Bricks getBricks4()
        {
            return bk4;
        }
        public static Bricks getBricks5()
        {
            return bk5;
        }
        public static Bricks getBricks6()
        {
            return bk6;
        }
        public static Bricks getchBricks1()
        {
            return ch_bk1;
        }
        public static Bricks getchBricks2()
        {
            return ch_bk2;
        }
        public static Bricks getchBricks3()
        {
            return ch_bk3;
        }
        
        
        public static Bricks getchBricks4()
        {
            return ch_bk4;
        }
        
        public static Bricks getchBricks5()
        {
            return ch_bk5;
        }
        public static Bricks getchFinalBricks()
        {
            return final_bk;
        }
        
}




/************************************************/
HandlingXY.java
/************************************************/


/**
 *
 * @author Shobhit Upadhyaya
 */
public class HandlingXY {
    
        private int speedX = 0;
        private int speedY = 1;
        private boolean last=false;
        private boolean jump_complete=true;
        private boolean f3_dead_state=false;
        private boolean f4_dead_state=false;

        private boolean hidden=false;
        private boolean dead=false;
        int JUMPSPEED = -17;
 final int MOVESPEED = 5;
 final int GROUND = 382;
 
 private int centerX = 100;
 private int centerY = GROUND;
 private boolean jumped = false;
 private boolean movingLeft = false;
 private boolean movingRight = false;
 private boolean ducked = false;

 
 private boolean beyond_base_brick=false;
        private boolean f1_coll=false;
        private boolean on_f1_brick=false;
        private boolean f2_coll=false;
        private boolean on_f2_brick=false;
        private boolean on_f3_brick=false;
        private boolean on_f4_brick=false;
        private boolean on_f5_brick=false;
        
        private Bricks game_bg = GameMain.getchBricks1();
        private Bricks game_bg0 = GameMain.getchBricks2();
        private Bricks baseBrick = GameMain.getBricks1();
        private Bricks f1_CollBrick = GameMain.getBricks2();
        private Bricks f2_CollBrick = GameMain.getBricks3();
        private Bricks f3_CollBrick = GameMain.getBricks4();
        private Bricks f4_CollBrick = GameMain.getBricks5();
        private Bricks f5_CollBrick = GameMain.getBricks6();
        
     private void handlingY()
    {
        // Updates Y Position
        centerY += speedY;

        if(dead==false && beyond_base_brick==false 
                && f3_dead_state==false && f4_dead_state==false)
        {
            if (centerY + speedY >= GROUND) {
                centerY = GROUND;
            }
        }
        // Handles Jumping
        if (jumped == true) {
                speedY += 1;

                if(JUMPSPEED==-5)
                {
                   // System.out.println("show the hidden brick");
                    hidden=true;
                }
                if(jump_complete==false)
                {
                    if (centerY + speedY >= GROUND) {
                            centerY = GROUND+150;   
                            speedY = 0;
                            jumped = false;
                            GameMain.isDead=true;
                    }
                }
                else
                if(dead || beyond_base_brick )
                {
                    if (centerY + speedY >= GROUND+50) {
                            centerY = GROUND+50;   
                            speedY = 0;
                            jumped = false;
                            GameMain.isDead=true;
                            dead=true;
                    }
                }
                else if(f4_dead_state)
                {
                    if (centerY + speedY >= GROUND +50) {
                            centerY = GROUND+50;   
                            speedY = 0;
                            dead=true;
                            GameMain.isDead=true;
                            jumped = false;
                    }
                }
                else if(f3_dead_state)
                {
                    if (centerY + speedY >= GROUND +50) {
                            centerY = GROUND+50;   
                            speedY = 0;
                            dead=true;
                            GameMain.isDead=true;
                            jumped = false;                        
                    }
                }
                else if(on_f5_brick)
                {

                    if (centerY + speedY >= f5_CollBrick.getY1() + 10) {
                        centerY = f5_CollBrick.getY1() + 10 ;   
                        speedY = 0;
                        jumped = false;
                        f2_coll=false;
                        f1_coll=false;
                        on_f2_brick=false;
                        on_f1_brick=false;
                        on_f3_brick=false;
                        on_f5_brick=true;
                        last=true;
                    }
                }
                else if(on_f4_brick)
                {

                    if (centerY + speedY >= f4_CollBrick.getY1()+10) {
                        centerY = f4_CollBrick.getY1() + 10 ;   
                        speedY = 0;
                        jumped = false;
                        f2_coll=false;
                        f1_coll=false;
                        on_f2_brick=false;
                        on_f1_brick=false;
                        on_f3_brick=false;
                    }
                }
                else if(on_f3_brick)
                {

                    if (centerY + speedY >= GROUND) {
                        centerY = f3_CollBrick.getY1() + 10 ;   
                        speedY = 0;
                        jumped = false;
                        f2_coll=false;
                        f1_coll=false;
                        on_f2_brick=false;
                        on_f1_brick=false;
                    }
                }
                else if(on_f2_brick)
                {

                    if (centerY + speedY >= GROUND) {
                        centerY = f1_CollBrick.getY1() + 10;   
                        speedY = 0;
                        jumped = false;
                        f2_coll=false;
                        f1_coll=false;
                        on_f2_brick=false;
                        on_f1_brick=true;
                        on_f3_brick=false;

                    }
                }
                else if(f2_coll)
                {

                    if (centerY + speedY >= f2_CollBrick.getY1()) {
                        
                            centerY = f2_CollBrick.getY1() + 10;   
                            speedY = 0;
                            jumped = false;
                            f2_coll=false;
                            if(on_f2_brick==false)
                                on_f2_brick=true;
                            //GameMain.isDead=true;
                    }
                }

                else if(f1_coll)
                {

                    if (centerY + speedY >= f1_CollBrick.getY1()+10) { 
                    
                            centerY = f1_CollBrick.getY1() + 10;   
                            speedY = 0;
                            jumped = false;
                            f1_coll=false;
                            if(on_f1_brick==false)
                                on_f1_brick=true;
                    
                    }
                }


                else 
                {   // going to ground base position
                     //System.out.println("last else");
                    if (centerY + speedY >= GROUND ) {
                            centerY = GROUND ;
                            speedY = 0;
                            jumped = false;
                            on_f1_brick=false;
                            on_f2_brick=false;
                    }

                }

        }
        

    }
    
    private void handlingX()
    {
    // Here we are fixing the minimum X coordinate of mainObject. 
        if (centerX + speedX <= 160) {   
                centerX = 161;
        }
        if (speedX < 0) {   

                centerX += speedX;

              
                
                if(     (   baseBrick.getX1() >= (GameMain.rX1 + GameMain.rX2 + speedX) )
                    ||  (   (GameMain.rX1 + GameMain.rX2 +speedX < f3_CollBrick.getX1()) 
                                &&  (GameMain.rY1 + GameMain.rY2 == f3_CollBrick.getY1()) 
                        ) 
                    ||  (   (GameMain.rX1 +speedX > (f2_CollBrick.getX1()+ f2_CollBrick.getX1())) 
                            &&  (GameMain.rY1 + GameMain.rY2 > f2_CollBrick.getY1()) 
                            &&  (GameMain.rY1 + GameMain.rY2 != f3_CollBrick.getY1()) 
                            &&  (GameMain.rX1 + GameMain.rX2 +speedX +10 < f3_CollBrick.getX1())
                        )
                 )  
                {
                    
                    jumped=true;

                    centerY=baseBrick.getY1()-100;
                    beyond_base_brick=true;
                   
                }
                else    // else if the character is not dead !!!
                {
                    if(on_f3_brick==false && on_f4_brick==false && on_f5_brick==false)
                    {
                        //System.out.println("on_f3_brick==false");
                            // handling -ve X axis  , scroll back the screen
                            if(f2_coll && ((GameMain.rX1 + GameMain.rX2 + speedX -3) < f2_CollBrick.getX1()) )
                            {
                                f2_coll=false;
                                on_f2_brick=false;
                                //System.out.println("collision removed on first Brick2");

                            }
                            else
                            if(f1_coll && ((GameMain.rX1 + GameMain.rX2 + speedX -3) < f1_CollBrick.getX1()) )
                            {
                                f1_coll=false;
                                on_f1_brick=false;
                                //System.out.println("collision removed on first Brick1");

                            }
                            else if( ((GameMain.rX1 + GameMain.rX2 + speedX -3) < f1_CollBrick.getX1()) 
                                    && (GameMain.rY1 + GameMain.rY2== f1_CollBrick.getY1()) )
                            
                            {
                            //    System.out.println("collision removed on first Brick2");
                                jumped=true;
                                centerY=baseBrick.getY1()-50;
                            }
                            else if (   on_f2_brick && 
                                    ((GameMain.rX1 + GameMain.rX2 + speedX -3) < f2_CollBrick.getX1()) 
                            )
                            {
                                
                                jumped=true;
                                centerY=baseBrick.getY1()-50;
                                //System.out.println("going back from the upper brick");
                            }

                            
                            baseBrick.setX1(baseBrick.getX1()-speedX);
                            f1_CollBrick.setX1(f1_CollBrick.getX1()-speedX);
                            f2_CollBrick.setX1(f2_CollBrick.getX1()-speedX);
                            f3_CollBrick.setX1(f3_CollBrick.getX1()-speedX);
                            f4_CollBrick.setX1(f4_CollBrick.getX1()-speedX);
                            f5_CollBrick.setX1(f5_CollBrick.getX1()-speedX);
                            game_bg0.setX1(game_bg0.getX1()-speedX);
                            game_bg.setX1(game_bg.getX1()-speedX);
                            if(game_bg.getX1() >= 1800)
                            {
                                game_bg.setX1(game_bg.getX1() - 2800);
                            
                            }
                            if(game_bg0.getX1() >= 1000)
                            {
                                game_bg0.setX1(game_bg0.getX1() - 2800);
                            
                            }


                    }
                    else
                    {
                      
                        if( (GameMain.rX1 + speedX) >= (f4_CollBrick.getX1() )
                                && (GameMain.rY1 + GameMain.rY2) == (f3_CollBrick.getY1())
                                )
                            {
                            JUMPSPEED=-5;
                        }
                        else
                            JUMPSPEED=-15;

                        if( (GameMain.rX1 + GameMain.rX2 + speedX) <= (f4_CollBrick.getX1() )
                                    && (GameMain.rY1 + GameMain.rY2) == (f4_CollBrick.getY1())
                                    )
                        {

                            on_f3_brick=true;
                            on_f4_brick=false;
                            jumped=true;
                            centerY=baseBrick.getY1()-50;

                        }


                        baseBrick.setX1(baseBrick.getX1()-speedX);
                        f1_CollBrick.setX1(f1_CollBrick.getX1()-speedX);
                        f2_CollBrick.setX1(f2_CollBrick.getX1()-speedX);
                        f3_CollBrick.setX1(f3_CollBrick.getX1()-speedX);
                        f4_CollBrick.setX1(f4_CollBrick.getX1()-speedX);
                        f5_CollBrick.setX1(f5_CollBrick.getX1()-speedX);
                        game_bg0.setX1(game_bg0.getX1()-speedX);
                        game_bg.setX1(game_bg.getX1()-speedX);
                        if(game_bg.getX1() >= 1800)
                        {
                            game_bg.setX1(game_bg.getX1() - 2800);
                         //   System.out.println("AppletMain.image1X == "+AppletMain.image1X);
                        }
                        if(game_bg0.getX1() >= 1000)
                        {
                            game_bg0.setX1(game_bg0.getX1() - 2800);
                         //   System.out.println("AppletMain.image1X == "+AppletMain.image1X);
                        }

                    }//end of inner else ...

                }// end of else, if the character is not dead ...


        }
        else if (speedX == 0) {   
                /*
                 Speed zero, when u don;t press any key
                 */
             }
        
        else 
        {
                if (centerX <= 400) // 400 is the max X point 
                {   

                    //System.out.println("<=400 && speedX >0");
                        
                        
                    if(on_f3_brick==false && on_f4_brick==false && on_f5_brick==false)
                    {
                    if(GameMain.rX1 + speedX -3 >= f3_CollBrick.getX1()+ f3_CollBrick.getX2())
                    {
                        jumped=true;

                        centerY=baseBrick.getY1()-50;
                        dead=true;

                        return ;
                    }
                    if( on_f1_brick==false 
                            && (GameMain.rX1 + GameMain.rX2 + speedX -3) >= f1_CollBrick.getX1()
                    )
                    {
                        f1_coll=true;
                        //System.out.println("f1_coll = true");


                    }else 
                    if( on_f2_brick==false 
                            && (GameMain.rX1 + GameMain.rX2 + speedX -3) >= f2_CollBrick.getX1()
                    )
                    {
                        f2_coll=true;

                    }
                    if(f1_coll==false || jumped)
                    {
                        //System.out.println("+ve eeeeeeeeeeeeeee");
                        if(f2_coll==false || jumped)
                            centerX += speedX;
                    }
                    }
                    else // on_f3_brick
                    {
                        if( (GameMain.rX1 + speedX) >= (f4_CollBrick.getX1() )

                                && (GameMain.rY1 + GameMain.rY2) == (f3_CollBrick.getY1())
                                )
                                    {
                                    JUMPSPEED=-5;
                        }
                        else
                            JUMPSPEED=-15;
                            centerX += speedX;
                    }
                } 
                else 
                {
                        
                     
                    if(on_f3_brick==false && on_f4_brick==false && on_f5_brick==false)
                    {
                        if(         on_f3_brick==false 
                                && ((GameMain.rX1 + GameMain.rX2 + speedX -3) >= f1_CollBrick.getX1()) 
                                && ((GameMain.rY1 + GameMain.rY2) == f1_CollBrick.getY1()+f1_CollBrick.getY2()) 
                        )
                        {
                          f1_coll=true;
                          //System.out.println("collision on first Brick1");

                        }
                        
                        if(f1_coll==false || jumped)
                        {
                            //System.out.println("on_f1_brick == "+on_f1_brick);
                            if(     on_f3_brick==false  && on_f1_brick==false 
                                    && ((GameMain.rX1 + GameMain.rX2 + speedX -3) >= f1_CollBrick.getX1()) 
                                    &&  ((GameMain.rY1 + GameMain.rY2) <= f1_CollBrick.getY1()) 
                            )
                            {
                                f1_coll=true;
                               // System.out.println("collision on first Brick10101");

                            }
                           
                            
                            if(on_f1_brick==true && on_f2_brick==false)
                            {
                                
                                if((GameMain.rX1 + GameMain.rX2 + speedX -3) >= f2_CollBrick.getX1())
                                {
                                    //System.out.println("Collision on third brick.. f2_coll_Brick");
                                    f2_coll=true;
                                }
                            }
                            if(jumped==false && on_f2_brick==true)
                            {
                                //System.out.println("object == "+(GameMain.rX1 + GameMain.rX2 + speedX -3)
                                  //          +" f2_brick == "+f2_CollBrick.getX1());
                             if((GameMain.rX1 + speedX -3) >= (f2_CollBrick.getX1()+f2_CollBrick.getX2()))
                                {
                                
                                    jumped=true;
                                    JUMPSPEED=-15;

                                    centerY=f2_CollBrick.getY1()-50;
                                
                                }
                            
                            }
                            if(f2_coll==false || jumped )
                            {

                                if(         on_f2_brick==false 
                                        && (GameMain.rX1 + GameMain.rX2 + speedX -3) >= f2_CollBrick.getX1() 
                                        &&  (GameMain.rY1 + GameMain.rY2) <= f2_CollBrick.getY1() 
                                )
                                {
                                    f2_coll=true;
                                    //System.out.println("collision on second uper brick");

                                }

                                if(        jumped==true 
                                        && GameMain.rX1 + speedX -3 >= f2_CollBrick.getX1()+ f2_CollBrick.getX2()
                                        && GameMain.rX1 + GameMain.rX2+ speedX -3 < f3_CollBrick.getX1()
                                 )
                                {
                                   // System.out.println("Jumping on f3_brick");
                                    jump_complete=false;
                                  
                                }


                                if(GameMain.rX1 + GameMain.rX2 -5 >= f3_CollBrick.getX1()
                                        &&
                                        GameMain.rY1  + GameMain.rY2 >= f3_CollBrick.getY1()
                                        )
                                {
                                   // System.out.println("On the f3_CollBrick");
                                    on_f3_brick=true;
                                    jump_complete=true;

                                }
                                if(  (GameMain.rX1 + speedX) >= (f3_CollBrick.getX1()+f3_CollBrick.getX2())
                                        &&
                                        (GameMain.rY1 + GameMain.rY2) == (f3_CollBrick.getY1())
                                        )
                                {
                                //System.out.println("Dead xxxxxxxxxxxxxxxxxxS");
                                jumped=true;
                                JUMPSPEED=-15;

                                centerY=baseBrick.getY1()-50;
                                dead=true;

                               // return ;
                                }
                                baseBrick.setX1(baseBrick.getX1()-speedX);
                                f1_CollBrick.setX1(f1_CollBrick.getX1()-speedX);
                                f2_CollBrick.setX1(f2_CollBrick.getX1()-speedX);
                                f3_CollBrick.setX1(f3_CollBrick.getX1()-speedX);
                                f4_CollBrick.setX1(f4_CollBrick.getX1()-speedX);
                                f5_CollBrick.setX1(f5_CollBrick.getX1()-speedX);
                                game_bg0.setX1(game_bg0.getX1()-speedX);
                                game_bg.setX1(game_bg.getX1()-speedX);
                                if(game_bg.getX1() <= -1800)
                                {
                                    game_bg.setX1(game_bg.getX1() + 2800);
                                 //   System.out.println("AppletMain.image1X == "+AppletMain.image1X);
                                }
                                if(game_bg0.getX1() <= -1000)
                                {
                                    game_bg0.setX1(game_bg0.getX1() + 2800);
                                 //   System.out.println("AppletMain.image1X == "+AppletMain.image1X);
                                }
                            }
                        }
                    }
                    else    // on_f3_brick
                    {
                                if(         (GameMain.rX1 + speedX) >= (f4_CollBrick.getX1() )
                                        && (GameMain.rY1 + GameMain.rY2) == (f3_CollBrick.getY1())
                                )
                                {
                                 //   System.out.println("hidden brick area start");
                                    JUMPSPEED = -5;
                                 

                                }
                                else
                                    JUMPSPEED = -15;

                               //System.out.println("(GameMain.rX1 + speedX) == "+(GameMain.rX1 + GameMain.rX2+speedX));
                               //System.out.println("((f4_CollBrick.getX1() )) == "+(f4_CollBrick.getX1()+f4_CollBrick.getX2() ));
                              
                                if(         on_f3_brick ==true && on_f4_brick==false 
                                        &&  (GameMain.rX1 + speedX) >= (f3_CollBrick.getX1()+f3_CollBrick.getX2())
                                        &&  (GameMain.rY1 + GameMain.rY2) <= (f3_CollBrick.getY1())
                                )
                                {
                                //System.out.println("Dead ************");
                                jumped=true;
                                JUMPSPEED=-15;
                                
                                centerY=f3_CollBrick.getY1()-50;
                                f3_dead_state=true;
                                
                                }

                                 if(        on_f4_brick==false && hidden==true 
                                         && (GameMain.rX1 + speedX) >= (f4_CollBrick.getX1())
                                         && (GameMain.rY1 + GameMain.rY2) <= (f4_CollBrick.getY1()) 
                                    )
                                         {
                                             on_f4_brick=true;
                                             //System.out.println("on hidden brick");
                                         }


                                 if(         on_f5_brick==false 
                                         &&  (GameMain.rX1 + speedX + GameMain.rX2) >= (f5_CollBrick.getX1())
                                         &&  (GameMain.rY1 + GameMain.rY2) <= (f5_CollBrick.getY1()) 
                                    )
                                    {
                                             on_f5_brick=true;
                                           //  System.out.println("on final brick");
                                           //  System.out.println("you won");

                                    }    
                             

                                 if( on_f4_brick ==true && on_f5_brick==false &&
                                        (GameMain.rX1 + speedX ) >= (f4_CollBrick.getX1()+f4_CollBrick.getX2())
                                        &&
                                        (GameMain.rY1 + GameMain.rY2) <= (f4_CollBrick.getY1())
                                        )
                                {
                               // System.out.println("Dead 5555555555");
                                jumped=true;
                                JUMPSPEED=-15;

                                centerY=baseBrick.getY1()-50;
                                f4_dead_state=true;
                                }


                                baseBrick.setX1(baseBrick.getX1()-speedX);
                                f1_CollBrick.setX1(f1_CollBrick.getX1()-speedX);
                                f2_CollBrick.setX1(f2_CollBrick.getX1()-speedX);
                                f3_CollBrick.setX1(f3_CollBrick.getX1()-speedX);
                                f4_CollBrick.setX1(f4_CollBrick.getX1()-speedX);
                                f5_CollBrick.setX1(f5_CollBrick.getX1()-speedX);
                                
                                game_bg0.setX1(game_bg0.getX1()-speedX);
                                game_bg.setX1(game_bg.getX1()-speedX);
                                if(game_bg.getX1() <= -1800)
                                {
                                    game_bg.setX1(game_bg.getX1() + 2800);
                                 //   System.out.println("AppletMain.image1X == "+AppletMain.image1X);
                                }
                                if(game_bg0.getX1() <= -1000)
                                {
                                    game_bg0.setX1(game_bg0.getX1() + 2800);
                                 //   System.out.println("AppletMain.image1X == "+AppletMain.image1X);
                                }
                    }
                    
                }// end of else, background scrolling
        }
    }// end of function
   
    public void updateXY()
    {
        if(last==true)
        {
         
            if(game_bg0.getX1()>0)
            {

                speedX=MOVESPEED;
                baseBrick.setX1(baseBrick.getX1()-speedX);
                f1_CollBrick.setX1(f1_CollBrick.getX1()-speedX);
                f2_CollBrick.setX1(f2_CollBrick.getX1()-speedX);
                f3_CollBrick.setX1(f3_CollBrick.getX1()-speedX);
                f4_CollBrick.setX1(f4_CollBrick.getX1()-speedX);
                f5_CollBrick.setX1(f5_CollBrick.getX1()-speedX);
                game_bg0.setX1(game_bg0.getX1()-speedX);
                game_bg.setX1(game_bg.getX1()-speedX);
                GameMain.youWon=true;
                last=false;
                
            }
                
        }
        else if(!dead)
        {
            handlingX();
            handlingY();
        }
        
    }

      
    public void moveRight() {
  if (ducked == false) {
   speedX = MOVESPEED;
  }
 }

 public void moveLeft() {
  if (ducked == false) {
   speedX = -MOVESPEED;
  }
 }

 public void stopRight() {
  setMovingRight(false);
  stop();
 }

 public void stopLeft() {
  setMovingLeft(false);
  stop();
 }

 private void stop() {
  if (isMovingRight() == false && isMovingLeft() == false) {
   speedX = 0;
  }

  if (isMovingRight() == false && isMovingLeft() == true) {
   moveLeft();
  }

  if (isMovingRight() == true && isMovingLeft() == false) {
   moveRight();
  }

 }

 public void jump() {
  if (jumped == false) {
   speedY = JUMPSPEED;
   jumped = true;
  }

 }

        public boolean getHidden()
        {
            return hidden;
        }
 public int getCenterX() {
  return centerX;
 }

 public int getCenterY() {
  return centerY;
 }

 public boolean isJumped() {
  return jumped;
 }

 public int getSpeedX() {
  return speedX;
 }

 public int getSpeedY() {
  return speedY;
 }

 public void setCenterX(int centerX) {
  this.centerX = centerX;
 }

 public void setCenterY(int centerY) {
  this.centerY = centerY;
 }

 public void setJumped(boolean jumped) {
  this.jumped = jumped;
 }

 public void setSpeedX(int speedX) {
  this.speedX = speedX;
 }

 public void setSpeedY(int speedY) {
  this.speedY = speedY;
 }

 public boolean isDucked() {
  return ducked;
 }

 public void setDucked(boolean ducked) {
  this.ducked = ducked;
 }

 public boolean isMovingRight() {
  return movingRight;
 }

 public void setMovingRight(boolean movingRight) {
  this.movingRight = movingRight;
 }

 public boolean isMovingLeft() {
  return movingLeft;
 }

 public void setMovingLeft(boolean movingLeft) {
  this.movingLeft = movingLeft;
 }
        public void resetAll()
        {
        
            jump_complete=true;
            hidden=false;
            dead=false;
            beyond_base_brick=false;
            f3_dead_state=false;
            f4_dead_state=false;
            JUMPSPEED = -15;
            centerX = 100;
            centerY = GROUND;
            jumped = false;
            movingLeft = false;
            movingRight = false;
            ducked = false;

            speedX = 0;
            speedY = 1;

            f1_coll=false;
            on_f1_brick=false;

            f2_coll=false;
            on_f2_brick=false;

            on_f3_brick=false;
            on_f4_brick=false;
            on_f5_brick=false;

            game_bg = GameMain.getchBricks1();
            game_bg0 = GameMain.getchBricks2();
            baseBrick = GameMain.getBricks1();
            f1_CollBrick = GameMain.getBricks2();
            f2_CollBrick = GameMain.getBricks3();
            f3_CollBrick = GameMain.getBricks4();
            f4_CollBrick = GameMain.getBricks5();
            f5_CollBrick = GameMain.getBricks6();
     
        }

}


/************************************************/
Bricks.java
/************************************************/

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author T_ShobhitU1
 */
public class Bricks {
    private int bX1;
    private int bY1;
    private int bX2;
    private int bY2;
    
    
    public Bricks(int x1,int y1, int x2, int y2)
    {
       // System.out.println("constructor of Bricks");
        bX1=x1;
        bY1=y1;
        
        bX2=x2;
        bY2=y2;
    }
    
    /*  getter */
    public int getX1() {
  return bX1;
 }
    public int getX2() {
  return bX2;
 }
    public int getY1() {
  return bY1;
 }
    public int getY2() {
  return bY2;
 }
    
    /* setter */
    public void setX1(int x1) {
            this.bX1=x1;
 } 
    public void setX2(int x2) {
            this.bX2=x2;
 }
    public void setY1(int y1) {
            this.bY1=y1;
 }
    public void setY2(int y2) {
            this.bY2=y2;
 }
        
}


Let's see what are the images we need.


1)    images/dead.png
2)  images/small_image1.png

3)   images/small_normal.png

4)   images/small_up.png


5)   images/small_down.png

6)   images/start_game.png

7)    images/game_over.png

8)    images/you_won.png

9)    images/base_brick.png

10)    images/common_brick.png

11)    images/hidden_brick.png

12)    images/final_brick.png

13)    images/game_background.png

14)  images/game_background2.png



You have to follow the same procedure what i have described in the previous part.
Create a new game Project. Create an Applet GameMain.java, two java classes HandlingXY.java and Bricks.java.
Bricks.java is new here this updates the X,Y co-ordinate of the Bricks. 
Create an images folder in the src directory. 
Copy all the images in the images folder.
Click on the GameMain.java and press SHIFT+F6 to run the Applet.


What are going to learn in the Android Game Development [Part 3]
1) We will setup ADT (Android Development Toolkit).
2) We will learn, how to create "Hello World" application in Android.
3) We will learn, how to port the "Hello World" application on our android cellphone.


One more thing i would like to say is, when you play the game you will find some small small bugs. My main object is "To give you and idea of how we can start game development". That is why i have not fixed those bugs.
Once you learn the concept i am damn sure you will develop great games.

Instruction to play the Game:-
1) Left Arrow key for move backward.
2) Right Arrow key for move forward.
3) Down Arrow key for move down.
4) Space for Jump.
5) Enter to start the game.

Following is the image of the Game :-




Enjoy the Game Buddies. 






Game Development "Part 1"                              



Please do your comments or suggestions on my blog regarding this Android Game Development Tutorial. Your comments can give me motivation and its also help me to improve myself.

Share this blog with your buddies.



Thanks and Cheers                      

1 comment: