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

📄 tank.java

📁 java 自己写的小程序!!!欢迎使用!!!!
💻 JAVA
字号:
import java.awt.*;

public class Tank{
	// contral varibles
	private final int UP = 0;
	private final int DOWN = 1;
	private final int LEFT = 2;
	private final int RIGHT = 3;
	// Physical information about the tank
	private int speed;
	private int health = 1;
	private final int SIZE = 12;
	private int direction;
	private int status = 1;
	private int Tankhealth = 1;
	private int xPos, yPos, xVPos, yVPos;
	// borders
	private Rectangle Tank, TankVirtual;
	private Rectangle border = new Rectangle(10, 10, 502, 502);

//================================ Construct method ==========================================

	public Tank(int a, int b, int c, int d){
		xPos = a;
		yPos = b;
		xVPos = a;
		yVPos = b;
		direction = c;
		status = d;
		Tank = new Rectangle(xPos - 12, yPos - 12, 25, 25);
		TankVirtual  = new Rectangle(xVPos - 12, yVPos - 12, 25, 25);
	}

//================================ move ======================================================
	public void move(Enemy1[] E1, Enemy2[] E2, Enemy3[] E3, Enemy4[] E4, wall[] Wall, Steelwall[] SteelWall, river[] River){
		boolean canmoveup = true, canmovedown = true, canmoveleft = true, canmoveright = true;

		// Tank border vs. environment border
		if(xPos - SIZE - speed < border.x + 1 && direction == LEFT){
			xPos = border.x + 1 + SIZE;
			canmoveleft = false;
		}
		if(xPos + SIZE + speed> border.x + border.width - 1 && direction == RIGHT){
			xPos = border.x + border.width - 2 - SIZE;
			canmoveright = false;
		}
		if(yPos - SIZE - speed < border.y + 1 && direction == UP){
			yPos = border.y + 1 + SIZE;
			canmoveup = false;
		}
		if(yPos + SIZE + speed> border.y + border.height - 1 && direction == DOWN){
			yPos = border.y + border.height - 2 - SIZE;
			canmovedown = false;
		}

		// Tank border vs. wall border
		Rectangle[] Wallborder = new Rectangle[4];
		for(int j = 0; j < SteelWall.length; j++){
			if(SteelWall[j] != null){
				Wallborder = SteelWall[j].detailborder();
				for(int i = 0; i < 4; i++){
					if(Wallborder[i] != null){
						if(Tank.intersects(Wallborder[i])){
							if(yPos - SIZE - 7 - speed <= Wallborder[i].y + 6 && direction == UP){
								canmoveup = false;
								yPos = yVPos;
								break;
							}
							if(yPos + SIZE + 7 + speed >= Wallborder[i].y + 6 && direction == DOWN){
								canmovedown = false;
								yPos = yVPos;
								break;
							}
							if(xPos - SIZE - 7 - speed <= Wallborder[i].x + 6 && direction == LEFT){
								canmoveleft = false;
								xPos = xVPos;
								break;
							}
							if(xPos + SIZE + 7 + speed >= Wallborder[i].x + 6 && direction == RIGHT){
								canmoveright = false;
								xPos = xVPos;
								break;
							}
						}
					}
				}
			}
		}
		for(int j = 0; j < Wall.length; j++){
			if(Wall[j] != null){
				Wallborder = Wall[j].detailborder();
				for(int i = 0; i < 4; i++){
					if(Wallborder[i] != null){
						if(Tank.intersects(Wallborder[i])){
							if(yPos - SIZE - 7 - speed <= Wallborder[i].y + 6 && direction == UP){
								canmoveup = false;
								yPos = yVPos;
								break;
							}
							if(yPos + SIZE + 7 + speed >= Wallborder[i].y + 6 && direction == DOWN){
								canmovedown = false;
								yPos = yVPos;
								break;
							}
							if(xPos - SIZE - 7 - speed <= Wallborder[i].x + 6 && direction == LEFT){
								canmoveleft = false;
								xPos = xVPos;
								break;
							}
							if(xPos + SIZE + 7 + speed >= Wallborder[i].x + 6 && direction == RIGHT){
								canmoveright = false;
								xPos = xVPos;
								break;
							}
						}
					}
				}
			}
		}

		// Tank border vs. enemy border
		Rectangle Enemy1;
		for(int i = 0; i < E1.length; i++){
			if(E1[i] != null){
				Enemy1 = E1[i].getE1();
				if(Enemy1.contains(xPos + 1, yPos - 12 - speed) || Enemy1.contains(xPos - 1, yPos - 12 - speed)){
					canmoveup = false;
					yPos = E1[i].getyPos() + 25;
				}
				if(Enemy1.contains(xPos + 1, yPos + 12 + speed) || Enemy1.contains(xPos - 1, yPos + 12 + speed)){
					canmovedown = false;
					yPos = E1[i].getyPos() - 25;
				}
				if(Enemy1.contains(xPos - 12 - speed, yPos + 1) || Enemy1.contains(xPos - 12 - speed, yPos - 1)){
					canmoveleft = false;
					xPos = E1[i].getxPos() + 25;
				}
				if(Enemy1.contains(xPos + 12 + speed, yPos + 1) || Enemy1.contains(xPos + 12 + speed, yPos - 1)){
					canmoveright = false;
					xPos = E1[i].getxPos() - 25;
				}
			}
		}

		Rectangle Enemy2;
		for(int i = 0; i < E2.length; i++){
			if(E2[i] != null){
				Enemy2 = E2[i].getE1();
				if(Enemy2.contains(xPos + 1, yPos - 12 - speed) || Enemy2.contains(xPos - 1, yPos - 12 - speed)){
					canmoveup = false;
					yPos = E2[i].getyPos() + 25;
				}
				if(Enemy2.contains(xPos + 1, yPos + 12 + speed) || Enemy2.contains(xPos - 1, yPos + 12 + speed)){
					canmovedown = false;
					yPos = E2[i].getyPos() - 25;
				}
				if(Enemy2.contains(xPos - 12 - speed, yPos + 1) || Enemy2.contains(xPos - 12 - speed, yPos - 1)){
					canmoveleft = false;
					xPos = E2[i].getxPos() + 25;
				}
				if(Enemy2.contains(xPos + 12 + speed, yPos + 1) || Enemy2.contains(xPos + 12 + speed, yPos - 1)){
					canmoveright = false;
					xPos = E2[i].getxPos() - 25;
				}
			}
		}
		Rectangle Enemy3;
		for(int i = 0; i < E3.length; i++){
			if(E3[i] != null){
				Enemy3 = E3[i].getE1();
				if(Enemy3.contains(xPos + 1, yPos - 12 - speed) || Enemy3.contains(xPos - 1, yPos - 12 - speed)){
					canmoveup = false;
					yPos = E3[i].getyPos() + 25;
				}
				if(Enemy3.contains(xPos + 1, yPos + 12 + speed) || Enemy3.contains(xPos - 1, yPos + 12 + speed)){
					canmovedown = false;
					yPos = E3[i].getyPos() - 25;
				}
				if(Enemy3.contains(xPos - 12 - speed, yPos + 1) || Enemy3.contains(xPos - 12 - speed, yPos - 1)){
					canmoveleft = false;
					xPos = E3[i].getxPos() + 25;
				}
				if(Enemy3.contains(xPos + 12 + speed, yPos + 1) || Enemy3.contains(xPos + 12 + speed, yPos - 1)){
					canmoveright = false;
					xPos = E3[i].getxPos() - 25;
				}
			}
		}
		Rectangle Enemy4;
		for(int i = 0; i < E4.length; i++){
			if(E4[i] != null){
				Enemy4 = E4[i].getE1();
				if(Enemy4.contains(xPos + 1, yPos - 12 - speed) || Enemy4.contains(xPos - 1, yPos - 12 - speed)){
					canmoveup = false;
					yPos = E4[i].getyPos() + 25;
				}
				if(Enemy4.contains(xPos + 1, yPos + 12 + speed) || Enemy4.contains(xPos - 1, yPos + 12 + speed)){
					canmovedown = false;
					yPos = E4[i].getyPos() - 25;
				}
				if(Enemy4.contains(xPos - 12 - speed, yPos + 1) || Enemy4.contains(xPos - 12 - speed, yPos - 1)){
					canmoveleft = false;
					xPos = E4[i].getxPos() + 25;
				}
				if(Enemy4.contains(xPos + 12 + speed, yPos + 1) || Enemy4.contains(xPos + 12 + speed, yPos - 1)){
					canmoveright = false;
					xPos = E4[i].getxPos() - 25;
				}
			}
		}

		// move
			if(direction == UP && canmoveup)
				yPos = yPos - speed;
			if(direction == DOWN && canmovedown)
				yPos = yPos + speed;
			if(direction == LEFT && canmoveleft)
				xPos = xPos - speed;
			if(direction == RIGHT && canmoveright)
				xPos = xPos + speed;

		for(int i = 0; i < River.length; i ++){
			if((River[i].Riverborder()).intersects(Tank)){
				xPos = xVPos;
				yPos = yVPos;
			}
		}

		// the graphical border of the tank
		Tank = new Rectangle(xPos - 12, yPos - 12, 25, 25);

		// the Virtual border of the tank which you can't see, but importnat for the calculations
		int a = (xPos - 10)/25;
		int b = (xPos - 10)%25;
		if(b < 7)
			b = 0;
		if(b > 18)
			b = 25;
		if((b < 19 && b > 6) || xPos < 17 || xPos > 492)
			b = 13;
		xVPos = a*25 + b + 10;

		int c = (yPos - 10)/25;
		int d = (yPos - 10)%25;
		if(d < 7)
			d = 0;
		if(d > 18)
			d = 25;
		if((d < 19 && d > 6) || yPos < 17 || yPos > 492)
			d = 13;

		yVPos = c*25 + d + 10;
		TankVirtual  = new Rectangle(xVPos - 12, yVPos - 12, 25, 25);
	}

//================================== other help methods======================================
	public int getImageNo(){
		return ((status - 1)*4 + direction);
	}

	public void ChangeFace(int a){
		direction = a;
		if(a == 0 || a == 1)
			xPos = xVPos;
		if(a == 2 || a == 3)
			yPos = yVPos;
		speed = 0;
	}

	public int getDirection(){
		return direction;
	}

	public void accelerate(){
		if(speed < 3)
			speed++;
	}

	public int getStatus(){
		return status;
	}

	public void changeStatus(int a){
		status = a;
	}

	public void powerup(){
		if(health < 2)
			health++;
	}

	public void hurt(){
		health--;
	}

	public int gettankhealth(){
		return health;
	}

	public Rectangle getBorder(){
		return Tank;
	}

	public Rectangle getVBorder(){
		return TankVirtual;
	}

	public int getxPos(){
		return xPos;
	}

	public int getyPos(){
		return yPos;
	}

	public int getxVPos(){
		return xVPos;
	}

	public int getyVPos(){
		return yVPos;
	}

	public void changePosition(int a, int b, int c){
		xPos = a;
		yPos = b;
		xVPos = a;
		yVPos = b;
		direction = c;
	}
}

⌨️ 快捷键说明

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