Wall类

package com.bigdata.tankbattle;

import java.awt.*;
//神奇的格式化代码的快捷键 : Ctrl + Alt + L

public class Wall {
    // 墙的属性
    public static final int WIDTH = 10;
    public static final int HEIGHT = 100;

    private int x;
    private int y;
    private int width;
    private int height;

    // 空参构造器
    public Wall() {

    }

    public Wall(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public Wall(int x, int y, int width, int height) {
        this(x, y);
        this.height = height;
        this.width = width;
    }

    // 画墙的方法
    public void fill(Graphics g) {

        g.fillRect(x, y, width, height);
        g.setColor(GamePanel.BLACK);
        g.setFont(GamePanel.PLAIN);
    }

    // 不可通过墙 向上
    public  void canTankThrough(Tank tank) {
        int direction = tank.getDirection();
        if (direction==Tank.UP){
            int tankLeftTopX = tank.getX() - tank.getWheelWidth();
            int tankLeftTopY = tank.getY() -Tank.SPEED - tank.getBarrelHeight();
            int wallLeftBottomY = y + height;
            int wallRightBottomX = x + width;
            int tankLeftBottomY=tank.getY() -Tank.SPEED+tank.getHeight()+Tank.NUM_FOUR;
            if (tankLeftTopX + tank.getWidth() + tank.getWheelWidth() * 2 > x && tankLeftTopX < wallRightBottomX && tankLeftTopY<wallLeftBottomY &&tankLeftBottomY>y){
                tank.setMove(false);
            }
        }else if (direction == Tank.LEFT){
            int tankLeftBottomY=tank.getY()+tank.getHeight()+tank.getWheelWidth();
            int wallLeftBottomY=y+height;
            int tankLeftBottomX=tank.getX()-Tank.SPEED-tank.getBarrelHeight();
            int tankRightBottmoX=tank.getX()-Tank.SPEED+tank.getWidth()+Tank.NUM_FOUR;
            int wallRightTopX=x+width;
            if (tankLeftBottomY>y && tankLeftBottomY-tank.getHeight()-tank.getWheelWidth()*2<wallLeftBottomY && tankLeftBottomX<wallRightTopX && tankRightBottmoX>x){
                tank.setMove(false);
            }
        }else if (direction == Tank.DOWN){
            int tankRightBottomX = tank.getX()+ tank.getWidth()+tank.getWheelWidth();
            int tankLeftBottomX = tank.getX()-tank.getWheelWidth();
            int tankBottomY = tank.getY()+tank.getHeight()+tank.getBarrelHeight()+Tank.SPEED;
            int tankTopY = tank.getY()-Tank.NUM_FOUR+Tank.SPEED;
            if (tankRightBottomX<x+width+tank.getWidth()+tank.getWheelWidth()*2 && tankLeftBottomX>x-tank.getWidth()-tank.getWheelWidth()*2 && tankBottomY>y && tankTopY<y+height){
                tank.setMove(false);
            }
        }else if (direction == Tank.RIGHT){
            int tankRightTopY = tank.getY()-tank.getWheelWidth();
            int wallLeftTopY = y;
            int tankRightBottomY =  tank.getY()+tank.getHeight()+tank.getWheelWidth();
            int wallLeftBottom =y+height;
            int tankRightBottomX=tank.getX()+tank.getWidth()+tank.getBarrelHeight()+Tank.SPEED;
            int tankLeftBottomX = tank.getX()-Tank.NUM_FOUR+Tank.SPEED;
            if (tankRightTopY>wallLeftTopY-tank.getHeight()-tank.getWheelWidth()*2 && tankRightBottomY<wallLeftBottom+tank.getHeight()+tank.getWheelWidth()*2 && tankRightBottomX>x && tankLeftBottomX<x){
                tank.setMove(false);
            }
        }
    }



    /**
     * get和set方法
     * @return
     */
    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 getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    @Override
    public String toString() {
        return "Wall{" +
                "x=" + x +
                ", y=" + y +
                ", width=" + width +
                ", height=" + height +
                '}';
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值