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

📄 missile.java

📁 手机上一个坦克游戏
💻 JAVA
字号:
package demo;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

import java.io.*;
import javax.microedition.lcdui.*;

import net.jscience.util.*;

public class Missile
    implements Runnable {

    //导弹的速率
    public boolean isMain; // 是否控制切换的主导弹
    private int speed;
    public int demage; // 对其他坦克的杀伤力
    public int attackRadius; // 攻击范围(对地形的破坏范围)
    private int angle;
    private Image pic; //导弹的图片
    public int direction;

    //public static int m_index = 0;
    //public boolean isCrash; //是否爆炸
    private int barrelEndX; //导弹发射装置末尾的x坐标
    private int barrelEndY; //导弹发射装置末尾的y坐标
    public int m_x; //导弹的x坐标
    public int m_y; //导弹的y坐标
    public int wind;
    private boolean needRepaintScreen = false;
    public boolean isRun = true;
    public static int missileIndex = 0;
    private Thread ctlThread;

    static final int s = MathFP.toFP("0.0174532");

    // 以下是一些中间计算用的变量
    int m;
    int f;
    int fx;
    int c;
    int t;
    int j;
    int q;
    int p;
    int d;

    public Missile(Tank tank, boolean main, int angle_add,
                   int windPower) {

        wind = windPower;
        //System.out.println("counstructor wind = " + wind);
        m = MathFP.toFP("0.1");
        f = MathFP.toFP("0.1");
        fx = MathFP.toFP("0.1");
        c = MathFP.toFP("9.82");
        t = MathFP.toFP("0.5");

        try {
            byte abyte[] = Main.b("bullet.png");
            pic = Image.createImage(abyte, 0, abyte.length);
            abyte = null;
        }
        catch (Exception exception) {
            exception.printStackTrace();
        }
        isMain = main;
        direction = tank.direction;
        demage = tank.basicDamage;
        attackRadius = tank.attackRadius;
        speed = tank.speed;
        //name = tank.userName;
        if (tank.angle + angle_add > 90) {
            angle = 90;
        }
        else {
            if (tank.angle + angle_add < 0) {
                angle = 0;
            }
            else {
                angle = tank.angle + angle_add;
            }
        }

        barrelEndX = tank.tank_x + tank.direction * 8;
        barrelEndY = tank.tank_y - 8;
        m_x = tank.tank_x + tank.direction * 8; //炮管末尾的x坐标
        m_y = tank.tank_y - 8; //炮管末尾的y坐标
        //j = 角度* 0.0174532
        j = MathFP.mul(MathFP.toFP(angle), s);
        //q = cos(j)
        q = MathFP.cos(j);
        //p = sin(j)
        p = MathFP.sin(j);
        //d = toFP(l)速率
        d = MathFP.toFP(speed);

        ctlThread = new Thread(this); // 开始一个导弹线程
        ctlThread.start();
    }

    //导弹坐标确定公式
    // 速率 velocity 角度 angle 系数 f
    //x = velocity * cos(angle) * f
    //tmp = velocity * sin(angle)*f - 0.5*9.82*f*f
    //y = tmp - 2*tmp + 原始y坐标
    //f = f + 0.1

    public void run() {
        while (isRun) {
            if (m_x < Main.displayable.MAP_WIDTH && m_x > 0 &&
                m_y > 0 && m_y < Main.displayable.MAP_HEIGHT) { // 在地图范围内
                if (!Main.displayable.isClision(this)) { //如果没有碰到任何东西,也就是不爆炸
                    //System.out.println("wind = " + wind);
                    if (direction == 1) { // 导弹向右
                        //i1 = 速率* cos(角度)*f
                        int i1 = 0;
                        if (wind > 0) { // 风向向右
                            i1 = MathFP.add(MathFP.mul(MathFP.mul(d,
                                q), f), fx);
                            // f = MathFP.toFP("0.1");q = cos(角度* 0.0174532);d = MathFP.toFP(speed);
                        }
                        else {
                            i1 = MathFP.sub(MathFP.mul(MathFP.mul(d,
                                q), f), fx);
                        }
                        int new_x = MathFP.toInt(i1) + barrelEndX; //确定x坐标
                        new_x = Math.min(new_x,
                                         Main.displayable.MAP_WIDTH);
                        if (new_x >
                            Main.displayable.screen_w * 3 / 4 -
                            Main.displayable.SCREEN_X &&
                            -Main.displayable.SCREEN_X <
                            Main.displayable.MAP_WIDTH -
                            Main.displayable.screen_w) { // 判断是否需要水平滚屏
                            int trans = - (new_x - m_x);
                            Main.displayable.setTranslateX(trans);
                            needRepaintScreen = true;
                        }
                        m_x = new_x;
                    }

                    if (direction == -1) { // 导弹向左
                        //j1 = 速率* cos(角度)*f
                        int j1 = 0;
                        if (wind < 0) { // 风向向左
                            j1 = MathFP.add(MathFP.mul(MathFP.mul(d,
                                q), f), fx);
                        }
                        else {
                            j1 = MathFP.sub(MathFP.mul(MathFP.mul(d,
                                q), f), fx);
                        }
                        int new_x = barrelEndX - MathFP.toInt(j1);
                        new_x = Math.max(new_x, 0);
                        if (new_x <
                            -Main.displayable.SCREEN_X +
                            Main.displayable.screen_w / 4 &&
                            Main.displayable.SCREEN_X < 0) { // 判断是否需要左滚屏
                            int trans = - (new_x - m_x);
                            Main.displayable.setTranslateX(trans);
                            needRepaintScreen = true;
                        }
                        m_x = new_x;
                    }

                    // k1 = 速率*sin(角度)*f  - 0.5*9.82*f*f
                    int k1 = MathFP.sub(MathFP.mul(MathFP.mul(d, p),
                        f),
                                        MathFP.mul(t,
                        MathFP.mul(c, MathFP.mul(f, f))));

                    int new_y = (MathFP.toInt(k1) - 2 * MathFP.toInt(k1)) +
                        barrelEndY;
                    //int new_y = k1;

                    if (new_y < m_y) { // 导弹向上飞
                        if (new_y <
                            Main.displayable.screen_h / 4 -
                            Main.displayable.SCREEN_Y &&
                            -Main.displayable.SCREEN_Y > 0) { // 是否需要向上滚屏
                            Main.displayable.setTranslateY(m_y -
                                new_y);
                            needRepaintScreen = true;
                        }
                    }
                    else { // 导弹向下落
                        if (new_y >
                            Main.displayable.screen_h * 3 / 4 -
                            Main.displayable.SCREEN_Y &&
                            -Main.displayable.SCREEN_Y <=
                            Main.displayable.MAP_HEIGHT -
                            Main.displayable.screen_h) {
                            Main.displayable.setTranslateY( -Math.
                                min(
                                new_y -
                                m_y,
                                Main.displayable.MAP_HEIGHT -
                                Main.displayable.screen_h +
                                Main.displayable.SCREEN_Y));
                            needRepaintScreen = true;
                        }
                    }

                    m_y = new_y;
                    //f = f + 0.1
                    f = MathFP.add(f, m);
                    //fx = fx + m * |wind| / 3
                    fx = MathFP.add(fx,
                                    MathFP.mul(m,
                                               MathFP.div(Math.abs(wind),
                        3) /**/));
                }
                else {
                    m_x = m_x - 10;
                    m_y = m_y - 7;
                    updateItemsPos();
                    needRepaintScreen = true;
                    break;
                }
            }
            else { //超出了屏幕范围
                break;
            }
            if (needRepaintScreen) {
                Main.displayable.repaint();
                needRepaintScreen = false;
            }
            else {
                Main.displayable.repaint(Main.displayable.SCREEN_X + m_x - 5,
                                         Main.displayable.SCREEN_Y + m_y - 5,
                                         20, 20);
            }

        }

        try {
            controlExplode();
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        this.ctlThread = null;

    }

    private void controlExplode() {
        Missile.missileIndex++;
        if (Main.displayable.getNewLives) {
            for (int k = 0; k < Main.displayable.runs.length; k++) {
                Tank ct = null;
                try {
                    ct = Main.displayable.runs[k];
                }
                catch (ArrayIndexOutOfBoundsException ex) {
                    break;
                }
                ct.life = ct.tmp_life;
            }
            Main.displayable.getNewLives = false;
        }
        if (Main.displayable.gameStatus > 5) {
            if (isMain) { // 如果是主导弹,并且不是自己发的,控制战场;
                isRun = false;
                Missile.missileIndex = 0;

                Main.displayable.repaint();
                Main.displayable.now = Main.displayable.nextTank();
                Main.displayable.scrollToNow();
                Main.displayable.now.setInit();
                Main.displayable.setGameStatus();
                Main.displayable.sentMissile = false; //发射导弹标志位设置为false
                Main.displayable.missiles = null;

                //return;
            }
            else { //產生新的導彈
                if (Main.displayable.gameStatus != 5) {
                    if (Missile.missileIndex < 2) {
                        Main.displayable.missiles = new Missile(
                            Main.displayable.now, false, 0,
                            Main.displayable.missiles.wind);
                    }
                    else { //產生主導彈
                        Main.displayable.missiles = new Missile(
                            Main.displayable.now, true, 20,
                            Main.displayable.missiles.wind);
                    }
                }
                else {
                    Missile.missileIndex = 0;
                }
            }
        }

    }

    //爆炸以後修改箱子的位置
    public void updateItemsPos() {
        if (Main.displayable.items != null &&
            Main.displayable.items.size() != 0) {
            //Resource tmp = null;
            for (int i = 0; i < Main.displayable.items.size(); i++) {
                if ( (Resource) Main.displayable.items.elementAt(i) != null) {
                    if ( ( (Resource) Main.displayable.items.elementAt(i)).y >
                        Main.displayable.MAP_HEIGHT - Main.displayable.BOX_SIZE) {
                        System.out.println("out of range " +
                                           ( (Resource) Main.displayable.items.
                                            elementAt(i)).y +
                                           ( (Resource) Main.displayable.items.
                                            elementAt(i)).prop);
                        ( (Resource) Main.displayable.items.elementAt(i)).
                            stopAnimation();
                        Main.displayable.items.removeElementAt(i);
                    }
                    else {
                        ( (Resource) Main.displayable.items.elementAt(i)).y =
                            Main.displayable.map.topLine[ ( (Resource) Main.
                            displayable.items.elementAt(i)).x] -
                            Main.displayable.BOX_SIZE;
                    }
                }
            }
        }
    }

    //画出导弹
    public void paint(Graphics g1) {
        if (isRun) {
            if (pic != null)
                g1.setColor(121, 61, 0);
            g1.fillRoundRect(m_x, m_y, 6, 6, 10, 10);
        }
    }
}

⌨️ 快捷键说明

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