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

JAVA基于J2ME的手机游戏开发和实现——贪吃蛇 MIDP API包含以下五个部分,如表2-1所示:表2-1 MIDP API类 别描 述包括MIDlet类。Timers主要包括Timers和Timers Task类。Networking提供访问设备通信能力的接口。通过记录管理系统(RMS)API访问用户永久存储。包括MIDP LCDUI(液晶显示器用户界面)类。 阅读详情

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


 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.结束. :)

 

 

 

基于J2ME的手机游戏开发 随着通信技术的发展和手机的普及,手机游戏的开发技术越来越为人们所关注。以J2ME为开发平台,利用Java提供强大工具,不但可以在手机上实现静态HTML技术所无法实现的计算处理、数据存储、与服务器的通信等功能,而且能够开发各种手机游戏。本文在介绍J2ME及其体系结构的基础上,以贪吃蛇游戏为实例,描述了借助J2ME的MIDlet类库开发手机游戏的过程。 阅读详情

相关推荐

JAVA基于J2ME的手机游戏开发(论文+源代码)

现在JAVA-JAVA2 Platform大致可分为J2SE、J2EE、J2ME三类。J2SE为JAVA-JAVA2 Platform 的标准版,通常在PC上使用的JAVA。J2EE是在J2SE的API上,扩展了给企业使用EJB与Servlet等主要使用在服务器上的功能。而J2ME则是面向家电和通信工具等微小设备。

2301_76992670的博客 757

J2ME 2D小游戏入门之旅(四)加入子弹群,实现碰撞运算

作者:favoyang 文章来源:J2ME开发网飞机类游戏中子弹是必不可少的,他们数量很多且充斥着整个屏幕,这些随机或者有着一定AI的小物体,实现起来不是总那么容易,有时候你不得不考虑很多和效能有关的问题。我们之前定义了GameObject,很大程度上就是为了方便的重用Sprite,因为我们有很多的子弹,不可能没增加一个子弹都是一个Sprite,我需要共享同一个Sprite。我们通过继承GameO

mobilechannel的专栏 2396

J2ME 游戏(基本功能开发)

游戏基本功能开发(任务 地图绘制),自己写的,可能会渣点

基于J2ME的手机游戏开发(Java贪吃蛇)

3.1.1 贪吃蛇游戏的规则简介在贪吃蛇游戏中,玩家操作由小方块连接而成的蛇,去吃随机散落在画面内的小方块,每吃一块就增加一小方块长度,要是撞壁以及撞自己的尾,就属于失败,如无失败则直到通关为止。屏幕的长度的行向为11单位,纵向为18单位。在这个范围内,玩家通过操作方向键来控制蛇的运动方向。该游戏的最大特色是屏幕自适应,无论各种手机,PDA的屏幕大小如何,该游戏总是能获得最佳的显示效果。3.1.2 开发环境。

weixin_48265867的博客 961

抖音__NS_sig3签名机制解析与动态Hook实战

在现代移动应用中,客户端与服务端之间的可信通信契约往往依赖于动态生成的请求签名,如抖音(某手)的__NS_sig3参数。该签名并非简单哈希,而是融合设备指纹、传感器数据、运行时上下文与轻量加密的综合摘要,体现了‘环境即凭证’的安全设计思想。其核心价值在于抵御静态伪造与批量模拟,提升接口调用的真实性与不可复用性。典型应用场景包括Feed流加载、点赞关注等关键业务接口的身份校验。本文基于真实逆向与Frida动态Hook实践,深入解析__NS_sig3的so层实现逻辑、JNI调用链路、输入参数真实性验证机制,以及

weixin_30127995的博客 378

逆向工程实战:解密AppleAccount的X-MMe-Nas-Qualify签名生成机制

本文通过逆向工程实战,详细解析了AppleAccount服务中X-MMe-Nas-Qualify签名的生成机制。作者利用Frida进行动态追踪定位签名源头,结合IDA Pro进行静态分析,并借助LLDB动态调试还原核心算法。文章最终分享了如何独立于苹果官方库,通过Python代码复现签名算法,为自动化测试和深度交互苹果服务的开发者提供了关键技术路径。

weixin_29323049的博客 709

Java毕设-基于J2ME的手机游戏开发

Key Words。

xuejunlw的博客 1549

大模型越狱指令数据集全解析:从AdvBench到HarmBench

本文全面解析了AdvBench、HarmBench等主流大模型越狱指令数据集。AdvBench作为基础测试集,用于快速评估模型安全基线;HarmBench则以其全面的文本、多模态及对抗性攻击评估,成为当前权威的深度安全审计工具。文章还介绍了MaliciousInstruct、JailbreakBench等特色数据集,并提供了根据实际场景选择与使用这些数据集的具体实战指南。

weixin_29327525的博客 630

JAVA 基于J2ME的手机游戏开发(论文+源码)_Nueve

现在JAVA-JAVA2 Platform大致可分为J2SE、J2EE、J2ME三类。J2SE为JAVA-JAVA2 Platform 的标准版,通常在PC上使用的JAVA。J2EE是在J2SE的API上,扩展了给企业使用EJB与Servlet等主要使用在服务器上的功能。而J2ME则是面向家电和通信工具等微小设备。

weixin_69071556的博客 1041

【网络原理】HTTP 请求 (Request)详解

【网络原理】HTTP 请求 (Request)详解,认识URLquery string关于 URL encode, GET方法,POST 方法,GET 和 POST 的区别,User-Agent (简称 UA),Referer,Cookie

ZXT的博客 1万+

C++总结笔记

/ ++操作 这是因为定义的数组,申请的是连续的内存空间,当ptr++(ptr是int类型的指针变量),地址空间向后偏移是以int类型的大小为单位进行偏移的,相当于int类型数组元素的向后偏移。使用场景:它是最通用的转换形式,用于将一种类型转换为另一种类型,例如基本数据类型的转换(如 float 转 int),指向基类的指针或引用转为指向派生类的指针或引用等。在派生类被调用时会调用派生类中的函数,不会调用基类中的被重写的函数(基类中的函数需要加上virtual才可以,即基类中的函数是虚函数)。

weixin_50890461的博客 5242

JAVA基于J2ME的手机游戏开发(毕设+实现+源码+数据库)

Key Words。

weixin_43658371的博客 1289

华为防火墙——本地Portal认证

配置IP地址、路由、Nat策略使得内网主机可以访问公网服务器对网段不进行认证,对网段进行portal认证。

m0_49864110的博客 3270

终极免费J2ME模拟器指南:如何在现代设备上重温经典手机游戏

还记得那些年我们沉迷的贪吃蛇、俄罗斯方块和像素风格的手机游戏吗?随着智能手机的普及,那些承载着青春记忆的J2ME经典游戏逐渐淡出视线。幸运的是,**FreeJ2ME** 这个开源的J2ME模拟器项目,让我们能够在现代设备上重新体验这些怀旧游戏!🎮 ## 什么是FreeJ2ME?重新定义经典游戏体验 FreeJ2ME是一个功能强大的免费J2ME模拟器,支持多种前端界面,包括libretro、A

gitblog_01062的博客 1058

YOLOv8+BiFPN双向特征金字塔:多尺度目标检测精度提升28%的实战教程

双向特征金字塔网络(BiFPN)是一种强化多尺度特征融合支持“自顶向下+自底向上”的双向特征流动,同时保留不同尺度的细节信息;引入加权融合机制,自动学习不同特征的重要性权重;裁剪冗余节点,在轻量化的同时提升融合效率。

m0_64661770的博客 496

EMB电子机械制动系统冗余备份方案介绍

随着汽车智能化技术的快速发展,自动驾驶技术的不断升级,对车辆制动系统的可靠性和安全性提出了更高要求。根据国际汽车安全法规(如UN R13-H)的最新修订,L3级及以上自动驾驶车辆必须配备具有冗余功能的制动系统,以确保在任何单一故障发生时仍能维持部分或全部制动能力。本文重点讨论最具前瞻性的EMB(电子机械制动)系统的冗余备份设计方案

m0_59012156的博客 863

java毕业设计——基于java+J2ME的贪吃蛇游戏设计与实现(毕业论文+程序源码)——贪吃蛇游戏

1.随着通信技术的发展和手机的普及,手机游戏的开发技术越来越为人们所关注。以J2ME为开发平台,利用Java提供强大工具,不但可以在手机上实现静态HTML技术所无法实现的计算处理、数据存储、与服务器的通信等功能,而且能够开发各种手机游戏。本文在介绍J2ME及其体系结构的基础上,以贪吃蛇游戏为实例,描述了借助J2ME的MIDlet类库开发手机游戏的过程。提示:以下为毕业论文的简略介绍,项目源码及完整毕业论文下载地址见文末。第一章 绪论1.1 研究背景。

m0_66238867的博客 2535

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

基于J2ME的手机游戏(毕设+实现+源码+数据库)

Key Words。

weixin_43658371的博客 882

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

基于JAVA的手机游戏开发 基于JAVA的贪吃蛇游戏设计(源码+文档)

现在JAVA-JAVA2 Platform大致可分为J2SE、J2EE、J2ME三类。J2SE为JAVA-JAVA2 Platform 的标准版,通常在PC上使用的JAVA。J2EE是在J2SE的API上,扩展了给企业使用EJB与Servlet等主要使用在服务器上的功能。而J2ME则是面向家电和通信工具等微小设备。

分享一些项目,探讨问题 1436

免费J2ME模拟器终极指南:重拾经典手机游戏记忆 [特殊字符]

想要重温那些年玩过的诺基亚、索爱经典手机游戏吗?FreeJ2ME作为一款完全免费的J2ME模拟器,让你在现代设备上完美运行怀旧Java手机游戏!这个开源项目支持libretro、AWT和SDL2前端,是重拾经典手机游戏记忆的最佳工具。 ## 🚀 FreeJ2ME是什么? **FreeJ2ME**是一个功能完整的J2ME模拟器,能够运行各种经典的Java手机游戏和应用。它采用多前端架构设计,支

gitblog_00396的博客 914

JAVA 基于J2ME的手机游戏设计与开发(论文+源码)_Nueve

现在JAVA-JAVA2 Platform大致可分为J2SE、J2EE、J2ME三类。J2SE为JAVA-JAVA2 Platform 的标准版,通常在PC上使用的JAVA。J2EE是在J2SE的API上,扩展了给企业使用EJB与Servlet等主要使用在服务器上的功能。而J2ME则是面向家电和通信工具等微小设备。

weixin_69071556的博客 220

java毕业设计下载(全套源码+配套论文)——基于java+J2ME的贪吃蛇游戏设计与实现

1.随着通信技术的发展和手机的普及,手机游戏的开发技术越来越为人们所关注。以J2ME为开发平台,利用Java提供强大工具,不但可以在手机上实现静态HTML技术所无法实现的计算处理、数据存储、与服务器的通信等功能,而且能够开发各种手机游戏。本文在介绍J2ME及其体系结构的基础上,以贪吃蛇游戏为实例,描述了借助J2ME的MIDlet类库开发手机游戏的过程。提示:以下为毕业论文的简略介绍,项目源码及完整毕业论文下载地址见文末。第一章 绪论1.1 研究背景。

m0_66238867的博客 882

JAVA游戏编程之二----j2me MIDlet 手机游戏入门开发--贪吃蛇

 JAVA游戏编程之二----j2me MIDlet 手机游戏入门开发--贪吃蛇作者:雷神QQ:38929568QQ群:28048051JAVA游戏编程(满) 28047782(将满) 与前一款扫雷比较,这个游戏多了一个 类,用来显示动画,也是蛇要吃的物品类, 也有了代码包,图片包,结构清晰,代码量500行,地图生成的简单算法,和播放动画的简单算法!有矩形碰撞,升级就是增加蛇的移动速度

1553

Java手机游戏开发专辑

Java手机游戏开发专辑  关键字 KXML  MMAPI  HTTP  XML  CLDC    移动动作游戏开发中的一些问题   即使目前最新型号的手机仍然不是最理想的游戏平台,但它已经是个良好的开端。处理器、内存和色彩深度提供了游戏开发所需的因素。开发者正努力把其它游戏平台上的质量标准运用到这个平台上。当然了,一些问题仍然存在,然而这些问题终将被解决,就象PC平台游戏开发者使用不断改进的Di

Pinocchio 的专栏 3468

[计算机毕设]基于java+J2ME的手机贪吃蛇游戏开发系统设计与实现(项目报告+源代码)

基于J2ME的手机游戏开发代码说明随着通信技术的发展和手机的普及,手机游戏的开发技术越来越为人们所关注。以J2ME为开发平台,利用Java提供强大工具,不但可以在手机上实现静态HTML技术所无法实现的计算处理、数据存储、与服务器的通信等功能,而且能够开发各种手机游戏。本文在介绍J2ME及其体系结构的基础上,以贪吃蛇游戏为实例,描述了借助J2ME的MIDlet类库开发手机游戏的过程。手机游戏; MIDP应用程序; 永久性数据目 录第一章 绪论. 11.1 研究背景. 11.2 研究内容. 1第二章 J2ME

2301_78287784的博客 1228

J2ME游戏开发(从零开始)

  手机游戏概论 大纲 l           手机游戏类型。 l           手机游戏开发特点。 l           手机发展历史及3G技术展望。 重点 l           手机游戏主要有以下几种类型:嵌入式游戏、短信息服务游戏、WAP浏览器游戏、J2ME和其他的解释语言类游戏。 l           合理开发手机游戏:缩短游戏时间、合理处理等待时间。 l...

金佳杰 1119
上一篇: 基于C#的网络SNIFFER网络IP层监听程序
下一篇: FLEX ActionScript 游戏开发教程之[贪吃蛇]Step By Step
aerror
博客等级 码龄23年 393粉丝 193原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址