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 + '}'; } }
Wall类
最新推荐文章于 2022-09-17 09:46:19 发布