路是爬出来的

游戏入门之三 雷电 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 路是爬出来的 阅读(149) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: