Tuesday 12 March 2013

Android Game Development Tutorial [ Part6 ]


Hello friends,
            Welcome you all in the Android Game Development [Part6].

In the part5 we were setting up the Game Architecture. We have created the interfaces and we have also created the implementation classes that use those interfaces.

In this part6, we will continue our Game Architecture setup.

Setting up the Game Architecture:-
In the previous part we have completed till step 9. We will continue from there.
10)    Copy and paste the following images into the assets folder.
·        dead.png

·        base_brick.png

·        common_brick.png

·        hidden_brick.png

·        final_brick.png

·        small_image1.png

·        small_normal.png

·        small_down.png

·        small_up.png

·        game_background.png


·        game_background2.png


·        menu.png 

·        button.jpg

·        splash.jpg


·        menutheme.mp3
     Copy any mp3 file into assets folder and rename it to menutheme.mp3


11)   Copy the icon.png image file into res à drawable-hdpi folder

     Our game architecture setup has been done now. Next step is to the add the gamecode.


Merging the GameCode :-
    Now we are at the step of merging the final game code what we have developed for the java application.

1)    Create a new package.
Right click on the src directory à New à package.
Name it as com.pocogame.gamecode

2)    Create the following classes
a.     Assets.java
b.     Background.java
c.      Bricks.java
d.     HandlingXY.java
e.     GameScreen.java
f.       LoadingScreen.java
g.     MainMenuScreen.java
h.     SampleGame.java
i.        SplashLoadingScreen.java

3)    Copy and paste the following code into the respective file.
a.     ************* Assets.java *************
package com.pocogame.gamecode;

import com.pocogame.framework.Image;
import com.pocogame.framework.Music;
import com.pocogame.framework.Sound;

public class Assets {
 
 public static Image image, character, bg1, bg2, bg3;
 public static Image currentObject, character1, characterDown, characterJumped, background;
 public static Image dead_character, base_brick,common_brick,hidden_brick,final_brick,game_background2;
    
 public static Image menu, splash, character2, character3;
 
 public static Image button;
 public static Sound click;
 public static Music theme;
 
 public static void load(SampleGame sampleGame) {
  // TODO Auto-generated method stub
  theme = sampleGame.getAudio().createMusic("menutheme.mp3");
  theme.setLooping(true);
  theme.setVolume(0.85f);
  theme.play();
 }
 
}


b.     ************* Background.java *************
package com.pocogame.gamecode;


public class Background {
 
 private int bgX, bgY, speedX;
 
 public Background(int x, int y){
  bgX = x;
  bgY = y;
  speedX = 0;
 }
 
 public void update() {
  bgX += speedX;

  if (bgX <= -2160){
   bgX += 4320;
  }
 }

 public int getBgX() {
  return bgX;
 }

 public int getBgY() {
  return bgY;
 }

 public int getSpeedX() {
  return speedX;
 }

 public void setBgX(int bgX) {
  this.bgX = bgX;
 }

 public void setBgY(int bgY) {
  this.bgY = bgY;
 }

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

 
 
 
}


c.      ************* Bricks.java *************
package com.pocogame.gamecode;

