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

📄 enemysub.java

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

import javax.microedition.lcdui.game.Sprite;
//import java.util.*;

import javax.microedition.lcdui.*;

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

public class EnemySub 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 xStep = 2;
    private int yStep = 0;
    private int vx    = 0;
    
    private int x = getX();
    private int y = getY();
    private int w = getWidth();
    private int h = getHeight();
    
    /** 初始化敌人图层Sprite
     * 
     * @param subCanvas     
     * @param image
     * @param xPosition      敌人潜艇初始横坐标
     * @param yPosition      敌人潜艇初始纵坐标
     * @param troop          敌我标识
     * @param levelState     难度水平
     */
    public EnemySub(SubCanvas subCanvas, Image image, int xPosition, int yPosition,  int levelState){
        super(image);
        this.subCanvas = subCanvas;
        
        //设定敌人潜艇出现位置
        this.x = xPosition;
        this.y = yPosition;
        this.setPosition(xPosition, yPosition);
        
        this.levelState = levelState * 3;
        this.lifeState  = true;
        this.troop      = SubCanvas.TROOP_ENEMY;
        
        //根据初始方向判断图形是否需要转向, 并设定速度方向
        if(this.x < SubCanvas.WORLD_WIDTH / 2){
            this.setTransform(Sprite.TRANS_NONE);
            vx = this.xStep;
        }else{
            this.setTransform(Sprite.TRANS_MIRROR);
            vx = (-1) * 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){
			x = 0;
			this.y = SubMIDlet.createRandom(SubCanvas.WORLD_HEIGHT);
		}
		if(x < 0){
		    x = SubCanvas.WORLD_WIDTH - w;
		    this.y = SubMIDlet.createRandom(SubCanvas.WORLD_HEIGHT);
		}
		if(y > SubCanvas.WORLD_HEIGHT - h){
		    this.y = 0;
		}
		if(y < 0){
		    this.y = SubCanvas.WORLD_HEIGHT - h;
		}
		
		this.setPosition(x, y);
		
		//处理碰撞事件
		collideStuff();
		
    }
    
    /**
     * 处理 撞击事件
     */
    public void collideStuff(){
        
        Tinfish collideable = null;
        
        Sprite sprite = subCanvas.getMySub();
        if(collidesWith(sprite, false)){
            this.lifeState = false;
        }
        
        for(int i = 0; i < subCanvas.tinfishCollectionVector.size(); i++)
        {
            collideable = (Tinfish) subCanvas.tinfishCollectionVector.elementAt(i);
            
            //对鱼雷数组中的元素,当鱼雷是玩家释放的时候,才触发碰撞事件
            if(collideable.getTroop() == SubCanvas.TROOP_PLAYER){
                if (collidesWith(collideable, false))
                {
                    this.lifeState = false;
                }
            }
        }
        collideable = null;
    }

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

    /**
     * @param troop 要设置的 troop。
     * 
     * @uml.property name="troop"
     */
    public void setTroop(int troop) {
        this.troop = troop;
    }

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

    


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

}











⌨️ 快捷键说明

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