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

📄 magic.java

📁 Java ME手机应用开发大全一书的配套光盘上的源码
💻 JAVA
字号:
import java.util.Random;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.Graphics;

public class Magic
{
    //定义物体的状态:平常态
    public static final int STATUS_NORMAL = 0;
    //点燃状态
    public static final int STATUS_FIRING = 1;
    //定义物体的状态:死亡态
    public static final int STATUS_DEAD = 2;
    //定义物体的状态:正在死亡态
    public static final int STATUS_DYING = 3;
    //当前物体状态
    public int status;
    //物体的类型
    public int type;
    //物体在地图上行数
    public int row; 
    //物体在地图上列数
    public int column;
    //动画
    public Sprite animation;
    //炸弹点燃时的序列
    private static int[] bombSeq = new int[]{0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1};
    //爆炸效果的序列
    private static int[] explodeSeq = new int[]{0,0,0,1,1,1,2,2,2,3,3,3,4,4,4};
    //镜子打碎时动画序列
    private static int[] brokingSeq = new int[]{0,1,2,3,3};
    //激光器的动画序列
    private static int[][] laserSeq = new int[][]{{0,1},{2,3},{4,5},{2,3}};
    //风扇和激光器被激光穿越时的动画序列
    private int firingduration = 12;
    //产生随机数
    static Random random = new Random();
    //位置左上角
    int x;
    //位置左上角
    int y;    
    
    public Magic(int type, int row, int column)
    {
        this.type = type;
        this.row = row;
        this.column = column;
        x = column << 4;
        y = row << 4;
        if (type == MainCanvas.STUFF_BOMB) 
        {
            animation = new Sprite(MainCanvas.image_bomb, 16, 16);
            animation.setFrameSequence(bombSeq);
            animation.setPosition(x, y);
        } 
        else if (type >= MainCanvas.STUFF_LASER_UP && type <= MainCanvas.STUFF_LASER_LEFT) 
        {
            animation = new Sprite(MainCanvas.image_laser, 24, 20);
            int d = type - MainCanvas.STUFF_LASER_UP;
            animation.setFrameSequence(laserSeq[d]);
            if (d == 3)
                animation.setTransform(Sprite.TRANS_MIRROR);
            animation.setPosition(x - 4, y - 4);
        }
    }
    
    public boolean apply(int tool)
    {
        //石头对任何工具都无效
        if (type == MainCanvas.STUFF_STONE)
            return false;
        switch (tool) 
        {
            case MainCanvas.TOOL_HAMMER:
            //锤头将破害炸弹、风扇、激光器、镜子
            if (status >= STATUS_DYING)
                return false;
            status = STATUS_DYING;
            if (type == MainCanvas.STUFF_MIRROR_CCW || type == MainCanvas.STUFF_MIRROR_CW)
            {
                animation = new Sprite(MainCanvas.image_broking, 24, 24);
                if (type == MainCanvas.STUFF_MIRROR_CW )
                    animation.setTransform(Sprite.TRANS_MIRROR);
                animation.setPosition(x - 4, y - 8);
                animation.setFrameSequence(brokingSeq);
            }
            else 
            {
                animation = new Sprite(MainCanvas.image_explode, 16, 19);
                animation.setFrameSequence(explodeSeq);
                animation.setPosition(x - 2, y - 2);
                if (type == MainCanvas.STUFF_BOMB)
                {
                    int ottaX = MainCanvas.otta.getRefPixelX();
                    int ottaY = MainCanvas.otta.getRefPixelY();
                    // 假如otta在火力范围内,则杀死它
                    if (ottaY < y + 24 && ottaY > y - 24 && ottaX < x + 24
                            && ottaX > x - 24 && MainCanvas.otta.status != Otta.STATUS_HOT)
                        MainCanvas.otta.die();
                }
            }            
            return true;
            case MainCanvas.TOOL_TORCH:
                // 火把将点燃处于常态的炸弹
                if (type == MainCanvas.STUFF_BOMB && status == STATUS_NORMAL) 
                {
                    status = STATUS_FIRING;
                    return true;
                }
                break;
            case MainCanvas.TOOL_POWER:
                //水桶将熄灭点燃状态的炸弹
                if (type == MainCanvas.STUFF_BOMB && status == STATUS_FIRING)
                {
                    status = STATUS_NORMAL;
                    animation.setFrame(0);
                    return true;
                }
                break;
            case MainCanvas.TOOL_WRENCH_CW:
            case MainCanvas.TOOL_WRENCH_CCW:
                //扳手将旋转镜子、风扇、激光器
                
                if (type >= MainCanvas.STUFF_LASER_UP && type <= MainCanvas.STUFF_LASER_LEFT
                    && status == STATUS_NORMAL) 
                {
                    int d = type - MainCanvas.STUFF_LASER_UP;
                    d = (tool == MainCanvas.TOOL_WRENCH_CW) ? ++d % 4 : (--d + 4) % 4;
                    animation.setFrameSequence(laserSeq[d]);
                    animation.setTransform((d == 3) ? Sprite.TRANS_MIRROR
                        : Sprite.TRANS_NONE);
                    type = MainCanvas.STUFF_LASER_UP + d;
                    animation.setPosition(x - 4, y - 4);
                    return true;
                } 
                if (type <= MainCanvas.STUFF_MIRROR_CCW && type >= MainCanvas.STUFF_MIRROR_CW
                    && status == STATUS_NORMAL) 
                {
                    type = (type == MainCanvas.STUFF_MIRROR_CCW) ? MainCanvas.STUFF_MIRROR_CW
                        : MainCanvas.STUFF_MIRROR_CCW;
                    return true;
                }
                break;
            case MainCanvas.LASER:
                if (status == STATUS_NORMAL)
                    status = STATUS_FIRING;
        }
        return false;
    }
    
