路是爬出來的

          游戲入門之三 雷電 Game

                 這里介紹的是Game的邏輯類,主要控制游戲的動作,以及繪制。

                 詳細里面代碼有注釋

          java 代碼


           


          1. /******************************************************************** 

          2.  * 項目名稱             :j2me學習          

             

          3.  *  

          4.  * Copyright 2005-2006 Wuhua. All rights reserved 

          5.  ********************************************************************/  

          6. package org.wuhua.battleplan;  

          7.   

          8. import java.util.Stack;  

          9.   

          10. import javax.microedition.lcdui.Graphics;  

          11. import javax.microedition.lcdui.Image;  

          12.   

          13. import org.wuhua.game.GameCanvas;  

          14. import org.wuhua.game.model.Fairy;  

          15. import org.wuhua.game.util.Log;  

          16.   

          17.    

          18. /** 

          19.  * 類名:Game.java 

             
             

          20.  * 編寫日期: 2006-11-30 

             

          21.  * 程序功能描述:游戲的主體類。游戲的繪制,狀態的改變都在這邊。 

             

          22.  * Demo: 

             

          23.  * Bug: 

             

          24.  *  

          25.  * 程序變更日期 :

             
             

          26.  * 變更作者 :

             
             

          27.  * 變更說明 :

             

          28.  *  

          29.  * @author wuhua 

             
             

          30.  */  

          31. public class Game extends GameCanvas {  

          32.     static Log log = Log.getLog("Game");  

          33.     private Hero hero;  

          34.       

          35.     private Stack balls;  

          36.     private Stack foes;  

          37.       

          38.     private Stack balst;  

          39.     /** 

          40.      * 爆炸效果索引 

          41.      */  

          42.     private int balstIndex;  

          43.       

          44.     /** 

          45.      * if time = 3 的時候建立一個 

          46.      */  

          47.     private int genaratBallTime;  

          48.       

          49.     private int genaratFoeTime;  

          50.       

          51.       

          52.     Game(){  

          53.         super();  

          54.         this.setFullScreenMode(true);  

          55.     }  

          56.       

          57.     void init(){  

          58.           

          59.         WIDTH = getWidth();  

          60.         HEIGHT = getHeight();  

          61.         log.debug("WIDTH=" + WIDTH);  

          62.         log.debug("hegiht=" + HEIGHT);  

          63.         this.bufferImage = Image.createImage(WIDTH, HEIGHT);  

          64.            

          65.         Platform.WIDTH = this.getWidth();  

          66.         Platform.HEIGHT = this.getHeight();  

          67.           

          68.           

          69.         hero = Hero.createHero(Platform.WIDTH/2, Platform.HEIGHT -30);  

          70.        

          71.         balst = new Stack();  

          72.            

          73.     }  

          74.       

          75.     /** 

          76.      * 產生爆炸效果 

          77.      * @param x 

          78.      * @param y 

          79.      */  

          80.     void genaratorBalst(int x, int y){  

          81.         balst.addElement(new Fairy(Resources.BLAST[0], x, y));  

          82.         balst.addElement(new Fairy(Resources.BLAST[1], x, y));  

          83.         balst.addElement(new Fairy(Resources.BLAST[2], x, y));  

          84.         balst.addElement(new Fairy(Resources.BLAST[3], x, y));  

          85.         balst.addElement(new Fairy(Resources.BLAST[4], x, y));  

          86.     }  

          87.       

          88.     /** 

          89.      * 碰撞。實在沒有好的實現。 我想不出來了. 

          90.      * 邏輯是遍歷所有子彈,然后再遍歷所有敵機,再判斷是否碰撞,如果碰撞,則產生一個爆炸實例. 

          91.      * 最后刪除子彈跟飛機. 

          92.      * 

          93.      */  

          94.     void collides(){  

          95.         if(balls == null   

          96.                 || foes == null)  

          97.             return ;  

          98.         for(int i = 0; i < balls.size(); i ++){  

          99.             Ball b = (Ball) balls.elementAt(i);  

          100.             for(int j =0; j < foes.size(); j ++){  

          101.                 Foe f = (Foe) foes.elementAt(j);  

          102.                 if(b.collidesWith(f)){  

          103.                     this.genaratorBalst(f.getX(), f.getY());  

          104.                     balls.removeElement(b);  

          105.                     foes.removeElement(f);  

          106.                     return;  

          107.                 }  

          108.                

          109.             }  

          110.         }  

          111.     }  

          112.       

          113.     /** 

          114.      * 繪制游戲場景跟Hero 

          115.      * 

          116.      */  

          117.     void drawGame(){  

          118.         if(Platform.HEIGHT < this.getHEIGHT()){  

          119.             Platform.HEIGHT = this.getHEIGHT();  

          120.         }  

          121.           

          122.         Graphics g = this.getGraphics();  

          123.         if(g == null)  

          124.             return;  

          125.         fillFullScreen(g,0x349293);  

          126.         paintHeroAndBall(g);  

          127.           

          128.         paintFoe(g);  

          129.           

          130.         paintBalst(g);  

          131.         this.flushGraphics();  

          132.     }  

          133.   

          134.     /** 

          135.      * 繪制爆炸效果 

          136.      * @param g 

          137.      */  

          138.     private void paintBalst(Graphics g) {  

          139.            

          140.         if(balst == null   

          141.                 || balst.size() == 0)  

          142.             return;  

          143.           

          144.         Fairy bf = (Fairy) balst.elementAt(balstIndex);  

          145.         bf.paint(g);  

          146.         if(balstIndex >= 4){  

          147.             balstIndex = 0;  

          148.             balst.removeAllElements();  

          149.         }  

          150.               

          151.         balstIndex++;  

          152.     }  

          153.   

          154.     /** 

          155.      * 繪制敵機 

          156.      * @param g 

          157.      */  

          158.     private void paintFoe(Graphics g) {  

          159.         if(foes == null)  

          160.             return ;  

          161.         for(int i=0; i < foes.size(); i++){  

          162.             Foe foe = (Foe) foes.elementAt(i);  

          163.             foe.paint(g);  

          164.         }  

          165.           

          166.     }  

          167.       

          168.     /** 

          169.      * 制造敵飛機 

          170.      * 

          171.      */  

          172.     public void genaratorFoe(){  

          173.         if(this.genaratFoeTime == 5){             

          174.             FoeManager.addFoe(FoeManager.genarator());  

          175.             FoeManager.clearFoesIsOut();  

          176.             foes = FoeManager.getFoes();  

          177.             genaratFoeTime = 0;  

          178.         }  

          179.           

          180.         genaratFoeTime++;  

          181.     }  

          182.       

          183.     /** 

          184.      * 敵機飛行 

          185.      * 

          186.      */  

          187.     public void foeFly(){  

          188.         if(foes == null)  

          189.             return ;  

          190.         for(int i = 0; i < foes.size(); i++){  

          191.             Foe foe = (Foe) foes.elementAt(i);  

          192.             foe.fly();  

          193.         }  

          194.     }  

          195.   

          196.     private void paintHeroAndBall(Graphics g) {  

          197.         hero.paint(g);  

          198.         paintBalls(g);  

          199.     }  

          200.   

          201.     /** 

          202.      * 繪制子彈 

          203.      * @param g 

          204.      */  

          205.     private void paintBalls(Graphics g) {  

          206.         if(balls == null)  

          207.             return ;  

          208.         for(int i = 0; i < balls.size(); i++){  

          209.             Ball ball = (Ball) balls.elementAt(i);  

          210.             ball.paint(g);  

          211.         }  

          212.           

          213.     }  

          214.       

          215.     /** 

          216.      * 子彈的飛行 

          217.      * 

          218.      */  

          219.     public void ballFly(){  

          220.         if(balls == null)  

          221.             return ;  

          222.         for(int i = 0; i < balls.size(); i++){  

          223.             Ball ball = (Ball) balls.elementAt(i);  

          224.             ball.fly();  

          225.         }  

          226.     }  

          227.       

          228.     /** 

          229.      * 飛機的動作 

          230.      * 

          231.      */  

          232.     public void heroAction(){  

          233.         checkHeroIsExists();  

          234.         int keyCode = this.getKeyStates();  

          235.            

          236.         switch(keyCode){  

          237.         case Platform.KEY_LEFT: hero.moveLeft(); break;  

          238.         case Platform.KEY_RIGHT: hero.moveRight(); break;  

          239.         case Platform.KEY_UP: hero.moveUp(); break;  

          240.         case Platform.KEY_DOWN: hero.moveDown(); break;  

          241.         case Platform.KEY_FIRE: genaratorBall(); break;  

          242.         }  

          243.     }  

          244.   

          245.     /** 

          246.      * 創建子彈 

          247.      * 

          248.      */  

          249.     private void genaratorBall() {  

          250.        

          251.         if(this.genaratBallTime == 3){  

          252.             checkHeroIsExists();  

          253.               

          254.             BallManager.addBall(BallManager.genarator(hero.getX(), hero.getY()));  

          255.             BallManager.clearBallsIsOut();  

          256.             balls = BallManager.getBalls();  

          257.             genaratBallTime = 0;  

          258.         }  

          259.           

          260.         genaratBallTime++;  

          261.           

          262.           

          263.     }  

          264.   

          265.     private void checkHeroIsExists() {  

          266.         if(hero == null){  

          267.             throw new java.lang.NullPointerException("Hero is Null");  

          268.         }  

          269.     }  

          270.   

          271.     /** 

          272.      * 游戲的run。控制游戲個各個方面 

          273.      * 

          274.      */  

          275.     public void run(){  

          276.         this.collides();  

          277.         this.heroAction();  

          278.         this.ballFly();  

          279.         this.genaratorFoe();  

          280.         this.foeFly();  

          281.           

          282.         this.drawGame();  

          283.         this.setKeyStates(1000);  

          284.     }  

          285. }  







          代碼就是上面的,如果有什么好的建議,請評論。下面的一課,我將介紹GameThread。

          posted on 2006-12-30 09:24 路是爬出來的 閱讀(153) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 永清县| 淄博市| 游戏| 沙坪坝区| 绵阳市| 广州市| 南城县| 桐梓县| 乌拉特中旗| 漳平市| 通山县| 呼伦贝尔市| 云阳县| 凤庆县| 乌恰县| 东阳市| 逊克县| 沛县| 高尔夫| 裕民县| 兴仁县| 波密县| 新丰县| 肥西县| 佛坪县| 山东省| 高碑店市| 防城港市| 南陵县| 青铜峡市| 海原县| 莆田市| 西贡区| 塔河县| 汕尾市| 新竹市| 珲春市| 松潘县| 湟源县| 宜兴市| 北票市|