J2ME手机游戏贪吃蛇开发Step By Step

本文详细介绍了一款手机贪吃蛇游戏的开发过程,包括环境搭建、代码编写及调试等步骤,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

呵呵,昨天晚上开始学习手机游戏编写,现在现学现卖,希望可以帮到一些初手.:).


 Step1.安装J2se SDK, j2sdk-1_4_2_08-windows-i586-p.exe/到C:/j2sdk1.4.2_08目录
 Step2.我的电脑->属性->高级->环境变量,设置PATH路径加上: C:/j2sdk1.4.2_08/bin;
 Step3.安装Motorola J2ME SDK for Linux OS Products,Mot_Linux_5.4.2.exe到目录C:/Motorola/5.4.2/
 Step4.在D:/创建以下目录结构D:/com/snake
 Step5.创建目录结构D:/com/snake/res/audio,复制C:/WINDOWS/Media/recycle.wav到D:/com/snake/res/audio目录下.
 Step6.创建并编写D:/com/snake/snake.jad,内容如下:
 

  1.    MIDlet-1: SnakeGame, ,com.snake.SnakeGame
  2.    MIDlet-Jar-Size: 9512
  3.    MIDlet-Jar-URL: snake.jar
  4.    MIDlet-Name: SnakeGame
  5.    MIDlet-Vendor: yourname@21cn.com
  6.    MIDlet-Version: 1.0
  7.    

Step7.创建并编写D:/com/snake/manifest.mf,内容如下:

  1. Manifest-Version: 1.0
  2. MIDlet-Name: Snake
  3. MIDlet-1: SnakeGame, ,com.snake.SnakeGame
  4. MIDlet-Version: 1.0
  5. MIDlet-Vendor: yourname@21cn.com
  6. MicroEdition-Configuration: CLDC-1.0
  7. MicroEdition-Profile: MIDP-1.0
  8. MIDlet-Description: Snake Game For Mobile Phone

Step8.创建并编写D:/makeSnakeEM3.bat,内容如下:

  1. @rem ***************************************************************
  2. @rem ** Copyright (c) 2002-2003 Motorola, Inc. All rights reserved.
  3. @rem **
  4. @rem ** makeOneEM1.bat
  5. @rem **
  6. @rem **     This script compiles one of the demo MIDlets.
  7. @rem **
  8. @rem ** Command Line Parameters:
  9. @rem **   - Location of .JAVA files to compile
  10. @rem **   - Location of Emulator
  11. @rem **
  12. @rem **
  13. @rem ** Assumptions:
  14. @rem **   - This script is invoked from the demo/scripts directory
  15. @rem **   - Demo midlets are in the package com
  16. @rem **   - The directory demo/com exists.
  17. @rem **   - Default behavior is to compile bounce for the Monty emulator
  18. @rem **   - Java 1.2 compiler can be invoked:
  19. @rem **     + javac.exe is on the PATH
  20. @rem **
  21. @rem ** Results:
  22. @rem **   - One of the potential targets will be created in it's directory
  23. @rem **   - Each output class will be written to a .JAR file
  24. @rem **     
  25. @rem **
  26. @rem ***************************************************************
  27. @echo off
  28. set MOTOSDKPATH=C:/Motorola/5.4.2/
  29. @rem ***************************************************************
  30. @rem **  Check to see if a command line argument was specified
  31. @rem **  First argument must be the target MIDlet name
  32. @rem ***************************************************************
  33. del  com/snake/*.class
  34. @if NOT "%1"=="" set COMPILETARG=%1
  35. @if     "%1"=="" set COMPILETARG=snake
  36. @rem ***************************************************************
  37. @rem **  Set the COMPILECLASS variable to point to the midlet dir
  38. @rem **  then append the name of the target MIDlet
  39. @rem ***************************************************************
  40. set COMPILECLASS=com/%COMPILETARG%
  41. @rem ***************************************************************
  42. @rem **  Check to see if a command line argument was specified
  43. @rem **  Second argument must be the target Emulator name
  44. @rem ***************************************************************
  45. @if NOT "%2"=="" set TARGETEMUL=%2
  46. @if     "%2"=="" set TARGETEMUL=EmulatorM.3
  47. @rem ***************************************************************
  48. @rem **  Next 3 lines can be un-REMarked to help with debugging 
  49. @rem **  a problematic build.
  50. @rem ***************************************************************
  51. @rem set COMPILETARG
  52. @rem set COMPILECLASS
  53. @rem set TARGETEMUL
  54. @echo Compiling ...
  55. @echo off
  56. @rem ***************************************************************
  57. @rem **  Change to the demo root dir
  58. @rem **  
  59. @rem ***************************************************************
  60. @rem ***************************************************************
  61. @rem **  Compile using the specified emulator and target MIDlet
  62. @rem **  source files
  63. @rem ***************************************************************
  64. @echo javac  -target 1.1 -O -classpath %MOTOSDKPATH%/%TARGETEMUL%/lib/midp.zip %COMPILECLASS%/*.java
  65. javac  -target 1.1 -O -classpath %MOTOSDKPATH%/%TARGETEMUL%/lib/midp.zip %COMPILECLASS%/*.java
  66. @echo Creating JAR ...
  67. @echo off
  68. @rem ***************************************************************
  69. @rem **  Now create the .JAR file in the directory with the sources
  70. @rem **  include the manifest, stored in meta-inf
  71. @rem ***************************************************************
  72. @echo off
  73. jar cmf %COMPILECLASS%/manifest.mf %COMPILECLASS%/%COMPILETARG%.jar %COMPILECLASS%/*.class 
  74. @rem ***************************************************************
  75. @rem **  See if the target midlet has any resources that will need
  76. @rem **  to be included in the .JAR
  77. @rem ***************************************************************
  78. echo jar uf ../%COMPILETARG%.jar *
  79. cd %COMPILECLASS%/res
  80. jar uf ../%COMPILETARG%.jar *
  81. cd  ../../../
  82. :JARCOMPLETE
  83. @echo Preverifying ...
  84. @echo off
  85. @rem ***************************************************************
  86. @rem **  Use the preverifier that is supplied with the emulator
  87. @rem **  
  88. @rem ***************************************************************
  89. @echo off
  90. %MOTOSDKPATH%/%TARGETEMUL%/bin/preverify -classpath %MOTOSDKPATH%/%TARGETEMUL%/lib;%MOTOSDKPATH%/%TARGETEMUL%/bin;%MOTOSDKPATH%/%TARGETEMUL%/lib/midp.zip -d %COMPILECLASS% %COMPILECLASS%/%COMPILETARG%.jar
  91. @rem ***************************************************************
  92. @rem **  Extract the preverified classes from the .JAR file. These
  93. @rem **  will be runnable if there are no resources involved.
  94. @rem ***************************************************************
  95. @echo off
  96. @echo Restoring preverified classes...
  97. jar xf %COMPILECLASS%/%COMPILETARG%.jar %COMPILECLASS%/*.class

Step8.创建并编写D:/com/snake/SnakeGame.java,内容如下:

  1. /*
  2. *  SnakeGame.java
  3. *
  4. *  2008-9-21 17:32
  5. *  
  6. *  This notice does not imply publication.
  7. */
  8. package com.snake;
  9. import java.util.Vector;
  10. import java.util.Random;
  11. import javax.microedition.lcdui.*;
  12. import javax.microedition.midlet.*;
  13. import javax.microedition.media.Player;
  14. import javax.microedition.media.Manager;
  15. import javax.microedition.media.MediaException;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. /**
  19. *
  20. * @see MIDlet
  21. */
  22. public class SnakeGame extends MIDlet  implements CommandListener{
  23.     /**
  24.      * The game speed (in milliseconds)
  25.      */
  26.     private final int GAME_SPEED = 200;
  27.     /**
  28.      * The main game screen
  29.      */
  30.     private GameFrame mainFrame;
  31.     /**
  32.      * The title screen
  33.      */
  34.     private Form titleScreen;
  35.     /**
  36.      * The results screen
  37.      */
  38.     private Form statsScreen;
  39.     /**
  40.      * The Snake Movie Clip object for this game
  41.      */
  42.     private SnakeMc mySnakeMc;
  43.     /**
  44.      * True if game has not started or player lost
  45.      */
  46.     private boolean gameOver = true;
  47.     /**
  48.      * Reference to Display
  49.      */
  50.     private Display myDisplay;
  51.     
  52.     
  53.     
  54.     /**
  55.      * The repaint lock for this Bounce MIDlet
  56.      */
  57.     public Object repaintLock = new Object();
  58.     public Object playSoundLock  = new  Object();
  59.     private int   playSound=0;
  60.     private SoundPlayer soundPlayer=null;
  61.     /**
  62.      * How many games have been played
  63.      */
  64.     private int numGames=0;
  65.     /**
  66.      * The number of milliseconds the user has been playing this game
  67.      */
  68.     private long elapsedTime;
  69.     
  70.     public SnakeGame() {
  71.         myDisplay = Display.getDisplay(this);
  72.         
  73.         /*
  74.          * Create main game screen
  75.          */
  76.         mainFrame = new GameFrame();
  77.         /*
  78.          * Create title screen
  79.          */
  80.         titleScreen = new Form("贪吃蛇");
  81.         
  82.         titleScreen.append(new StringItem(null,
  83.                 "很经典的游戏哦,希望你喜欢。 /n联系作者: dany21cn@21cn.com"));
  84.         titleScreen.setCommandListener(this);
  85.         titleScreen.addCommand(new Command("开始", Command.OK, 1));
  86.         /*
  87.          * Create results screen
  88.          */
  89.         statsScreen = new Form("游戏统计");
  90.         statsScreen.setCommandListener(SnakeGame.this);
  91.         statsScreen.addCommand(new Command("再玩一局?", Command.OK, 1));
  92.     }
  93.     /**
  94.      * Begin the application, show its frame
  95.      */
  96.     protected void startApp() {
  97.         myDisplay.setCurrent(titleScreen);
  98.     }
  99.     /**
  100.      * Application is being terminated, kill threads
  101.      */
  102.     protected void pauseApp() {
  103.         gameOver = true;
  104.         mySnakeMc = null;
  105.       
  106.     }
  107.     /**
  108.      * Clean up application
  109.      */
  110.     protected void destroyApp(boolean unconditional) {
  111.         gameOver = true;
  112.     }
  113.     /**
  114.      * Start the game
  115.      */
  116.     public void commandAction(Command c, Displayable d) {
  117.         /*
  118.          * Create Sound Play And Snake Game  threads
  119.          */
  120.         mySnakeMc = new SnakeMc();
  121.         soundPlayer = new  SoundPlayer();
  122.         
  123.         /*
  124.          * Get the starting time
  125.          */
  126.         elapsedTime = System.currentTimeMillis();
  127.         
  128.         /*
  129.          * Set the initial Snake positions
  130.          */
  131.         mySnakeMc.setUp();
  132.         /*
  133.          * Set the screen
  134.          */
  135.         myDisplay.setCurrent(mainFrame);
  136.         /*
  137.          * Start the thread
  138.          */
  139.         gameOver = false;
  140.         playSound =0;
  141.       
  142.         soundPlayer.start();
  143.         mySnakeMc.start();
  144.         
  145.         
  146.     }
  147.     public class SnakeBlock
  148.  {
  149.   public int r=2;
  150.   public int x=0;
  151.   public int y=0;
  152.   public int xx=0;
  153.   public int yy=0;
  154.   public SnakeBlock()
  155.   {
  156.    super();
  157.   }
  158.   
  159.  }
  160.     
  161.     /**
  162.      * The main Screen for this game
  163.      */
  164.     class GameFrame extends Canvas {
  165.      Font font = Font.getFont(Font.FACE_MONOSPACE,
  166.                    Font.STYLE_PLAIN, Font.SIZE_SMALL);
  167.       
  168.       int gameAction = 0;
  169.         /**
  170.          * Canvas receives all key events
  171.          */
  172.         public void keyPressed(int keyCode) {
  173.            
  174.          synchronized (SnakeGame.this.repaintLock){
  175.              try {
  176.                  gameAction = getGameAction(keyCode);
  177.              } catch (Exception e) {
  178.              }
  179.    
  180.              if (gameAction == LEFT) {
  181.                  
  182.               mySnakeMc.kbq.addElement(new Integer(0));
  183.              } else if (gameAction == RIGHT) {
  184.               mySnakeMc.kbq.addElement(new Integer(2));
  185.              }
  186.              else if(gameAction == UP)
  187.              {
  188.               mySnakeMc.kbq.addElement(new Integer(1));
  189.              }
  190.              else if(gameAction == DOWN)
  191.              {
  192.               mySnakeMc.kbq.addElement(new Integer(3));
  193.              }
  194.          }
  195.             
  196.             
  197.             return;
  198.         }
  199.         /**
  200.          * Clear the game screen
  201.          */
  202.         private void clear(Graphics g) {
  203.             g.setColor(0xFFFFFF);
  204.             g.fillRect(0, 0, getWidth(), getHeight());
  205.         }
  206.         /**
  207.          * Paint the game screen
  208.          */
  209.         public void paint(Graphics g) {
  210.             g.setFont(font);
  211.             synchronized (g) {
  212.                 /*
  213.                  * Clear the screen
  214.                  */
  215.                 clear(g);
  216.               
  217.                 /*
  218.                  * Paint the Snake and Food
  219.                  */
  220.                 synchronized (SnakeGame.this.repaintLock)
  221.                 {
  222.                  
  223.                  g.setColor(0x2000ff);
  224.                  for(int i=0;i<mySnakeMc.food.size();i++)
  225.                  {
  226.                   SnakeBlock s = (SnakeBlock)mySnakeMc.food.elementAt(i);
  227.                     g.fillRect(s.x, s.y, mySnakeMc.blocksize-1,
  228.                       mySnakeMc.blocksize-1);
  229.                  }
  230.                  
  231.                  g.setColor(0x007fff); 
  232.                  for(int i=0;i<mySnakeMc.sa.size();i++)
  233.                  {
  234.                   SnakeBlock s = (SnakeBlock)mySnakeMc.sa.elementAt(i);
  235.                      g.fillRect(s.x, s.y, mySnakeMc.blocksize-1,
  236.                       mySnakeMc.blocksize-1);
  237.                  }
  238.                 }
  239.                 
  240.                 
  241.                 g.setColor(0xff6000);
  242.                 
  243.                 /*
  244.                  * Draw a line in the top of the canvas
  245.                  */
  246.                String szInfo = "1P[DANY] "+
  247.                     "时间: "+mySnakeMc.tickcount*GAME_SPEED/1000+
  248.                     " 得分: "+mySnakeMc.scores;
  249.                 g.drawString(szInfo, 0, 0,
  250.                         Graphics.LEFT | Graphics.TOP);
  251.                
  252.                 
  253.             }
  254.         }
  255.     }
  256.     /**
  257.      * The ball thread
  258.      */
  259.     class SnakeMc extends Thread {
  260.      public int  StartX  = 0;
  261.      public int  StartY  =  0;
  262.      public int blocksize  =  8;
  263.      public int  blWidth    =  64;
  264.      public int  blHeight    =  32;
  265.      public int   numFood   =  5;
  266.      public TextField  t   =  null;
  267.      public TextField  t2    =  null;
  268.     
  269.      public SnakeBlock  ss   =  null;
  270.      public Vector sa     =  new Vector();
  271.      public Vector kbq    =  new Vector();
  272.      public Vector rr    =  new Vector();
  273.      public Vector food  =   new Vector();
  274.      public int  scores    =  0;
  275.      public int  bonus     =  0;
  276.      public long  tickcount   = 0;
  277.       
  278.         /**
  279.          * Create a Snake Game
  280.          */
  281.         public SnakeMc() {
  282.             setUp();
  283.         }
  284.         /**
  285.          * Set starting position
  286.          */
  287.         void setUp() {
  288.          blWidth  = mainFrame.getWidth()/blocksize;
  289.          blHeight = mainFrame.getHeight()/blocksize;
  290.         }
  291.   public SnakeBlock  CreateSprite(int w)
  292.   {
  293.    SnakeBlock  s   =  new SnakeBlock();
  294.    return s;
  295.   }
  296.   
  297.   public SnakeBlock  AppendSprite(int w)
  298.   {
  299.    SnakeBlock  s   =  CreateSprite(w);
  300.    sa.addElement(s);
  301.    if(sa.size()>1)
  302.    {
  303.     SnakeBlock  pres = (SnakeBlock)sa.elementAt(sa.size()-2);
  304.     s.x    =    pres.x;
  305.     s.y    =    pres.y;
  306.     s.xx    =   pres.xx;
  307.     s.yy    =   pres.yy;
  308.     s.r    =    (pres.r+2)%4;
  309.     UpdateSnakePos(s);
  310.     s.r    =    pres.r;
  311.    }
  312.    else
  313.    {
  314.     s.x  =    StartX;
  315.     s.y  =    StartY;
  316.     s.xx    =    s.x/blocksize;
  317.     s.yy    =    s.y/blocksize;
  318.     
  319.    }
  320.    return s;
  321.   }
  322.   
  323.   public void   UpdateSnakePos(SnakeBlock s)
  324.   {
  325.   
  326.    {
  327.     switch(s.r)
  328.           {
  329.           case 0:
  330.            s.x -= blocksize;
  331.            s.xx--;
  332.            break;
  333.           case 1:
  334.            s.y -= blocksize;
  335.            s.yy--;
  336.            break;
  337.           case 2:
  338.            s.x += blocksize;
  339.            s.xx++;
  340.            break;
  341.           case 3:
  342.            s.y += blocksize;
  343.            s.yy++;
  344.            break;
  345.           }
  346.          
  347.    }
  348.   }
  349.   
  350.   public void   upateSnakesPos()
  351.   {
  352.     int  i   = 0;
  353.     for(i = 0;i<sa.size();i++)
  354.     {
  355.      SnakeBlock  s   = (SnakeBlock)sa.elementAt(i);
  356.      for(int  j  = 0;j<rr.size();j++)
  357.      {
  358.       int vtmp =((Integer)rr.elementAt(j)).intValue();
  359.       if(i ==   (vtmp>>16))
  360.       {
  361.        s.r    =  vtmp%4;
  362.       }
  363.      
  364.      }
  365.      UpdateSnakePos(s);
  366.     }
  367.     
  368.     Vector  tt     =    new Vector();
  369.     for(i = 0;i<rr.size();i++)
  370.     {
  371.      int vi =((Integer)rr.elementAt(i)).intValue();
  372.      int  tmp     =    (vi>>16);
  373.      if(tmp <sa.size()+10)
  374.      {
  375.      
  376.       tt.addElement(new Integer(((tmp+1)<<16)|(vi&0xffff)));
  377.      }
  378.     }
  379.     rr    =    null;
  380.     rr    =    tt;
  381.   }
  382.   
  383.   
  384.   public void  startGame()
  385.   {
  386.    tickcount++;
  387.    if(ss==null)
  388.    {
  389.     tickcount++;
  390.     cleanup();
  391.     ss = AppendSprite(0);
  392.     genFood(); 
  393.    }
  394.   }
  395.   
  396.   
  397.   public boolean   IntersectSnake(int xx, int yy)
  398.   {
  399.    int  i  = 0;
  400.    for(i = 0;i<sa.size();i++)
  401.    {
  402.     SnakeBlock s = (SnakeBlock)sa.elementAt(i);
  403.     if(xx == s.xx && yy == s.yy)
  404.     {
  405.      return true;
  406.     }
  407.    }
  408.    return false;
  409.   }
  410.   
  411.   public boolean   IsGameOver()
  412.   {
  413.   
  414.    
  415.    if(ss.xx<0 || ss.xx >blWidth-1 || ss.yy<0 || ss.yy >blHeight-1)
  416.    {
  417.     return true;
  418.    }
  419.    int  i   = 0;
  420.    for(i = 1;i<sa.size();i++)
  421.    {
  422.     SnakeBlock s = (SnakeBlock)sa.elementAt(i);
  423.     if(ss.xx == s.xx && ss.yy == s.yy)
  424.     {
  425.      return true;
  426.     }
  427.    }
  428.   
  429.    
  430.    return false;
  431.   }
  432.   
  433.   public SnakeBlock   getFood(int xx,int yy)
  434.   {
  435.    for(int  i  = 0;i<food.size();i++)
  436.    {
  437.     SnakeBlock s = (SnakeBlock)food.elementAt(i);
  438.     if(s.xx    == xx && s.yy    == yy)
  439.     {
  440.      return s; 
  441.     }
  442.    }
  443.    return null;
  444.   }
  445.   
  446.   public void   genFood()
  447.   {
  448.    if(food.size()!= 0)
  449.      return;
  450.    int ii=0;
  451.    for(;food.size() <numFood;ii++)
  452.    {
  453.     Random random = new Random();
  454.     int k = random.nextInt();
  455.     int  xx  = Math.abs(k % blWidth);
  456.     k = random.nextInt();
  457.     int  yy  = Math.abs(k % blHeight); ;
  458.     
  459.     if(getFood(xx,yy)    == null &&!IntersectSnake(xx,yy)  )
  460.     {
  461.      SnakeBlock  s     =     CreateSprite(1);
  462.      s.x    =    xx*blocksize;
  463.      s.y    =    yy*blocksize;
  464.      s.xx    =    xx;
  465.      s.yy    =    yy;
  466.      food.addElement(s);
  467.     }  
  468.    }
  469.    
  470.   }
  471.   
  472.   
  473.   public void   eatFood()
  474.   {
  475.   if(ss!= null)
  476.   {
  477.   
  478.    for(int  i  = 0;i<food.size();i++)
  479.    {
  480.     SnakeBlock s = (SnakeBlock)food.elementAt(i);
  481.     if(s.xx    == ss.xx && s.yy    == ss.yy)
  482.     {
  483.      AppendSprite(0);
  484.      if(sa.size()>2)
  485.      {
  486.       bonus += 4*(sa.size()-2);
  487.       if(tickcount>0)
  488.       {
  489.        bonus += sa.size()/tickcount;
  490.       }
  491.      }
  492.      
  493.      scores   +=    10+bonus;
  494.      
  495.      food.removeElementAt(i);
  496.      
  497.      synchronized (SnakeGame.this.playSoundLock)
  498.               {
  499.                playSound++;
  500.               }
  501.      
  502.      return;
  503.     }
  504.    }
  505.    
  506.   }
  507.   }
  508.   
  509.   public void   cleanup()
  510.   {
  511.    sa.removeAllElements();
  512.    food.removeAllElements();
  513.    rr.removeAllElements();
  514.    kbq.removeAllElements();
  515.       
  516.    ss      =    null;
  517.    scores      =    0;
  518.    bonus       =    0;
  519.    tickcount     =    0;
  520.    
  521.    
  522.   }
  523.   
  524.   public void  GameFrame()
  525.   {
  526.   
  527.    if(ss!= null)
  528.     {
  529.     tickcount++;
  530.      if(kbq.size()>0)
  531.        {
  532.         int  c  = 0;
  533.            c    =   ((Integer)kbq.elementAt(0)).intValue(); // pick the next turn in the queue (may be undefined if queue is empty)
  534.              kbq.removeElementAt(0);
  535.            // and check that it is not undefined and not a 180 degree turn
  536.            // (annoying to be able to turn into the snake with one key press)
  537.            //
  538.            if (c%2!= ss.r%2) 
  539.            { 
  540.           rr.addElement(new Integer(c)); // change current direction to the new value
  541.            }
  542.        } 
  543.        
  544.        upateSnakesPos();
  545.        if(IsGameOver())
  546.        {
  547.         SnakeGame.this.gameOver  =  true;
  548.       ss      =    null;
  549.         return;
  550.        }
  551.        eatFood();
  552.        genFood();
  553.    
  554.     } 
  555.   }
  556.         /**
  557.          * Loop to run thread, loops until game over
  558.          */
  559.         public void run() {
  560.          startGame();
  561.             while (!gameOver) {
  562.                 /*
  563.                  * Sleep for animation delay
  564.                  */
  565.                 try {
  566.                     Thread.sleep(GAME_SPEED);
  567.                 } catch (Exception e) {
  568.                     return;
  569.                 }
  570.                 synchronized (SnakeGame.this.repaintLock) {
  571.                  GameFrame();
  572.                  mainFrame.repaint(0,0,mainFrame.getWidth(),mainFrame.getHeight());
  573.                 }
  574.             }
  575.             
  576.             /*
  577.              * Get statistic information
  578.              */
  579.             elapsedTime = System.currentTimeMillis()
  580.               - elapsedTime;
  581.             numGames++;
  582.             statsScreen.append(new StringItem("Game " +
  583.                     numGames + " over""时间: " +
  584.                     (elapsedTime/1000) + " 秒 " +
  585.                     "得分: "+scores
  586.                     ));
  587.             /*
  588.              * Show the statistics screen
  589.              */
  590.             myDisplay.setCurrent(statsScreen);
  591.         }
  592.         
  593.         
  594.     }
  595.     
  596.     class SoundPlayer extends Thread {
  597.      
  598.   Player myPlayer = null;
  599.   public SoundPlayer()
  600.   { 
  601.    try {
  602.     InputStream is = getClass().getResourceAsStream("/audio/recycle.wav");
  603.      myPlayer =Manager.createPlayer(is,"audio/x-wav");
  604.     } catch (MediaException pe) {
  605.     } catch (IOException ioe) {
  606.    }
  607.   }
  608.         public void run() {
  609.                while (!gameOver) {
  610.                 try {
  611.                        Thread.sleep(5);
  612.                    } catch (Exception e) {
  613.                        return;
  614.                    }
  615.                    
  616.                    int num=0;
  617.                    synchronized (SnakeGame.this.playSoundLock)
  618.                    {
  619.                     num =playSound;
  620.                     playSound=0;
  621.                    }
  622.                    
  623.                    for(int i=0;i<num;i++)
  624.                 {
  625.                     try {
  626.                      myPlayer.start();
  627.                    } catch (MediaException pe) {
  628.                    }
  629.                
  630.                 }
  631.                }
  632.                
  633.               
  634.         }
  635.     }
  636.   
  637. }

Step9.运行D:/makeSnakeEM3.bat,编译生成snake.jar.

Step10.运行C:/Motorola/5.4.2/launchpad.exe,选择D:/com/snake/snake.jad,Launch运行测试.

Step11.使用Usb线把D:/com/snake/snake.jar复制到手机(我使用的型号是e680i)上.
Step12.在手机运行snake.jar,提示安装,按提示安装完毕,在程序菜单中运行游戏.
Step13.结束. :)

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值