路是爬出来的

游戏入门之一 雷电 精灵模型

      设计游戏我个人认为一个好的角色操作将事半工倍。所以我现在把雷电的所有角色抽象成一个Fairy。里面有实现绘制的方法以及移动,碰撞算法。

      在这里我强调下,我的碰撞算法是简单的实现。就是一个物体为参照物体。在10像素范围内x,y轴如果发现另外一个物体侵入则判断为true,发之为false

java 代码


 


  1.    

  2. package org.wuhua.game.model;  

  3.   

  4. import javax.microedition.lcdui.Graphics;  

  5. import javax.microedition.lcdui.Image;  

  6.   

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

  8.    

  9. /** 

  10.  * 类名:Sprite.java 

     
     

  11.  * 编写日期: 2006-11-29 

     

  12.  * 程序功能描述:建立精灵物体模型 

     

  13.  * Demo: 

     

  14.  * Bug: 

     

  15.  *  

  16.  * 程序变更日期 :

     
     

  17.  * 变更作者 :

     
     

  18.  * 变更说明 :

     

  19.  *  

  20.  * @author wuhua 

     
     

  21.  */  

  22. public class Fairy {  

  23.     static Log log = Log.getLog("Fairy");  

  24.     /** 

  25.      * position of Fairy in x offset  

  26.      */  

  27.     int x; // = 0;  

  28.   

  29.     /** 

  30.      * position of Fairy in y offset  

  31.      */  

  32.     int y; // = 0;  

  33.   

  34.     /** 

  35.      * width of layer  

  36.      */  

  37.     int width; // = 0;  

  38.   

  39.     /** 

  40.      * height of layer 

  41.      */  

  42.     int height; // = 0;  

  43.   

  44.     /**  

  45.      * If the Layer is visible it will be drawn when paint 

  46.      * is called. 

  47.      */  

  48.     boolean visible = true;  

  49.       

  50.     /** 

  51.      * 图片资源 

  52.      *   

  53.      */  

  54.       

  55.     Image fairy;   

  56.       

  57.     public Fairy(Image fairy,int x, int y){  

  58.         this.fairy = fairy;  

  59.         this.x = x;  

  60.         this.y = y;  

  61.     }  

  62.       

  63.     public void setPosition(int x, int y) {  

  64.         this.x = x;  

  65.         this.y = y;  

  66.     }  

  67.       

  68.     public void move(int dx, int dy) {    

  69.        

  70.         x += dx;  

  71.         y += dy;  

  72.     }  

  73.       

  74.     public void setVisible(boolean visible) {  

  75.         this.visible = visible;  

  76.     }  

  77.   

  78.      

  79.     public final boolean isVisible() {  

  80.         return visible;  

  81.     }  

  82.   

  83.     public final int getHeight() {  

  84.         return height;  

  85.     }  

  86.   

  87.     public final int getWidth() {  

  88.         return width;  

  89.     }  

  90.   

  91.     public final int getX() {  

  92.         return x;  

  93.     }  

  94.   

  95.     public final int getY() {  

  96.         return y;  

  97.     }  

  98.       

  99.     public void paint(Graphics g){  

  100.         if (g == null) {  

  101.             throw new NullPointerException("Graphics 不存在");  

  102.         }  

  103.         if(this.visible){  

  104.             //log.debug("x=" + x + " y=" + y);   

  105.             g.drawImage(fairy, x, y,  Graphics.TOP | Graphics.HCENTER);  

  106.         }  

  107.     }  

  108.   

  109.     /** 

  110.      * 进行简单的碰撞算法, 希望高手可以给个建议。 

  111.      * @param f 

  112.      * @return 

  113.      */  

  114.     public final boolean collidesWith(Fairy f){  

  115.           

  116.        

  117.         if((f.getX() >= this.getX() - 20 && f.getX() <= this.getX() + 20)  

  118.                 &&  (f.getY() >= this.getY() - 10  && f.getY() <= this.getY()+10 )){  

  119.             //log.debug("this.getY=" + this.getY());  

  120.             //log.debug("f.getY=" + f.getY());  

  121.                

  122.                

  123.             return true;  

  124.         }  

  125.               

  126.         return false;  

  127.     }  

  128.    

  129.   

  130. }  


posted on 2006-12-30 09:24 路是爬出来的 阅读(1286) 评论(0)  编辑  收藏


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


网站导航: