⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tinfish.java

📁 系统是一个用JAVA来实现的潜艇大战游戏。厘米那包括可执行源程序
💻 JAVA
字号:
/* 
 * 
 * 创建日期 2006-5-17
 *
 * TODO 要更改此生成的文件的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
package net.wangsong;


import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.*;


/** * @author user
 * 
 * TODO 要更改此生成的类型注释的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板 */

public class Tinfish extends Sprite implements SubObject  {

    /**
     * 
     * @uml.property name="subCanvas"
     * @uml.associationEnd multiplicity="(0 1)"
     */
    private SubCanvas subCanvas;


    
    private boolean lifeState = false;    //生命状态
    private int troop         = 0;        //敌我识别标识
    private int levelState    = 0;        //速度状态
    private int directionState = 1;       //当前图形方向.  0为左, 1为右
    
    private int xStep = 15;
    private int yStep = 0;
    private int vx    = 0;
    
    private int x = getX();
    private int y = getY();
    private int w = getWidth();
    private int h = getHeight();
    
    /** 初始化鱼雷图层
     * @param subCanvas         Canvas类
     * @param image             鱼雷图片
     * @param xPosition         初始化坐标起点
     * @param yPosition         初始化坐标终点
     * @param troop             敌我标识
     * @param levelState        速度水平
     * @param directionState    初始化方向
     */
    public Tinfish(SubCanvas subCanvas, Image image, int xPosition, int yPosition, 
            int troop, int levelState, int directionState){
        super(image);
        this.subCanvas = subCanvas;
        //设定鱼雷出现位置
        this.x = xPosition;
        this.y = yPosition;
        this.setPosition(xPosition, yPosition);
        
        this.levelState     = levelState;
        this.lifeState      = true;
        this.troop          = troop;
        this.directionState = directionState;
        
        //根据初始方向判断图形是否需要转向, 并设定速度方向
        if(this.directionState % 2 == 0){
            setTransform(Sprite.TRANS_MIRROR);
            vx = (-1) * this.xStep;
        }else{
            
            setTransform(Sprite.TRANS_NONE);
            vx = this.xStep;
        }
    }
    
    /**
     * 秒触发
     * 在生存状态中, 根据速度水平(循环次数)移动鱼雷
     */
    public void tick(){
        int i = 0;
        
        //当 当前生命状态为真, 并且速度状态尚没有结束(还可以继续运行时)
        while(lifeState && (i < this.levelState)){
            movePosition();
            i++;
        }
    }
    
    /**
     * 以一个步长为单位移动图形, 如果超出边界则置生存状态为false
     * 移动之后检查碰撞事件, 如果需要消失也同样置生存状态为false
     */
    protected void movePosition(){
        this.x = x + this.vx;
        
        //确保图形在游戏区域
		if(x > SubCanvas.WORLD_WIDTH - w){
			this.lifeState = false;
		}
		if(x < 0){
		    this.lifeState = false;
		}
		if(y > SubCanvas.WORLD_HEIGHT - h){
		    this.lifeState = false;
		}
		if(y < 0){
		    this.lifeState = false;
		}
		
		this.setPosition(x, y);
		
		//处理碰撞事件
		collideStuff();
    }
    
    /**
     * 处理鱼雷撞击事件
     */
    public void collideStuff(){
        Sprite collideable = null;
        if(this.troop == SubCanvas.TROOP_PLAYER){
            //当鱼雷是玩家潜艇释放时
            for(int i = 0; i < subCanvas.enemyCollectionVector.size(); i++)
            {
                collideable = (Sprite) subCanvas.enemyCollectionVector.elementAt(i);
                if (collidesWith(collideable, false))
                {
                    this.lifeState = false;
                }
            }
        }else{
            //当鱼雷是敌人潜艇释放时
            collideable = subCanvas.getMySub();
            if(collidesWith(collideable, true)){
                this.lifeState = false;
            }
        }
        
        collideable = null;
    }
    


    /**
     * 
     * @uml.property name="troop"
     */
    public int getTroop() {
        return this.troop;
    }

    /**
     * @return 返回 levelState。
     * 
     * @uml.property name="levelState"
     */
    public int getLevelState() {
        return levelState;
    }

    /**
     * @return 返回 lifeState。
     * 
     * @uml.property name="lifeState"
     */
    public boolean isLifeState() {
        return lifeState;
    }

    
    
}






⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -