JAVAゲームプログラミングの二----j 2 me MIDlet携帯ゲーム入門開発--ヘビを食いしん坊


作者:雷神QQ:38929568 QQ群:28048051 JAVAゲームプログラミング(満)280477782(満)
前の掃雷と比較して、このゲームは1つのクラスが多くて、アニメーションを表示するために使用して、蛇が食べるもののクラスで、コードバッグ、ピクチャーバッグ、構造がはっきりしていて、コード量500行、地図生成の簡単なアルゴリズム、アニメーションを再生する簡単なアルゴリズムもあります!矩形の衝突があって、アップグレードするのは蛇の移動速度を増加して、簡単な方法はリフレッシュ速度を向上させて、毎回蛇をリフレッシュするたびに1つの格子を移動して、現れた物品は蛇の長さを増加して、あるものは蛇の長さを減少します.
プログラムをコンパイルするには画像が必要です
工事書類は添付ファイルのように、住所は以下の通りである:蛇を貪る
Gif.JAvaというクラスはアニメーションを描くクラスで、ヘビの食べ物クラスでもあります

  
  
  
  
  1. package code;  
  2.  
  3. import javax.microedition.lcdui.Image;  
  4. import javax.microedition.lcdui.Graphics;  
  5.  
  6. public class Gif   
  7. ...{  
  8.     public String     name  = "gif";  
  9.     public int         life  =  1;  
  10.     public int         pos_x =  0;  
  11.     public int         pos_y =  0;  
  12.     public int         w       = 10;  
  13.     public int         h       = 10;  
  14.     public int         form  =  1;  
  15.  
  16.     private int        allTime;  
  17.     private int     currForm;  
  18.     private long     startTime;  
  19.  
  20.     public boolean isShow=true;  
  21.     public int[] formTime=...{100};  
  22.  
  23.     private Image[] img;  
  24.       
  25.     public Gif( String _name, int _form, int _w, int _h )  
  26.     ...{  
  27.         w     = _w;  
  28.         h     = _h;  
  29.         setGif( _name, _form );  
  30.         startTime = System.currentTimeMillis();  
  31.         setPos();  
  32.     }  
  33.  
  34.     public void setGif( String _name, int _form )  
  35.     ...{  
  36.         try 
  37.         ...{  
  38.             name     = _name;  
  39.             form     = _form;  
  40.             formTime= new int[form];  
  41.             img        = new Image[form];  
  42.               
  43.             forint i=0; i<form; i++ )  
  44.             ...{  
  45.                 formTime[i]    =  100;  
  46.                 allTime        += formTime[i];  
  47.                 img[i]         =  Image.createImage("/pics/"+name+"_"+i+".png");  
  48.             }  
  49.  
  50. //            Image temp     = Image.createImage("/pics/"+name+".png");  
  51. //            form        = temp.getWidth()/w;  
  52. //            formTime    = new int[form];  
  53. //            img            = new Image[form];  
  54. //            Graphics gn;  
  55. //            for( int i=0; i<img.length; i++ )  
  56. //            {  
  57. //                formTime[i]    = 100;                  
  58. //                allTime        +=formTime[i];      
  59. //                img[i]         = Image.createImage(w, h);  
  60. //                gn             = img[i].getGraphics();      
  61. //                gn.drawImage(temp, -i*w, 0, gn.LEFT|gn.TOP);  
  62. //            }  
  63. //            gn     = null;  
  64. //            temp = null;      
  65. //            System.gc();  
  66.  
  67.         }catch(Exception e)...{ e.printStackTrace(); }          
  68.     }  
  69.       
  70.     public void setPos()  
  71.     ...{  
  72.         pos_x = cGame.rand.nextInt(cGame.s_width-w)  + ( w>>1 );  
  73.         pos_y = cGame.rand.nextInt(cGame.s_height-h) + ( h>>1 );  
  74.         startTime = System.currentTimeMillis();  
  75.     }  
  76.       
  77.     public void setFormTime(int[] _formTime)  
  78.     ...{  
  79.         formTime = _formTime;  
  80.         allTime  = 0;  
  81.         forint i=0; i<formTime.length; i++ )  
  82.         ...{  
  83.             allTime    +=formTime[i];  
  84.         }  
  85.         startTime = System.currentTimeMillis();  
  86.     }  
  87.       
  88.     public void paint(Graphics g)  
  89.     ...{  
  90.         if( isShow )  
  91.         ...{  
  92.             long now = System.currentTimeMillis();  
  93.             long time= (now-startTime)%allTime;  
  94.             for(int i=0; i<form; i++)  
  95.             ...{  
  96.                 time -= formTime[i];  
  97.                 if( time<=0 )  
  98.                 ...{  
  99.                     g.drawImage(img[i], pos_x, pos_y, g.HCENTER|g.VCENTER);  
  100.                     break;  
  101.                 }  
  102.             }  
  103.         }  
  104.     }  

cGame.JAvaというクラスはもうよく知られていません.作用機能は前のゲームと同じです.
 

  
  
  
  
  1. ////////////////////////////////////////////////////////////////////////////////  
  2. //  
  3. // cGame.java  
  4. //  
  5. // Project: Minesweeper  
  6. // Author(s): Gao Lei  
  7. // Create: 2007-10-12  
  8. ////////////////////////////////////////////////////////////////////////////////  
  9. package code;  
  10.  
  11. import java.util.Random;  
  12. import javax.microedition.lcdui.*;  
  13.  
  14. ////////////////////////////////////////////////////////////////////////////////  
  15.  
  16. class cGame extends Canvas implements Runnable  
  17. ...{  
  18.     private static final int STATEPLAY        = 0;  
  19.     private static final int STATELOST        = 1;  
  20.     private static final int STATEWIN        = 2;  
  21.  
  22.     private static final int KEY_UP         = 1;  
  23.     private static final int KEY_DOWN         = 2;  
  24.     private static final int KEY_LEFT         = 3;  
  25.     private static final int KEY_RIGHT     = 4;  
  26.     private static final int KEY_FIRE         = 5;  
  27.       
  28.     public static int     s_width     = 0;  
  29.     public static int     s_height    = 0;  
  30.     public static long    updates        = 0;  
  31.     public static Random rand;              
  32.       
  33.     private int maxRand = 1000;  
  34.     private int map_x     = 10;  
  35.     private int map_y     = 10;  
  36.     private int map_w     = 16;  
  37.     private int map_h     = 16;  
  38.       
  39.  
  40.     private int key_x     = map_x / 2;  
  41.     private int key_y     = map_y / 2;  
  42.     private int snake_w    = 8;  
  43.     private int snake_h    = 8;  
  44.     private int pos_x     = map_x / 2;  
  45.     private int pos_y     = map_y / 2;  
  46.     private int aspect_x= 0;  
  47.     private int aspect_y= 1;  
  48.     private int    snake_max    = 100;  
  49.     private int    snake_n        = 5;  
  50.     private int    gameState     = STATEPLAY;  
  51.     private int level        = 1;  
  52.     private long sleepTime    =160;  
  53.       
  54.     private int[]    snake_x        = new int[ snake_max ];  
  55.     private int[]    snake_y        = new int[ snake_max ];  
  56.       
  57.     private int[][] map;  
  58.     private boolean isShowInfo    = false;  
  59.  
  60.     private Font     font         = Font.getFont( Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE );  
  61.     private Thread    thread;                          
  62.     private Image[] imgGameBg    = new Image[4];      
  63.     private Image[] imgItem        = new Image[3];      
  64.     private Gif[]    gold        = new Gif[5];  
  65.     private Gif[]    dung        = new Gif[2];  
  66.     private Gif        fungus;  
  67.       
  68.     cGame()  
  69.     ...{  
  70.         setFullScreenMode(true);  
  71.         s_width = getWidth();      
  72.         s_height= getHeight();      
  73.         rand    = new Random( System.currentTimeMillis() );  
  74.         try 
  75.         ...{  
  76.             forint i=0; i<gold.length; i++ )  
  77.             ...{  
  78.                 gold[i]= new Gif("gold"51915 );  
  79.             }  
  80.             forint i=0; i<dung.length; i++ )  
  81.             ...{  
  82.                 dung[i]= new Gif("dung"62130 );  
  83.                 dung[i].life = -2;    // --life  
  84.             }  
  85.               
  86.             fungus = new Gif("fungus"122523 );  
  87.             fungus.setFormTime  
  88.             (   
  89.                 new int[] ...{ 2000,  150,  150,  150,  
  90.                              150,  200,  100,  100,  
  91.                              200,  150,  200,  200 
  92.                 }   
  93.             );  
  94.             fungus.life = 5;    // --life +5  
  95.               
  96.             forint i=0; i<imgItem.length; i++ )  
  97.             ...{  
  98.                 imgItem[i]=Image.createImage("/pics/item_16_"+i+".png");  
  99.             }  
  100.  
  101.             Image temp     = Image.createImage("/pics/bg_tile_0.png");  
  102.             Graphics gn;                                              
  103.             forint i=0; i<imgGameBg.length; i++ )      
  104.             ...{  
  105.                 imgGameBg[i] = Image.createImage(1616);  
  106.                 gn = imgGameBg[i].getGraphics();  
  107.                 gn.drawImage(temp, -i*160, gn.LEFT|gn.TOP);  
  108.             }  
  109.             gn     = null;  
  110.             temp = null;      
  111.             System.gc();  
  112.         }catch(Exception e)...{ e.printStackTrace(); }  
  113.  
  114.         rePlay( level );  
  115.         thread  = new Thread(this);  
  116.         thread.start();      
  117.     }  
  118.       
  119.     public void run()  
  120.     ...{  
  121.         whiletrue )  
  122.         ...{  
  123.             try 
  124.             ...{  
  125.                 updates++;              
  126.                 repaint();  
  127.                 serviceRepaints();  
  128.                 thread.sleep(sleepTime);  
  129.             }catch(Exception e)  
  130.             ...{  
  131.                 e.printStackTrace();  
  132.             }  
  133.         }  
  134.     }  
  135.       
  136.  
  137.     public void paint(Graphics g)  
  138.     ...{  
  139.         g.setClip(00, s_width, s_height);  
  140.         g.setColor(0x000000);                  
  141.         g.fillRect(00, s_width, s_height);  
  142.  
  143.         forint i=0; i<map_x; i++ )              
  144.         ...{  
  145.             forint j=0; j<map_y; j++ )  
  146.             ...{  
  147.                 g.drawImage(imgGameBg[ map[i][j] ], j*map_h, i*map_w, g.LEFT|g.TOP);  
  148.             }      
  149.         }  
  150.  
  151.         forint i=0; i<gold.length; i++ )  
  152.         ...{  
  153.             gold[i].paint(g);  
  154.         }  
  155.         forint i=0; i<dung.length; i++ )  
  156.         ...{  
  157.             dung[i].paint(g);  
  158.         }  
  159.         if( snake_n>20 )  
  160.             fungus.paint( g );  
  161.           
  162.         paintSnake( g );  
  163.  
  164.         if( isShowInfo || gameState != STATEPLAY )      
  165.         ...{  
  166.             g.setColor(0xFFFFFF);                  
  167.             forint i=0; i<=map_y; i++ )    // |||      
  168.             ...{  
  169.                 g.drawLine(i*map_w, 0, i*map_w, map_h*map_x);  
  170.             }  
  171.             forint i=0; i<=map_x; i++ )     // ===      
  172.             ...{  
  173.                 g.drawLine(0, i*map_h, map_y*map_w, i*map_h);  
  174.             }  
  175.             g.setFont( font );  
  176.         }  
  177.         g.setColor( 0xff0000 );  
  178.         g.setFont( font );  
  179.         g.drawString( "life:"+snake_n, 2,  20 );  
  180.         g.drawString( "level:"+level,  2180 );  
  181.     }  
  182.  
  183.  
  184.     void paintSnake( Graphics g )  
  185.     ...{  
  186.         g.setColor(0x0000FF);              
  187.         forint i=snake_n; i>0; i-- )  
  188.         ...{  
  189.             snake_x[i] = snake_x[i-1];   
  190.             snake_y[i] = snake_y[i-1];  
  191.             g.fillRect(snake_x[i]-snake_w/2, snake_y[i]-snake_h/2, snake_w, snake_h);  
  192.         }  
  193.         snake_x[0] += aspect_x*8;  
  194.         snake_y[0] += aspect_y*8;  
  195.         g.setColor(0x6666FF);      
  196.         g.fillRect(snake_x[0]-snake_w/2, snake_y[0]-snake_h/2, snake_w, snake_h);  
  197.  
  198.         if( snake_x[0]<0 || snake_x[0]>s_width || snake_y[0]<0 ||snake_y[0]>s_height )  
  199.         ...{  
  200.             rePlay(level);  
  201.         }  
  202.               
  203.         forint i=snake_n; i<snake_n; i++ )  
  204.         ...{  
  205.             if( isIntersect(snake_x[0], snake_y[0],    snake_w, snake_h,   
  206.                             snake_x[i], snake_y[i],    snake_w, snake_h )   
  207.                 )  
  208.             ...{  
  209.                 rePlay(level);  
  210.             }  
  211.                   
  212.         }  
  213.           
  214.         forint i=0; i<gold.length; i++ )  
  215.         ...{  
  216.             if( isIntersect(snake_x[0],        snake_y[0],        snake_w,    snake_h,   
  217.                             gold[i].pos_x,    gold[i].pos_y,    gold[i].w,     gold[i].h )   
  218.               )  
  219.             ...{  
  220.                 addSnake( gold[i].life );  
  221.                 gold[i].setPos();  
  222.             }  
  223.         }  
  224.         forint i=0; i<dung.length; i++ )  
  225.         ...{  
  226.             if( isIntersect(snake_x[0],        snake_y[0],        snake_w,    snake_h,   
  227.                             dung[i].pos_x,    dung[i].pos_y,    dung[i].w,     dung[i].h )   
  228.               )  
  229.             ...{  
  230.                 addSnake( dung[i].life );  
  231.                 dung[i].setPos();  
  232.             }  
  233.         }  
  234.           
  235.         if( isIntersect(snake_x[0],        snake_y[0],        snake_w,    snake_h,   
  236.                 fungus.pos_x,    fungus.pos_y,    fungus.w,     fungus.h )   
  237.           )  
  238.         ...{  
  239.             addSnake( fungus.life );  
  240. //            fungus.isShow =false;  
  241.             fungus.setPos();  
  242.         }  
  243.           
  244.     }  
  245.       
  246.     boolean isIntersect(int x1,int y1, int w1, int h1, int x2, int y2, int w2, int h2)  
  247.     ...{  
  248.         if( Math.abs(x2-x1) < (w1+w2)/2 && Math.abs(y2-y1) < (h1+h2)/2 )  
  249.         ...{  
  250.             return true;  
  251.         }  
  252.         else 
  253.             return false;  
  254.     }  
  255.       
  256.     public void keyPressed(int key)  
  257.     ...{  
  258.         key = Math.abs(key);  
  259.         System.out.println("key="+key);  
  260.  
  261.         switch( key )  
  262.         ...{  
  263.             case KEY_NUM2:  
  264.             case KEY_UP:  
  265.                 if( gameState != STATEPLAY )      
  266.                     break;  
  267.                 else 
  268.                 ...{  
  269.                     if( aspect_y <= 0)  
  270.                     ...{  
  271.                         aspect_x =  0;  
  272.                         aspect_y = -1;  
  273.                     }  
  274.                 }  
  275.             break;  
  276.  
  277.             case KEY_NUM8:  
  278.             case KEY_DOWN:  
  279.                 if( gameState != STATEPLAY )  
  280.                     break;  
  281.                 else 
  282.                 ...{  
  283.                     if( aspect_y >= 0)  
  284.                     ...{  
  285.                         aspect_x =  0;  
  286.                         aspect_y = +1;  
  287.                     }  
  288.                 }  
  289.             break;  
  290.  
  291.             case KEY_NUM4:  
  292.             case KEY_LEFT:  
  293.                 if( gameState != STATEPLAY )      
  294.                     break;  
  295.                 else 
  296.                 ...{  
  297.                     if( aspect_x <= 0)  
  298.                     ...{  
  299.                         aspect_y =  0;  
  300.                         aspect_x = -1;  
  301.                     }  
  302.                 }  
  303.             break;  
  304.  
  305.             case KEY_NUM6:  
  306.             case KEY_RIGHT:  
  307.                 if( gameState != STATEPLAY )  
  308.                     break;  
  309.                 else 
  310.                 ...{  
  311.                     if( aspect_x >= 0)  
  312.                     ...{  
  313.                         aspect_y =  0;  
  314.                         aspect_x = +1;  
  315.                     }  
  316.                 }  
  317.             break;  
  318.  
  319.             case KEY_FIRE:  
  320.             case KEY_NUM5:  
  321.                 if( gameState == STATEPLAY )  
  322.                 ...{  
  323. //                    addSnake();  
  324. //                    System.out.println("snake_n="+snake_n);  
  325.                 }  
  326.             break;  
  327.  
  328.             case KEY_NUM1:          
  329.  
  330.             break;  
  331.             case KEY_NUM3:          
  332.                 isShowInfo = !isShowInfo;  
  333.             break;  
  334.             case KEY_NUM0:          
  335.                 rePlay( level );          
  336.             break;  
  337.         }  
  338.         this.repaint();  
  339.     }  
  340.       
  341.     private void addSnake(int life)  
  342.     ...{  
  343.         snake_n += life;  
  344.         if( snake_n > snake_max )  
  345.         ...{  
  346.             snake_n = snake_max;  
  347.             level++;  
  348.             rePlay(level);  
  349.         }  
  350.         if( snake_n < snake_n )        //game over  
  351.         ...{  
  352.             rePlay(level);  
  353.             snake_n = snake_n;  
  354.         }  
  355.     }  
  356.  
  357.     public void rePlay( int level )  
  358.     ...{  
  359.         sleepTime     = 150-level*20;  
  360.         map_x        = s_height/16;  
  361.         map_y        = s_width/16;  
  362.         key_x         = map_x>>1;  
  363.         key_y         = map_y>>1;  
  364.         gameState    = STATEPLAY;  
  365.         map          = new int[map_x][map_y];  
  366.         isShowInfo    = false;  
  367.         snake_n        = 5;  
  368.         aspect_x    = 0;  
  369.         aspect_y    = 0;  
  370.         forint i=0; i<map_x; i++ )    //draw bg  
  371.         ...{  
  372.             forint j=0; j<map_y; j++ )  
  373.             ...{  
  374.                 int r = rand.nextInt(maxRand);  
  375.                 forint k=0; k<imgGameBg.length; k++ )  
  376.                 ...{  
  377.                     if( r < maxRand>>k+1  )  
  378.                     ...{  
  379.                         map[i][j] = k;  
  380.                     }  
  381.                 }  
  382.             }  
  383.         }  
  384.  
  385.         forint i=0; i<snake_n; i++ )    //init snake  
  386.         ...{  
  387.             snake_x[i] = s_width>>1;  
  388.             snake_y[i] = (s_height>>1)-(i*snake_h);  
  389.         }  
  390.     }  

Snake.JAvaというクラスは以前のMinesweeperです.JAvaというクラスは基本的に名前が必要で、他には何も変更する必要はありません!
 

  
  
  
  
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////  
  2. //  
  3. // Snake.java  
  4. //  
  5. // Project: Snake  
  6. // Author(s): Gao Lei  
  7. // Create: 2007-10-08  
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////  
  9. package code;                          
  10.  
  11. import javax.microedition.midlet.*;      
  12. import javax.microedition.lcdui.*;      
  13.  
  14. ///////////////////////////////////////////////////////////////////////////////////////////////////  
  15.  
  16. public class Snake extends MIDlet   
  17. ...{  
  18.     static Snake s_midlet;  
  19.     static Display s_display = null;  
  20.     static cGame s_game = null;      
  21.       
  22.     public Snake()  
  23.     ...{  
  24.         s_midlet = this;  
  25.     }  
  26.       
  27.     public void startApp()               
  28.     ...{  
  29.         if (s_display == null)   
  30.         ...{  
  31.             s_display = Display.getDisplay(this);  
  32.         }  
  33.  
  34.         if (s_game == null)   
  35.         ...{  
  36.             s_game = new cGame();  
  37.             s_display.setCurrent(s_game);  
  38.         }   
  39.         else   
  40.         ...{  
  41.             s_display.setCurrent(s_game);  
  42.         }  
  43.     }  
  44.  
  45.     public void pauseApp()           
  46.     ...{  
  47.           
  48.     }  
  49.  
  50.     public void destroyApp(boolean unconditional)   
  51.     ...{  
  52.         notifyDestroyed();  
  53.     }  

今回のコードのコメントが少ないのは、私のIDEが中国語入力をサポートしていないからです.だから注釈をつけても英語です!ほとんどの関数を追加していない方法と前のゲームはあまり変わっていません!