Bombastiz
  • Home
  • Tutorial Game DropOff Menggunakan J2ME.

    Cara membuat game DropOff menggunakan J2ME (Java 2 Micro Edition), sebelum membuat game J2ME tersebut kita harus mengetahui apa itu J2ME? J2ME merupakan bahasa pemrograman yang dikhususkan untuk mobile, J2ME sendiri bagian dari bahasa pemrograman java.
          Sebelum membuat game ini, pastikan PC atau Laptop anda sudah terinstall JDK, Sun Java (TM) Wireless Toolkit dan Editor java(misalnya : Notepad, Jcreator, crimson, dll.). JDK berguna untuk disini saya menggunakan jdk-6u6-windows-i586-p, Sun Java(TM) Wireless Toolkit berguna sebagai emulator yang bertujuan untuk menampilkan output disini saya menggunakan Sun Java(TM) Wireless Toolkit 2.5.2 dan editor berguna untuk tempat penulisan script program disini saya menggunakan JCreator 3.50.
       Untuk membuat game DropOff ini menggunakan 3 file yaitu dropOff.java, dropOffCanvas.java, GameThread.java (disarankan besar kecil huruf sama seperti saya), berikut ini cara-cara untuk membuat ketiga file tersebut :

    Langkah 1 : Setelah semua yang dibutuhkan untuk pembuatan game ini, maka anda buka Jcreator (disini saya menggunakan Jcreator) kemudian pilih File/New/File..(ctrl+N) kemudian akan muncul seperti ini :

            
    Langkah 2 : Kemudian didalam name ketikkan DropOff yang artinya name file tersebut bernama DropOff, setelah itu klik finish berikut ini tampilannya :



    Langkah 3 : Didalam kolom yang ditunjuk tanda panah tempat untuk menulis program, ketikkan program tersebut sesuai dengan yang dibawah ini :
    See more...


    dropOff.java 


    package dropOff;
    import java.util.*;
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;

    /**
     * This is the main class of the mini games.
     *
     * @author Jeremiah McLeod
     */
    public class dropOff extends MIDlet implements CommandListener {

      private Command myExitCommand = new Command("Exit", Command.EXIT, 99);

      private Command myPauseCommand = new Command("Pause", Command.SCREEN, 1);
      
      private Command myGoCommand = new Command ("Go",Command.SCREEN, 1);
      
      private Command myNewGameCommand = new Command ("New Game",Command.SCREEN,1);
      
      private long pauseTime;
      
       //* the the canvas that all of the game will be drawn on.
       //*/
      dropOffCanvas myDropOffCanvas;
      
      /**
       * the thread that advances the animation
       */
      GameThread myGameThread;

      //-----------------------------------------------------
      //    initialization and game state changes

      /**
       * Initialize the canvas and the commands.
       */
      public dropOff () {
        pauseTime=0;
        myDropOffCanvas = new dropOffCanvas(this);
    myDropOffCanvas.addCommand(myExitCommand);
       myDropOffCanvas.addCommand(myGoCommand);
       myDropOffCanvas.setCommandListener(this);

       


      }
      //----------------------------------------------------------------
      //  implementation of MIDlet

      /**
       * Start the application.
       */
      public void startApp() throws MIDletStateChangeException {
         myGameThread = new GameThread(myDropOffCanvas);
         myDropOffCanvas.start();
        
        myGameThread.go();
      }
      /**
       * stop and throw out the garbage.
       */
      public void destroyApp(boolean unconditional)
          throws MIDletStateChangeException {
        myGameThread.requestStop();
        myGameThread = null;
        myDropOffCanvas = null;
        System.gc();
      }

      public void pauseApp() {



        myGameThread.pause();
      }

    public void setMyPauseCommand () {



    }



    public void setMyGoCommand () {
    myDropOffCanvas.removeCommand (myPauseCommand);
    myDropOffCanvas.addCommand (myGoCommand);

    }

    public void setMyNewGameCommand () {



    }
      //----------------------------------------------------------------
      //  implementation of CommandListener

      /*
       * Respond to a command issued on the Canvas.
       */
      public void commandAction(Command c, Displayable s) {
        int p,t;

       if (c == myExitCommand) {
          try {
    destroyApp(false);
    notifyDestroyed();
          } catch (MIDletStateChangeException ex) {
          }
        }
       
        else if (c==myGoCommand) {
    myDropOffCanvas.go=true;
    if (myDropOffCanvas.gameOver==true) myDropOffCanvas.newGame();
    myDropOffCanvas.removeCommand(myGoCommand);
    myDropOffCanvas.addCommand(myPauseCommand);



    }
    else if (c==myPauseCommand) {
    pauseTime=System.currentTimeMillis();
    myDropOffCanvas.go=false;
    myDropOffCanvas.removeCommand (myPauseCommand);
    myDropOffCanvas.addCommand (myGoCommand);
      }
    }//commandlistener
    }

    dropOffCanvas.java

    //Author Jeremiah McLeod xdebugx
    package dropOff;
    import java.util.*;
    import javax.microedition.lcdui.*;
    //import javax.microedition.lcdui.game.*;
    public class dropOffCanvas extends Canvas {
      static int CORNER_X;
      static int CORNER_Y;
      static int width;
      static int height;
      static int DISP_WIDTH;
      static int DISP_HEIGHT;
      Display myDisplay;
      dropOff myMiniGames;

      static boolean go;


      Random myRandom;

      boolean myInitialized;

      Font FONT;

      public boolean gameOver;

      private int counter,lCounter;

      private int lineShrinkage;

    public int slow;

    private boolean rep;

      private boolean notBuffered;

        private Image imgDoubleBuffer;

        private Graphics doubleBuffer;

      private int ballWidth,ballHeight,holeWidth,ballX,ballY;

      private long nextLineTime;

      private long gameStartTime,speedUpTime,currentRiseTime;

      private line lines[];

      private int numLines,nextLineHeight;

      private int lineWidth,moveWidth;

      private boolean moveLeft,moveRight,lineWidthAdd;

      private int score,scorePlaceX,scorePlaceY;

     public dropOffCanvas(dropOff midlet) {
        myDisplay = Display.getDisplay(midlet);
        myMiniGames = midlet;
        myInitialized=false;
        go=false;
        notBuffered=false;
      }
      void start() {
        myDisplay.setCurrent(this);
        repaint();
      }
      void reset() {
         }
      void flushKeys() {
      }
      //-------------------------------------------------------
      //  graphics methods
      /**
       * paint the game graphic on the screen.
       */
      public void paint(Graphics g) {
        int p,t;
        Graphics saved=null;
        // perform the calculations if necessary:
        if(!myInitialized) {
    if( !isDoubleBuffered()) notBuffered=true;
    //notBuffered=true;
     moveLeft=false;
        moveRight=false;
     FONT = g.getFont();
          CORNER_X = g.getClipX();
          CORNER_Y = g.getClipY();
          width = g.getClipWidth();
          height = g.getClipHeight();
       //  width=120;
       //  height=75;
          DISP_WIDTH=width;
          DISP_HEIGHT=height;
        //  g.setFont (FONT.SIZE_SMALL);
          FONT=Font.getFont(FONT.getFace(),FONT.getStyle(),Font.SIZE_SMALL);
       g.setFont (FONT);
         ballWidth=floatReg (width,180,20);
         ballHeight=floatReg (height,177,20);

         holeWidth=floatReg (width,180,55);

         lineWidth=floatReg (height,177,3);
         lineWidth=(((height*10000)/177)*3);
         moveWidth=(((width*10000)/180)*4);

         lineShrinkage=ballHeight;

         if (moveWidth<1) moveWidth=1;
         if (lineWidth<1) lineWidth=1;

    lines=new line [15];

    scorePlaceX=width-FONT.stringWidth ("Score: 555555");
    scorePlaceY=FONT.getHeight();

          if (notBuffered==true) {
      imgDoubleBuffer=Image.createImage (width,height);
      doubleBuffer=imgDoubleBuffer.getGraphics ();
    }
          myRandom = new Random(System.currentTimeMillis());
    newGame();
          myInitialized = true;
        }

        else
        {
       g.setFont (FONT);
           if (notBuffered==true) {
    saved=g;
    g=imgDoubleBuffer.getGraphics();
    g.setFont (FONT);
    }

        if (go==false && gameOver==false) {
    g.setColor (0xffffffff);
    g.fillRect (0,0,width,height);
    g.setColor (0x00000000);
    g.drawString ("Drop Off",(width/2)-(FONT.stringWidth ("Drop Off")/2),(FONT.getHeight()*1),CORNER_X|CORNER_Y);
    g.drawString ("Use Left & Right",(width/2)-(FONT.stringWidth ("Use Left & Right")/2),(FONT.getHeight()*2),CORNER_X|CORNER_Y);
    g.drawString ("Or 4 & 6 Keys",(width/2)-(FONT.stringWidth ("Or 4 & 6 Keys")/2),(FONT.getHeight()*3),CORNER_X|CORNER_Y);
    g.drawString ("To Move Ball",(width/2)-(FONT.stringWidth ("To Move Ball")/2),(FONT.getHeight()*4),CORNER_X|CORNER_Y);
    g.drawString ("Stay Below the Top",(width/2)-(FONT.stringWidth ("Stay Below the Top")/2),(FONT.getHeight()*5),CORNER_X|CORNER_Y);
    g.drawString ("Go to Start",(width/2)-(FONT.stringWidth ("Go to Start")/2),(FONT.getHeight()*6),CORNER_X|CORNER_Y);
    }

    if (gameOver==true) {
    g.setColor (0xffffffff);
    g.fillRect (0,0,width,height);
    g.setColor (0x00000000);
    g.drawString ("Drop Off",(width/2)-(FONT.stringWidth ("Drop Off")/2),(FONT.getHeight()*1),CORNER_X|CORNER_Y);
    g.drawString ("Your Score: "+score,(width/2)-(FONT.stringWidth ("Your Score: "+score)/2),(FONT.getHeight()*2),CORNER_X|CORNER_Y);
    g.drawString ("Go To Restart",(width/2)-(FONT.stringWidth ("Go To Restart")/2),(FONT.getHeight()*3),CORNER_X|CORNER_Y);

    }

    if (go==true) {
    g.setColor (0xffffffff);
    g.fillRect (0,0,width,height);
    g.setColor (0x00000000);
    for (p=0;p
    g.drawLine (lines[p].line1X,(lines[p].y/10000),lines[p].line1X+lines[p].line1Width,(lines[p].y/10000));
    g.drawLine (lines[p].line2X,(lines[p].y/10000),lines[p].line2X+lines[p].line2Width,(lines[p].y/10000));
    }// p
    g.fillRoundRect ((ballX/10000),(ballY/10000),ballWidth,ballHeight,(ballWidth),(ballHeight));
    g.drawString ("Score: "+score,scorePlaceX,scorePlaceY,CORNER_X|CORNER_Y);

    }//else

    if (notBuffered==true) saved.drawImage (imgDoubleBuffer,0,0,CORNER_X|CORNER_Y);

    }//else
        } //paint

      //  game movements

      void advance() {


    int p,x1,x2,width1,width2;
            if (rep=true) repaint();
            rep=!rep;

      if (go==true && gameOver==false) {
     if ((ballY/10000)<0-(ballHeight/2)) {
     gameOver=true;
     myMiniGames.setMyGoCommand();
     go=false;
     }
        score=score+(21-((int) (currentRiseTime)));
        ballY=ballY+lineWidth;
        if ((ballY/10000)+ballHeight>height) ballY=(height-ballHeight)*10000;
        for (p=0;p
    if ((((ballX/10000)+ballWidth>=lines[p].line1X && (ballX/10000)lines[p].line2X && (ballX/10000)<=(lines[p].y/10000) && (ballY/10000)+ballHeight>=(lines[p].y/10000)) {
    ballY=lines[p].y-(ballHeight*10000)-10000;
    }
    }
    if (moveRight==true) ballX=ballX+moveWidth;
    if (moveLeft==true) ballX=ballX-moveWidth;

    if ((ballX/10000)+ballWidth>width) ballX=(width-ballWidth)*10000;


    if (ballX<0) ballX=0;


       counter++;


       if (counter>300) {
    if (currentRiseTime==5) slow=17;

    currentRiseTime-=3;
     if (currentRiseTime==5) slow=17;
     slow--; if (slow<5) slow=5;

    if (currentRiseTime<=2) {
    currentRiseTime=2;
    lineWidth+=500;
    }else  nextLineHeight+=lineShrinkage;

    counter = 0;
    }
    lCounter+=7;
    if (lCounter>=currentRiseTime) {
    lCounter=0;
    for (p=0;p
    }
        for (p=0;p
    if ((lines[p].y/10000)<=nextLineHeight && lines[p].lineSpawned==false) {
    x1=0;
     width1=getRandomInt (width-holeWidth);
     x2=x1+width1+holeWidth;
     width2=width-x2;

     lines [numLines]= new line (x1,x2,width1,width2,height);
     numLines++;
      lines[p].lineSpawned=true;
    }
    if ((lines[p].y/10000)<1) removeLine (p);
    }

    }


      }
      /**
       * Respond to keystrokes.
       */

       private void removeLine (int x) {
       int p;
       for (p=x;p
    lines[p]=lines[p+1];
      }
      lines[numLines-1]=null;
      numLines--;
      }

      public void keyReleased (int keyCode) {


      if (keyCode ==-4 || keyCode==RIGHT || keyCode==KEY_NUM6) {
       moveRight=false;

      }



      if (keyCode==-3 || keyCode==LEFT || keyCode==KEY_NUM4)   {


       moveLeft=false;
    }

    }//keyReleased



      public void keyPressed (int keyCode) {



       if (keyCode ==-4 || keyCode==RIGHT  || keyCode==KEY_NUM6) {


    moveRight=true;

    }



       if (keyCode==-3 || keyCode==LEFT || keyCode==KEY_NUM4)   {


    moveLeft=true;
    }
    }

      public void newGame () {


      int p,x1,x2,width1,width2;
    lCounter=0;
      lineWidthAdd=false;
      score=0;
      counter=0;
      x1=0;
      width1=getRandomInt (width-holeWidth);
      x2=x1+width1+holeWidth;
      width2=width-x2;
      for (p=0;p<15;p++) lines[p]=null;
      lines [0]= new line (x1,x2,width1,width2,height);
      numLines=1;

      gameStartTime=System.currentTimeMillis();

      nextLineHeight=0;
      currentRiseTime=20;
      ballX=(width/2-(ballWidth/2))*10000;
      ballY=0;
      gameOver=false;
      slow=10;
      rep=true;

      }



      private int floatReg (int org, int div, int mul) {


          int x=0;int oldX=0;

          org=org*10000;
          x=org/div;
          x=x*mul;
           x=x/10000;

          return (x);


          }

    public int getRandomInt(int upper) {


        int retVal = myRandom.nextInt() % upper;
        if(retVal < 0) {
          retVal += upper;
        }
        return(retVal);
      }

    }


    class line {
    public int line1X,line2X,line1Width,line2Width,y;
    public long riseTime;
    public boolean lineSpawned;

    public line (int l1X,int l2X,int l1Width,int l2Width,int lY)


    {

    line1X=l1X;


    line2X=l2X;
    line1Width=l1Width;
    line2Width=l2Width;
    y=lY*10000;
    riseTime=System.currentTimeMillis();
    lineSpawned=false;
    }

    }//class line
    GameThread.java

    package dropOff;
    /**
     * This class contains the loop that keeps the game running.
     *
     * @author Jeremiah McLeod xdebugx
     */
    public class GameThread extends Thread {

      //---------------------------------------------------------
      //   fields

      /**
       * Whether or not this thread has been started.
       */
      boolean myAlreadyStarted;

      /**
       * A handle back to the graphical components.
       */
      dropOffCanvas myDropOffCanvas;
       private long runTime,sleepTime;

        int MS_PER_FRAME=10;
      //----------------------------------------------------------
      //   initialization

      /**


       * standard constructor.
       */
      GameThread(dropOffCanvas canvas) {
        myDropOffCanvas = canvas;
      }
      //----------------------------------------------------------
      //   actions

      /**
       * start or pause or unpause the game.
       */
      void go() {
        if(!myAlreadyStarted) {
          myAlreadyStarted = true;
          start();
        } else {
        }
      }

      /**
       * pause the game.
       */
      void pause() {
      }

      /**
       * stops the game.
       */
      static void requestStop() {
      }

      /**
       * start the game..
       */
      public void run() {
          runTime=0;
          runTime=System.currentTimeMillis();
          while(true) {
          myDropOffCanvas.advance();
     MS_PER_FRAME=myDropOffCanvas.slow;
     sleepTime=System.currentTimeMillis()-runTime;
       if (sleepTime
       try
       {
        Thread.sleep((int) (MS_PER_FRAME-sleepTime));
       }
       catch (java.lang.InterruptedException e)
       {}
       } //if
        runTime=System.currentTimeMillis()+(System.currentTimeMillis()-runTime-MS_PER_FRAME);
     }// while
    }
    }

        Setelah semua file sudah terbuat maka anda langsung buka Sun Java(TM) Wireless Toolkit 2.5.2 untuk mengimplementasikan program tersebut. Berikut ini tampilannya :


          

          Kemudian klik open project, pilih folder droopOff dan akan muncul seperti dibawah ini:








          Setelah file droopOff terbuka, kemudian lakukan build :




          Setelah dibuild akan muncul seperti berikut ini :







          Kemudian setelah tahapan build selesai, klik Run untuk menjalankan game droopOff dan berikut inilah tampilan game droopOff:


    Selamat Mencoba !!!