/*
 * 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;
 }
        
}

d.     ************* HandlingXY.java *************
package com.pocogame.gamecode;

import android.util.Log;

/**
*
* @author Shobhit Upadhyaya
*/
public class HandlingXY {
 private static final String TAG = "MyActivity";

       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 = GameScreen.getchBricks1();
       private Bricks game_bg0 = GameScreen.getchBricks2();
       private Bricks baseBrick = GameScreen.getBricks1();
       private Bricks f1_CollBrick = GameScreen.getBricks2();
       private Bricks f2_CollBrick = GameScreen.getBricks3();
       private Bricks f3_CollBrick = GameScreen.getBricks4();
       private Bricks f4_CollBrick = GameScreen.getBricks5();
       private Bricks f5_CollBrick = GameScreen.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;
                           GameScreen.isDead=true;
                   }
               }
               else
               if(dead || beyond_base_brick )
               {
                   if (centerY + speedY >= GROUND+50) {
                           centerY = GROUND+50;   
                           speedY = 0;
                           jumped = false;
                           GameScreen.isDead=true;
                           dead=true;
                           Log.v(TAG,"xxxxxxxx  beyond_base_brick, game_over  xxxx\n");
                   }
               }
               else if(f4_dead_state)
               {
                   if (centerY + speedY >= GROUND +50) {
                           centerY = GROUND+50;   
                           speedY = 0;
                           dead=true;
                           GameScreen.isDead=true;
                           jumped = false;
                   }
               }
               else if(f3_dead_state)
               {
                   if (centerY + speedY >= GROUND +50) {
                           centerY = GROUND+50;   
                           speedY = 0;
                           dead=true;
                           GameScreen.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 >= f3_CollBrick.getY1() +10) {
                       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;
                           //GameScreen.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() >= (GameScreen.rX1 + GameScreen.rX2 + speedX) )
                   ||  (   (GameScreen.rX1 + GameScreen.rX2 +speedX < f3_CollBrick.getX1()) 
                               &&  (GameScreen.rY1 + GameScreen.rY2 == f3_CollBrick.getY1()) 
                       ) 
                   ||  (   (GameScreen.rX1 +speedX > (f2_CollBrick.getX1()+ f2_CollBrick.getX1())) 
                           &&  (GameScreen.rY1 + GameScreen.rY2 > f2_CollBrick.getY1()) 
                           &&  (GameScreen.rY1 + GameScreen.rY2 != f3_CollBrick.getY1()) 
                           &&  (GameScreen.rX1 + GameScreen.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 && ((GameScreen.rX1 + GameScreen.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 && ((GameScreen.rX1 + GameScreen.rX2 + speedX -3) < f1_CollBrick.getX1()) )
                           {
                               f1_coll=false;
                               on_f1_brick=false;
                               //System.out.println("collision removed on first Brick1");

                           }
                           else if( ((GameScreen.rX1 + GameScreen.rX2 + speedX -3) < f1_CollBrick.getX1()) 
                                   && (GameScreen.rY1 + GameScreen.rY2== f1_CollBrick.getY1()) )
                           
                           {
                           //    System.out.println("collision removed on first Brick2");
                               jumped=true;
                               centerY=baseBrick.getY1()-50;
                           }
                           else if (   on_f2_brick && 
                                   ((GameScreen.rX1 + GameScreen.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( (GameScreen.rX1 + speedX) >= (f4_CollBrick.getX1() )
                               && (GameScreen.rY1 + GameScreen.rY2) == (f3_CollBrick.getY1())
                               )
                           {
                           JUMPSPEED=-5;
                       }
                       else
                           JUMPSPEED=-15;

                       if( (GameScreen.rX1 + GameScreen.rX2 + speedX) <= (f4_CollBrick.getX1() )
                                   && (GameScreen.rY1 + GameScreen.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(GameScreen.rX1 + speedX -3 >= f3_CollBrick.getX1()+ f3_CollBrick.getX2())
                   {
                       jumped=true;

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

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


                   }else 
                   if( on_f2_brick==false 
                           && (GameScreen.rX1 + GameScreen.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( (GameScreen.rX1 + speedX) >= (f4_CollBrick.getX1() )

                               && (GameScreen.rY1 + GameScreen.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 
                               && ((GameScreen.rX1 + GameScreen.rX2 + speedX -3) >= f1_CollBrick.getX1()) 
                               && ((GameScreen.rY1 + GameScreen.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 
                                   && ((GameScreen.rX1 + GameScreen.rX2 + speedX -3) >= f1_CollBrick.getX1()) 
                                   &&  ((GameScreen.rY1 + GameScreen.rY2) <= f1_CollBrick.getY1()) 
                           )
                           {
                               f1_coll=true;
                              // System.out.println("collision on first Brick10101");

                           }
                          
                           
                           if(on_f1_brick==true && on_f2_brick==false)
                           {
                               
                               if((GameScreen.rX1 + GameScreen.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 == "+(GameScreen.rX1 + GameScreen.rX2 + speedX -3)
                                 //          +" f2_brick == "+f2_CollBrick.getX1());
                            if((GameScreen.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 
                                       && (GameScreen.rX1 + GameScreen.rX2 + speedX -3) >= f2_CollBrick.getX1() 
                                       &&  (GameScreen.rY1 + GameScreen.rY2) <= f2_CollBrick.getY1() 
                               )
                               {
                                   f2_coll=true;
                                   //System.out.println("collision on second uper brick");

                               }

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


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

                               }
                               if(  (GameScreen.rX1 + speedX) >= (f3_CollBrick.getX1()+f3_CollBrick.getX2())
                                       &&
                                       (GameScreen.rY1 + GameScreen.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(         (GameScreen.rX1 + speedX) >= (f4_CollBrick.getX1() )
                                       && (GameScreen.rY1 + GameScreen.rY2) == (f3_CollBrick.getY1())
                               )
                               {
                                //   System.out.println("hidden brick area start");
                                   JUMPSPEED = -5;
                                

                               }
                               else
                                   JUMPSPEED = -15;

                              //System.out.println("(GameScreen.rX1 + speedX) == "+(GameScreen.rX1 + GameScreen.rX2+speedX));
                              //System.out.println("((f4_CollBrick.getX1() )) == "+(f4_CollBrick.getX1()+f4_CollBrick.getX2() ));
                             
                               if(         on_f3_brick ==true && on_f4_brick==false 
                                       &&  (GameScreen.rX1 + speedX) >= (f3_CollBrick.getX1()+f3_CollBrick.getX2())
                                       &&  (GameScreen.rY1 + GameScreen.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 
                                        && (GameScreen.rX1 + speedX) >= (f4_CollBrick.getX1())
                                        && (GameScreen.rY1 + GameScreen.rY2) <= (f4_CollBrick.getY1()) 
                                   )
                                        {
                                            on_f4_brick=true;
                                            //System.out.println("on hidden brick");
                                        }


                                if(         on_f5_brick==false 
                                        &&  (GameScreen.rX1 + speedX + GameScreen.rX2) >= (f5_CollBrick.getX1())
                                        &&  (GameScreen.rY1 + GameScreen.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 &&
                                       (GameScreen.rX1 + speedX ) >= (f4_CollBrick.getX1()+f4_CollBrick.getX2())
                                       &&
                                       (GameScreen.rY1 + GameScreen.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);
               GameScreen.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 = GameScreen.getchBricks1();
           game_bg0 = GameScreen.getchBricks2();
           baseBrick = GameScreen.getBricks1();
           f1_CollBrick = GameScreen.getBricks2();
           f2_CollBrick = GameScreen.getBricks3();
           f3_CollBrick = GameScreen.getBricks4();
           f4_CollBrick = GameScreen.getBricks5();
           f5_CollBrick = GameScreen.getBricks6();
    
       }

}





e.     ************* GameScreen.java *************
package com.pocogame.gamecode;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;

import com.pocogame.framework.Game;
import com.pocogame.framework.Graphics;
import com.pocogame.framework.Image;
import com.pocogame.framework.Input.TouchEvent;
import com.pocogame.framework.Screen;

public class GameScreen extends Screen {
 enum GameState {
  Ready, Running, Paused, GameOver, YouWon
 }
 private static final String TAG = "MyActivity";
 GameState state = GameState.Ready;

 // Variable Setup
 static public int image3X=-1000;
 static public int image1X=0;
 static public int image2X=1000;

 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;
    HandlingXY mainObject;
    
    static boolean initial_img = true;
    
 private static Background bg1, bg2;

 private Image currentObject, character1, characterDown, characterJumped, character;
 

 int livesLeft = 1;
 Paint paint, paint2;

 public GameScreen(Game game) {
  super(game);

  // Initialize game objects here
  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();

  currentObject = Assets.character;
  character1 = Assets.character1;
  characterDown = Assets.characterDown;
  characterJumped = Assets.characterJumped;
  
  character = Assets.character;


  // Defining a paint object
  paint = new Paint();
  paint.setTextSize(30);
  paint.setTextAlign(Paint.Align.CENTER);
  paint.setAntiAlias(true);
  paint.setColor(Color.WHITE);

  paint2 = new Paint();
  paint2.setTextSize(100);
  paint2.setTextAlign(Paint.Align.CENTER);
  paint2.setAntiAlias(true);
  paint2.setColor(Color.WHITE);

 }

 private void loadMap() {
  ArrayList lines = new ArrayList();
  int width = 0;
  int height = 0;

  Scanner scanner = new Scanner(SampleGame.map);
  while (scanner.hasNextLine()) {
   String line = scanner.nextLine();

   // no more lines to read
   if (line == null) {
    break;
   }

   if (!line.startsWith("!")) {
    lines.add(line);
    width = Math.max(width, line.length());

   }
  }
  height = lines.size();

  for (int j = 0; j < 12; j++) {
   String line = (String) lines.get(j);
   for (int i = 0; i < width; i++) {

    if (i < line.length()) {
     char ch = line.charAt(i);
    }

   }
  }

 }

 @Override
 public void update(float deltaTime) {
  List touchEvents = game.getInput().getTouchEvents();
  if(isGameOver==1)
   state=GameState.GameOver;

  // We have four separate update methods in this example.
  // Depending on the state of the game, we call different update methods.
  // Refer to Unit 3's code. We did a similar thing without separating the
  // update methods.

  if (state == GameState.Ready)
   updateReady(touchEvents);
  if (state == GameState.Running)
   updateRunning(touchEvents, deltaTime);
  if (state == GameState.Paused)
   updatePaused(touchEvents);
  if (state == GameState.GameOver)
   updateGameOver(touchEvents);
  if (state == GameState.YouWon)
   updateYouWon(touchEvents);
 }

 private void updateReady(List touchEvents) {

  // This example starts with a "Ready" screen.
  // When the user touches the screen, the game begins.
  // state now becomes GameState.Running.
  // Now the updateRunning() method will be called!

  if (touchEvents.size() > 0)
   state = GameState.Running;
 }

 private void updateRunning(List touchEvents, float deltaTime) {

  // This is identical to the update() method from our Unit 2/3 game.

  // 1. All touch input is handled here:
  int len = touchEvents.size();
  for (int i = 0; i < len; i++) {
   TouchEvent event = touchEvents.get(i);
   if (event.type == TouchEvent.TOUCH_DOWN) {
    if (inBounds(event, 0, 220, 65, 65)) { // UP
     
     mainObject.jump();
     currentObject = characterJumped;
     mainObject.setDucked(false);
    }else
    if (inBounds(event, 0, 285, 65, 65) 
      && mainObject.isJumped() == false) { //DOWN
      currentObject = characterDown;
      mainObject.setDucked(true);
      mainObject.setSpeedX(0);
      
    }
     
    else if (inBounds(event, 0, 350, 65, 65)) { //RIGHT

     if (mainObject.isDucked() == false && mainObject.isJumped() == false)
     {
      if(mainObject.isMovingRight()==true)
      { mainObject.stopRight();
      
      }
      else
      {
      mainObject.moveRight();
      mainObject.setMovingRight(true);
      }
      currentObject = character1;

     }
    }

    else if (inBounds(event, 0, 415, 65, 65)){ //LEFT
     if(mainObject.isMovingLeft()==true)
     { mainObject.stopLeft();
     
     }
     else
     {
     
     mainObject.moveLeft();
     mainObject.setMovingLeft(true);
     }

     currentObject = character1;

    }

    if (event.x > 400) {
     // Move right.
      mainObject.moveRight();
      mainObject.setMovingRight(true);

    }

   }

   if (event.type == TouchEvent.TOUCH_UP) {

    if (inBounds(event, 0, 285, 65, 65)) {
     //currentSprite = anim.getImage();
     mainObject.setDucked(false);

    }

    if (inBounds(event, 0, 0, 85, 85)) {
     pause();

    }

    if (event.x > 400) {
     // Move right.
     mainObject.stopRight();
    }
   }

  }

  // 2. Check miscellaneous events like death:
  if(isDead==true)
   state=GameState.GameOver;
  
  if(youWon==true)
   state=GameState.YouWon;
  

  // 3. Call individual update() methods here.
  // This is where all the game updates happen.
  mainObject.updateXY();
  
  if(mainObject.isJumped())
    
  {
   currentObject = characterJumped;

  }
   else if (mainObject.isJumped() == false && mainObject.isDucked() == false)
         {
                currentObject = character1;
         }
          rX1=mainObject.getCenterX() -55; 
          rY1=mainObject.getCenterY() -45; 
         
          
 }

 private boolean inBounds(TouchEvent event, int x, int y, int width,
   int height) {
  if (event.x > x && event.x < x + width - 1 && event.y > y
    && event.y < y + height - 1)
   return true;
  else
   return false;
 }

 private void updatePaused(List touchEvents) {
  int len = touchEvents.size();
  for (int i = 0; i < len; i++) {
   TouchEvent event = touchEvents.get(i);
   if (event.type == TouchEvent.TOUCH_UP) {
    if (inBounds(event, 0, 0, 800, 240)) {

     if (!inBounds(event, 0, 0, 35, 35)) {
      resume();
     }
    }

    if (inBounds(event, 0, 240, 800, 240)) {
     nullify();
     goToMenu();
    }
   }
  }
 }

 private void updateGameOver(List touchEvents) {
  int len = touchEvents.size();
  for (int i = 0; i < len; i++) {
   TouchEvent event = touchEvents.get(i);
   if (event.type == TouchEvent.TOUCH_DOWN) {
    if (inBounds(event, 0, 0, 800, 480)) {
     nullify();
     game.setScreen(new MainMenuScreen(game));
     return;
    }
   }
  }

 }
 
 private void updateYouWon(List touchEvents) {
  int len = touchEvents.size();
  for (int i = 0; i < len; i++) {
   TouchEvent event = touchEvents.get(i);
   if (event.type == TouchEvent.TOUCH_DOWN) {
    if (inBounds(event, 0, 0, 800, 480)) {
     nullify();
     game.setScreen(new MainMenuScreen(game));
     return;
    }
   }
  }

 }
 
 

 @Override
 public void paint(float deltaTime) {
  Graphics g = game.getGraphics();

  g.drawImage(Assets.background,ch_bk1.getX1(),ch_bk1.getY1());
        g.drawImage(Assets.game_background2,ch_bk2.getX1(),ch_bk2.getY1());

        g.drawImage(Assets.base_brick,bk1.getX1(),bk1.getY1());
        g.drawImage(Assets.common_brick,bk2.getX1(),bk2.getY1());
        g.drawImage(Assets.common_brick,bk3.getX1(),bk3.getY1());
        g.drawImage(Assets.base_brick,bk4.getX1(),bk4.getY1());

        if(mainObject.getHidden())
            g.drawImage(Assets.hidden_brick,bk5.getX1(),bk5.getY1());
        
        g.drawImage(Assets.final_brick,bk6.getX1(),bk6.getY1());

        g.drawImage(currentObject, 
                mainObject.getCenterX() - 61, 
                mainObject.getCenterY() - 63);

  // Secondly, draw the UI above the game elements.
  if (state == GameState.Ready)
   drawReadyUI();
  if (state == GameState.Running)
   drawRunningUI();
  if (state == GameState.Paused)
   drawPausedUI();
  if (state == GameState.GameOver)
   drawGameOverUI();
  if (state == GameState.YouWon)
   drawYouWonUI();

 }


 private void nullify() {

  // Set all variables to null. You will be recreating them in the
  // constructor.
  isDead=false;
  youWon=false;
  paint = null;
  bg1 = null;
  bg2 = null;
  character = null;
 
  // Call garbage collector to clean up memory.
  System.gc();

 }

 private void drawReadyUI() {
  Graphics g = game.getGraphics();

  g.drawARGB(155, 0, 0, 0);
  g.drawString("Tap to Start.", 400, 240, paint);
        Log.v(TAG,"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");


 }

 private void drawRunningUI() {
  Graphics g = game.getGraphics(); //shobhit
  g.drawImage(Assets.button, 0, 220, 0, 0, 65, 65);
  g.drawImage(Assets.button, 0, 285, 0, 65, 65, 65);
  g.drawImage(Assets.button, 0, 350, 0, 130, 65, 65);
  g.drawImage(Assets.button, 0, 415, 0, 195, 65, 65);
  g.drawImage(Assets.button, 0, 0, 0, 260, 65, 65);

 }

 private void drawPausedUI() {
  Graphics g = game.getGraphics();
  // Darken the entire screen so you can display the Paused screen.
  g.drawARGB(155, 0, 0, 0);
  g.drawString("Resume", 400, 165, paint2);
  g.drawString("Menu", 400, 360, paint2);

 }

 private void drawGameOverUI() {
  Graphics g = game.getGraphics();
  g.drawRect(0, 0, 1281, 801, Color.BLACK);
  g.drawString("GAME OVER.", 400, 240, paint2);
  g.drawString("Tap to return.", 400, 290, paint);

 }
 
 private void drawYouWonUI() {
  Graphics g = game.getGraphics();
  g.drawRect(0, 0, 1281, 801, Color.BLACK);
  g.drawString("You Won.", 400, 240, paint2);
  g.drawString("Tap to return.", 400, 290, paint);

 }

 @Override
 public void pause() {
  if (state == GameState.Running)
   state = GameState.Paused;

 }

 @Override
 public void resume() {
  if (state == GameState.Paused)
   state = GameState.Running;
 }

 @Override
 public void dispose() {

 }

 @Override
 public void backButton() {
  pause();
 }

 private void goToMenu() {
  // TODO Auto-generated method stub
  game.setScreen(new MainMenuScreen(game));

 }

 public static Background getBg1() {
  // TODO Auto-generated method stub
  return bg1;
 }

 public static Background getBg2() {
  // TODO Auto-generated method stub
  return bg2;
 }

 /*public static Robot getRobot() {
  // TODO Auto-generated method stub
  return robot;
 }*/

 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;
    }
}

f.       ************* LoadingScreen.java *************
package com.pocogame.gamecode;

import com.pocogame.framework.Game;
import com.pocogame.framework.Graphics;
import com.pocogame.framework.Graphics.ImageFormat;
import com.pocogame.framework.Screen;

public class LoadingScreen extends Screen {
 public LoadingScreen(Game game) {
  
  super(game);
 }

 @Override
 public void update(float deltaTime) {
  Graphics g = game.getGraphics();

  Assets.dead_character = g.newImage("dead.png", ImageFormat.ARGB4444);
  Assets.base_brick  = g.newImage("base_brick.png", ImageFormat.ARGB4444);
  Assets.common_brick  = g.newImage("common_brick.png", ImageFormat.ARGB4444);
  Assets.hidden_brick  = g.newImage("hidden_brick.png", ImageFormat.ARGB4444);
  Assets.final_brick  = g.newImage("final_brick.png", ImageFormat.ARGB4444);
  Assets.character  = g.newImage("small_image1.png", ImageFormat.ARGB4444);
  Assets.character1  = g.newImage("small_normal.png", ImageFormat.ARGB4444);
  Assets.characterDown = g.newImage("small_down.png", ImageFormat.ARGB4444);
  Assets.characterJumped = g.newImage("small_up.png", ImageFormat.ARGB4444);
  Assets.background  = g.newImage("game_background.png", ImageFormat.ARGB4444);
  Assets.game_background2 = g.newImage("game_background2.png", ImageFormat.ARGB4444);
  
  
  Assets.menu = g.newImage("menu.png", ImageFormat.RGB565);
  Assets.button = g.newImage("button.jpg", ImageFormat.RGB565); 
  game.setScreen(new MainMenuScreen(game));

 }

 @Override
 public void paint(float deltaTime) {
  Graphics g = game.getGraphics();
  g.drawImage(Assets.splash, 0, 0);
 }

 @Override
 public void pause() {

 }

 @Override
 public void resume() {

 }

 @Override
 public void dispose() {

 }

 @Override
 public void backButton() {

 }
}

g.     ************* MainMenuScreen.java *************
package com.pocogame.gamecode;

import java.util.List;

import com.pocogame.framework.Game;
import com.pocogame.framework.Graphics;
import com.pocogame.framework.Screen;
import com.pocogame.framework.Input.TouchEvent;

public class MainMenuScreen extends Screen {
 public MainMenuScreen(Game game) {
  super(game);
 }

 @Override
 public void update(float deltaTime) {
  Graphics g = game.getGraphics();
  List touchEvents = game.getInput().getTouchEvents();

  int len = touchEvents.size();
  for (int i = 0; i < len; i++) {
   TouchEvent event = touchEvents.get(i);
   if (event.type == TouchEvent.TOUCH_UP) {

    if (inBounds(event, 50, 350, 250, 450)) {
     game.setScreen(new GameScreen(game));
    }

   }
  }
 }

 private boolean inBounds(TouchEvent event, int x, int y, int width,
   int height) {
  if (event.x > x && event.x < x + width - 1 && event.y > y
    && event.y < y + height - 1)
   return true;
  else
   return false;
 }

 @Override
 public void paint(float deltaTime) {
  Graphics g = game.getGraphics();
  g.drawImage(Assets.menu, 0, 0);
 }

 @Override
 public void pause() {
 }

 @Override
 public void resume() {

 }

 @Override
 public void dispose() {

 }

 @Override
 public void backButton() {
        android.os.Process.killProcess(android.os.Process.myPid());

 }
}


h.     ************* SampleGame.java *************
package com.pocogame.gamecode;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.util.Log;

import com.pocogame.framework.Screen;
import com.pocogame.framework.implementation.AndroidGame;

public class SampleGame extends AndroidGame {

 public static String map;
 boolean firstTimeCreate = true;

 @Override
 public Screen getInitScreen() {

  if (firstTimeCreate) {
   Assets.load(this);
   firstTimeCreate = false;
  }

  return new SplashLoadingScreen(this);

 }

 @Override
 public void onBackPressed() {
  getCurrentScreen().backButton();
 }

 

 @Override
 public void onResume() {
  super.onResume();

  Assets.theme.play();

 }

 @Override
 public void onPause() {
  super.onPause();
  Assets.theme.pause();

 }
 
 
}

i.        ************* SplashLoadingScreen.java *************
package com.pocogame.gamecode;

import com.pocogame.framework.Game;
import com.pocogame.framework.Graphics;
import com.pocogame.framework.Screen;
import com.pocogame.framework.Graphics.ImageFormat;

public class SplashLoadingScreen extends Screen {
 public SplashLoadingScreen(Game game) {
  super(game);
 }

 @Override
 public void update(float deltaTime) {
  Graphics g = game.getGraphics();
  Assets.splash= g.newImage("splash.jpg", ImageFormat.RGB565);

  
  game.setScreen(new LoadingScreen(game));

 }

 @Override
 public void paint(float deltaTime) {

 }

 @Override
 public void pause() {

 }

 @Override
 public void resume() {

 }

 @Override
 public void dispose() {

 }

 @Override
 public void backButton() {

 }
}

4)    Update the AndroidManifest.xml file


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pocogame.gamecode"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.VIBRATE" />
    
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="Pocogame" >
        
        <activity
            android:name=".SampleGame"
            android:configChanges="keyboard|keyboardHidden|orientation"
            android:label="Pocogame"
            android:screenOrientation="landscape" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


   Assets classIn this class we are creating reference variables to all assets (images/audio) that will be used in our game.

SplashLoadingScreen & LoadingScreen – SplashLoadingScreen class will be used to load the splash screen. Loading Screen class will be used to load the menu screen.


Main Menu Screen - Used to start the game.

GameScreen - All the gameplay will take place in this class. This class is similar to our GameMain class.

SampleGame - The Activity class that is launched when the game starts. It opens our application.

 As you know activity class is like a class which have the main() func. Here in the AndroidManifest.xml file we are giving the activity class name. 
So the SampleGame.java class got called first. getInitScreen() when it calls first time, we load all the assets and create a object of SplashLoadingScreen.

5)    Run the application and test it on the Emulator. You can port this application on your android device also.
   
You can check the Android Game Development Tutorial [ Part 3 ] to know more how to run the android application on Emulator or an Android Device.




Enjoy the game buddies.


Please give your suggestions and share it with your buddies.









No comments:

Post a Comment