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

📄 tank.java

📁 手机上一个坦克游戏
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package demo;

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

import com.nokia.mid.ui.*;
import net.jscience.util.*;

public class Tank {

    public String userName; //坦克名称(用户名)
    public int speed; //射击的速率
    public int angle; //炮管的角度
    public int move; //还可以移动多少次
    public int life;
    public int tmp_life = 0;
    public int attackRadius;
    public int missile_num = 1;
    public Vector properties; //携带的物品:导弹 0$物品的Id$物品的名字$攻击力$导弹个数$攻击范围$

    //物品:1$物品的Id$物品的名字$血$移动力$
    public int direction;
    public int grade; //梯度
    public int basicDamage;
    public boolean isEnemy;
    public Image img_l; //坦克的图片
    public int tank_x; //x坐标位置
    public int tank_y; //y坐标位置
    public static int TANK_VSCALE = 24;
    public static int TANK_HSCALE = 18;

    //攻击力$导弹个数$攻击范围$移动力$
    public int INIT_LIFE;
    private int INIT_DEMAGE;

    //private int INIT_MISSILE;
    private int INIT_ATTACKAREA;
    private int INIT_MOVE;
    public boolean USED_PRO; //一轮内是否已经用过物品,用过就不能再用。

    public Tank( /*BattleField battleCan,/**/String tankName,
                                             int tankID, int move, int grade,
                                             int initDemage,
                                             int x, int y, int initLife,
                                             boolean isEnemy, int radius) {
        //tankID tankName tankPic move grade initLife initDamage
        speed = 0; //导弹的速率
        angle = 45; //炮管的角度
        INIT_LIFE = life = initLife;
        userName = tankName;
        //System.out.println("******** tankID *******" + tankID);
        try {
            byte abyte[] = Main.b("t_l_" + tankID + ".png");
            img_l = Image.createImage(abyte, 0, abyte.length);
            abyte = null;
        }
        catch (Exception e) {
            //System.out.println("hihihihi");
            e.printStackTrace();
        }
        this.isEnemy = isEnemy;
        //TANK_VSCALE = img_l.getHeight();
        //TANK_HSCALE = img_l.getWidth();
        tank_x = x; // 坦克中心点在屏幕上的坐标
        tank_y = y - TANK_VSCALE / 2;
        //System.out.println("tank_x : " + tank_x);
        //System.out.println("tank_y : " + tank_y);
        direction = x < 100 ? 1 : -1;
        INIT_MOVE = this.move = move;
        INIT_DEMAGE = basicDamage = initDemage;
        //System.out.println("basicDamage = " + basicDamage);
        INIT_ATTACKAREA = attackRadius = radius;
        this.grade = grade;
        //battleCanvas = battleCan; //战场
        properties = new Vector();
        USED_PRO = false;
        // 导弹 0$物品的Id$物品的名字$攻击力$导弹个数$攻击范围$
    }

    //用来确定炮管的两个坐标点
    //计算公式如下
    //x1 : 炮管开始x坐标x1 = k() + 9;
    //y1 : 炮管开始y坐标y1 = h() + 9;
    //x2 : 炮管末尾x坐标 x2 = x1 + 13  * cos(angle * t); // 13应该是炮管长度
    //y2 : 炮管末尾y坐标 y2 = y1 -  13 * sin(angle * t);
    /*public void getBarrelPosition() {
        //炮管的初始位置
        x1 = tank_x + 4;
        y1 = tank_y + 4;
        //根据角度g算出导弹发射装置的末尾位置,并保存到s,y2
        //先把角度g通toFP函数来转化为定点整数
        //t = toFP("0.0174532")
        //调用mul函数来对两个数进行乘法运算 k = 0.0174532*angle
        k = MathFP.mul(MathFP.toFP(angle), t);
        //cos(k)
        p = MathFP.cos(k);
        //sin(k)
        o = MathFP.sin(k);
        //toFP(13)
        h = MathFP.toFP(13);
        //toFP(x1)
        n = MathFP.toFP(x1);
        //toFP(y1)
        w = MathFP.toFP(y1);
        x2 = MathFP.toInt(MathFP.add(n, MathFP.mul(h, p)));
        if (direction == -1) {
            x2 = x2 - 2 * (x2 - x1); //相反方向
        }
        int i1 = MathFP.sub(w, MathFP.mul(h, o));
        y2 = MathFP.toInt(i1);
         }*/
    public void setInit() {
        move = INIT_MOVE;
        speed = 0;
        basicDamage = INIT_DEMAGE;
        attackRadius = INIT_ATTACKAREA;
        //angle = 25;
        missile_num = 1; //导弹数目
        USED_PRO = false; //没有用过物品
        Main.displayable.route.removeAllElements();
        Main.displayable.route.addElement(new Integer(tank_x));
        Main.displayable.route.addElement(new Integer(tank_y));

    }

    public void paint(Graphics g1) {
        if (direction == 1) { // 向右
            DirectGraphics dg = DirectUtils.getDirectGraphics(g1);
            if (img_l != null)
                dg.drawImage(img_l, tank_x - TANK_HSCALE / 2,
                             tank_y - TANK_VSCALE / 2, 20, 0x2000); //Image img, int x, int y, int anchor, int manipulation()FLIP_HORIZONTAL
        }
        else { // 向左
            if (img_l != null)
                g1.drawImage(img_l, tank_x - TANK_HSCALE / 2,
                             tank_y - TANK_VSCALE / 2,
                             Graphics.TOP | Graphics.LEFT); //0x2000, FLIP_HORIZONTAL
        }
    }

    //角度判断
    public void increaseAngle() {
        if (angle < 90) {
            angle++;
        }
    }

    //角度判断
    public void decreaseAngle() {
        if (angle > 0) {
            angle--;
        }
    }

    public void moveTank(int dir) {
        //boolean isRight = (dir == 1);
        boolean scope = false;
        //System.out.println("x = " + tank_x + " y = " + tank_y);
        if (dir == 1) { //向右
            //System.out.println("isRight!");
            scope = tank_x <= Main.displayable.MAP_WIDTH - TANK_HSCALE / 2;
        }
        else { //向左
            scope = tank_x >= TANK_HSCALE / 2;
        }
        if (scope) {
            //System.out.println("x = " + tank_x + " y = " + tank_y);
            int temp = Main.displayable.map.getTankY(this);
            //System.out.println("temp = " + temp);
            if (temp == -1) {
            }
            else {
                if (temp == -2) { // 坦克死亡
                    if (userName.equals(Main.displayable.userName)) { // 是轮到自己
                        Main.displayable.isMe = false;
                        tank_y = Main.displayable.MAP_HEIGHT;
                        tank_x = tank_x + dir * TANK_HSCALE / 2;
                        if (tank_x < TANK_HSCALE / 2) {
                            tank_x = TANK_HSCALE / 2;
                        }
                        else {
                            if (tank_x >
                                Main.displayable.MAP_WIDTH - 1 -
                                TANK_HSCALE / 2) {
                                tank_x = Main.displayable.MAP_WIDTH - 1 -
                                    TANK_HSCALE / 2;
                            }
                        }

                        Main.displayable.route.addElement(new Integer(tank_x));
                        Main.displayable.route.addElement(new Integer(tank_y));
                        /*System.out.println("add x = " + tank_x + ", y = " +
                                           tank_y);
                                                 Main.displayable.repaint(0, 0,
                                                 Main.displayable.screen_w,
                         Main.displayable.screen_h);/**/
                        HttpClientHolder httpClientHolder = new
                            HttpClientHolder();
                        httpClientHolder.WriteByte( (byte) 37);
                        Main.displayable.writeRoute(httpClientHolder);
                        Main.displayable.route.removeAllElements();
                        Main.displayable.mHttpConnection.addSendMessage(
                            httpClientHolder);
                    }
                    try {
                        Thread.sleep(100L);
                    }
                    catch (InterruptedException ex) {
                    }
                    life = 0;
                    Main.displayable.repaint();
                    Main.displayable.now = Main.displayable.nextTank();
                    Main.displayable.scrollToNow();
                    Main.displayable.now.setInit();
                    Main.displayable.setGameStatus();
                    return;

                }

⌨️ 快捷键说明

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