    public void draw(Graphics g)
    {
        switch(status) 
        {
            case STATUS_NORMAL:
            if (type == MainCanvas.STUFF_STONE)
            {
                //绘制石头
                g.drawRegion(MainCanvas.image_stone, MainCanvas.floor * 16, 0, 16, 23,
                        Sprite.TRANS_NONE, x, y - 7, 20);
            } 
            else if (type >= MainCanvas.STUFF_LASER_UP && type <= MainCanvas.STUFF_LASER_LEFT)
            {
                animation.nextFrame();
                animation.paint(g);
            } 
            else if (type == MainCanvas.STUFF_MIRROR_CCW ||  type == MainCanvas.STUFF_MIRROR_CW) 
            {
                g.drawRegion(MainCanvas.image_mirror, 0, 0, 16, 24,
                        (type == MainCanvas.STUFF_MIRROR_CCW) ? Sprite.TRANS_NONE
                                : Sprite.TRANS_MIRROR, x, y - 8, 20);
            }
            else 
            {
                animation.paint(g);
            }
            break;
            case STATUS_FIRING:
                animation.nextFrame();
                animation.paint(g);
                if (type == MainCanvas.STUFF_BOMB) 
                {
                    if (isLastFrame())
                        apply(MainCanvas.TOOL_HAMMER);
                } 
                else
                {
                    int x1 = x + 3;
                    int y1 = y + 3;
                    g.drawImage(MainCanvas.image_firing, x1 + (random.nextInt() % 5), y1
                        + (random.nextInt() % 5), 20);
                    g.drawImage(MainCanvas.image_firing, x1 + (random.nextInt() % 5), y1
                        + (random.nextInt() % 5), 20);
                    if (--firingduration == 0)
                        apply(MainCanvas.TOOL_HAMMER);
                }
                break;
            case STATUS_DYING:
                animation.nextFrame();
                if (type == MainCanvas.STUFF_BOMB) 
                {
                    int sRow = (row > 1) ? row - 1 : 0;
                    int sColumn = (column > 1) ? column - 1 : 0;
                    int eRow = (row < 10) ? row + 1 : 11;
                    int eColumn = (column < 8) ? column + 1 : 9;
                    for (int i = sRow; i <= eRow; i++) 
                    {
                        for (int j = sColumn; j <= eColumn; j++)
                        {
                            animation.setPosition(j * 16 - 2, i * 16 - 2);
                            animation.paint(g);
                            if (animation.getFrame() == 3) 
                            {
                                Magic stuff = MainCanvas.stuffs[i][j];
                                if (stuff != null)
                                    stuff.apply(MainCanvas.TOOL_HAMMER);
                            }
                        }
                    }
                } 
                else 
                {
                    animation.paint(g);
                } 
                if (isLastFrame()) status = STATUS_DEAD;
                break;
        }
    }
    
    public boolean isLastFrame() 
    {
        return animation.getFrame() == animation.getFrameSequenceLength() - 1;
    }
}

⌨️ 快捷键说明

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