ゲーム入門の一つ雷電精霊モデル


ゲームをデザインするのは個人的には、良いキャラクターの操作が半分になると思います.だから私は今雷のすべての役をFairyに抽象化しています.描画を実現する方法や移動、衝突アルゴリズムが含まれています.
ここで強調すると,私の衝突アルゴリズムは簡単な実現である.一つの物体が参照物体である.10画素範囲x,y軸において別の物体が侵入していることを発見するとtrue,falseと判断する
JAvaコード
 
   
package org.wuhua.game.model;  
  
import javax.microedition.lcdui.Graphics;  
import javax.microedition.lcdui.Image;  
  
import org.wuhua.game.util.Log;  
   
/** 
*クラス名:Sprite.java    
*作成日:2006-11-29 *プログラム機能説明:精霊物体モデルを構築する
 * Demo:   
 * Bug:   
 *  
*プログラム変更日:*変更作成者:*変更の説明: *  
 * @author wuhua    
 */  
public class Fairy {  
    static Log log = Log.getLog("Fairy");  
    /** 
     * position of Fairy in x offset  
     */  
    int x; // = 0;  
  
    /** 
     * position of Fairy in y offset  
     */  
    int y; // = 0;  
  
    /** 
     * width of layer  
     */  
    int width; // = 0;  
  
    /** 
     * height of layer 
     */  
    int height; // = 0;  
  
    /**  
     * If the Layer is visible it will be drawn when  paint  
     * is called. 
     */  
    boolean visible = true;  
      
    /** 
*画像リソース     *   
     */  
      
    Image fairy;   
      
    public Fairy(Image fairy,int x, int y){  
        this.fairy = fairy;  
        this.x = x;  
        this.y = y;  
    }  
      
    public void setPosition(int x, int y) {  
        this.x = x;  
        this.y = y;  
    }  
      
    public void move(int dx, int dy) {    
       
        x += dx;  
        y += dy;  
    }  
      
    public void setVisible(boolean visible) {  
        this.visible = visible;  
    }  
  
     
    public final boolean isVisible() {  
        return visible;  
    }  
  
    public final int getHeight() {  
        return height;  
    }  
  
    public final int getWidth() {  
        return width;  
    }  
  
    public final int getX() {  
        return x;  
    }  
  
    public final int getY() {  
        return y;  
    }  
      
    public void paint(Graphics g){  
        if (g == null) {  
throw new NullPointerException(「Graphicsは存在しない」);  
        }  
        if(this.visible){  
            //log.debug("x=" + x + " y=" + y);   
            g.drawImage(fairy, x, y,  Graphics.TOP | Graphics.HCENTER);  
        }  
    }  
  
    /** 
*簡単な衝突アルゴリズムを行い、上級者にアドバイスしてほしい. 
     * @param f 
     * @return 
     */  
    public final boolean collidesWith(Fairy f){  
          
       
        if((f.getX() >= this.getX() - 20 && f.getX() <= this.getX() + 20)  
                &&  (f.getY() >= this.getY() - 10  && f.getY() <= this.getY()+10 )){  
            //log.debug("this.getY=" + this.getY());  
            //log.debug("f.getY=" + f.getY());  
               
               
            return true;  
        }  
              
        return false;  
    }  
   
  
}