当前位置: 首页 > 工具软件 > Bullet > 使用案例 >

Bullet类

逑兴安
2023-12-01
package com.bigdata.tankbattle;

    import java.awt.*;

    public class Bullet {

        private Tank tank;
        // 子弹位子
        private int x;
        private int y;
        private int diameter;
        private int direction;
        // 子弹速度
        public static final int SPEED=5;
        // 子弹直径
        public static final int DIAMETER=7;

        /**
         *  空参构造器
         *  并且传参
         */
        //********************************
        public Bullet(){

        }
        public Bullet(Tank tank,int diameter){
            this.tank=tank;
            this.diameter=diameter;
            direction=tank.getDirection();
            x=tank.getX()+tank.getWidth()/2;
            y=tank.getY()+tank.getHeight()/2-4;
        }

        public void fill(Graphics g){
            g.fillOval(x,y,diameter,diameter);
        }
        // **********************************
        // 移动的距离
        public void move() {
            if (direction == Tank.UP) {
                y -= SPEED;
            } else if (direction == Tank.RIGHT) {
                x += SPEED;
            } else if (direction == Tank.DOWN) {
                y += SPEED;
            } else if (direction == Tank.LEFT) {
                x -= SPEED;
            }
        }

        /**
         * set和get
         * @return
         */
        public int getDirection() {
            return direction;
        }

        public void setDirection(int direction) {
            this.direction = direction;
        }

        public Tank getTank(){
            return tank;
        }
        public void setTank(Tank tank){
            this.tank=tank;
        }
        public int getX() {
            return x;
        }

        public void setX(int x) {
            this.x = x;
        }

        public int getY() {
            return y;
        }

        public void setY(int y) {
            this.y = y;
        }

        public int getDiameter() {
            return diameter;
        }

        public void setDiameter(int diameter) {
            this.diameter = diameter;
        }
    }

 

 类似资料: