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

📄 enemy2.java

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

public class Enemy2{
	// control 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 enemy tank
	private int speed;
	private int interval;
	private int destination = 1;
	private final int SIZE = 12;
	private int xPos, yPos, xVPos, yVPos;
	// borders
	private Rectangle E1, EV1;
	private Rectangle border = new Rectangle(10, 10, 502, 502);
	// others
	private boolean flashing;
	private int falshingTime = 0;

//====================================== the construct method ================================

	public Enemy2(int a, int b){
		if((int)(Math.random()*10) > 6)
			destination = (int)(Math.random()*4);
		interval = (int)(Math.random()*300);
		xPos = a;
		yPos = b;
		xVPos = a;
		yVPos = b;
		E1 = new Rectangle(xPos - SIZE, yPos - SIZE, 25, 25);
		EV1 =  new Rectangle(xVPos - SIZE, yVPos - SIZE, 25, 25);
	}

//================================================ move ======================================

	public void move(Tank tank, Enemy1[] e1, Enemy2[] e2, Enemy3[] e3, Enemy4[] e4, int index, wall[] Wall, Steelwall[] SteelWall, river[] River, int timestop){
		if(speed < 4)
			speed++;

		if(flashing)
			falshingTime++;

		// Enemy2 border vs. environment border
		boolean notoutofborder = true;
		if(xPos - SIZE - speed < border.x + 1 && destination == LEFT){
			xPos = border.x + 1 + SIZE;
			notoutofborder = false;
		}
		if(xPos + SIZE + speed> border.x + border.width - 1 && destination == RIGHT){
			xPos = border.x + border.width - 2 - SIZE;
			notoutofborder = false;
		}
		if(yPos - SIZE - speed < border.y + 1 && destination == UP){
			yPos = border.y + 1 + SIZE;
			notoutofborder = false;
		}
		if(yPos + SIZE + speed> border.y + border.height - 1 && destination == DOWN){
			yPos = border.y + border.height - 2 - SIZE;
			notoutofborder = false;
		}


		// Enemy2 border vs. Tank border
		boolean canmoveup = true, canmovedown = true, canmoveleft = true, canmoveright = true;
		Rectangle tankborder = tank.getBorder();
		if(tankborder.contains(xPos + 1, yPos - 12 - speed) || tankborder.contains(xPos - 1, yPos - 12 - speed)){
			canmoveup = false;
			while(destination == 0)
				destination = (int)(Math.random()*4);
			yPos = tank.getyPos() + 25;
		}
		if(tankborder.contains(xPos + 1, yPos + 12 + speed) || tankborder.contains(xPos - 1, yPos + 12 + speed)){
			canmovedown = false;
			while(destination == 1)
				destination = (int)(Math.random()*4);
			yPos = tank.getyPos() - 25;
		}
		if(tankborder.contains(xPos - 12 - speed, yPos + 1) || tankborder.contains(xPos - 12 - speed, yPos - 1)){
			canmoveleft = false;
			while(destination == 2)
				destination = (int)(Math.random()*4);
			xPos = tank.getxPos() + 25;
		}
		if(tankborder.contains(xPos + 12 + speed, yPos + 1) || tankborder.contains(xPos + 12 + speed, yPos - 1)){
			canmoveright = false;
			while(destination == 3)
				destination = (int)(Math.random()*4);
			xPos =tank.getxPos() - 25;
		}

		// Enemy2 border vs. enemie1 borders
		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;
					while(destination == 0)
						destination = (int)(Math.random()*4);
					yPos = e1[i].getyVPos() + 25;
				}
				if(Enemy1.contains(xPos + 1, yPos + 12 + speed) || Enemy1.contains(xPos - 1, yPos + 12 + speed)){
					canmovedown = false;
					while(destination == 1)
						destination = (int)(Math.random()*4);
					yPos = e1[i].getyVPos() - 25;
				}
				if(Enemy1.contains(xPos - 12 - speed, yPos + 1) || Enemy1.contains(xPos - 12 - speed, yPos - 1)){
					canmoveleft = false;
					while(destination == 2)
						destination = (int)(Math.random()*4);
					xPos = e1[i].getxVPos() + 25;
				}
				if(Enemy1.contains(xPos + 12 + speed, yPos + 1) || Enemy1.contains(xPos + 12 + speed, yPos - 1)){
					canmoveright = false;
					while(destination == 3)
						destination = (int)(Math.random()*4);
					xPos = e1[i].getxVPos() - 25;
				}
			}
		}

		// Enemy2 border vs. Enwmy2 border
		Rectangle Enemy2;
		for(int i = 0; i < e2.length; i++){
			if(e2[i] != null && i != index){
				Enemy2 = e2[i].getE1();
				if(Enemy2.contains(xPos + 1, yPos - 12 - speed) || Enemy2.contains(xPos - 1, yPos - 12 - speed)){
					canmoveup = false;
					while(destination == 0)
						destination = (int)(Math.random()*4);
					yPos = e2[i].getyVPos() + 25;
				}
				if(Enemy2.contains(xPos + 1, yPos + 12 + speed) || Enemy2.contains(xPos - 1, yPos + 12 + speed)){
					canmovedown = false;
					while(destination == 1)
						destination = (int)(Math.random()*4);
					yPos = e2[i].getyVPos() - 25;
				}
				if(Enemy2.contains(xPos - 12 - speed, yPos + 1) || Enemy2.contains(xPos - 12 - speed, yPos - 1)){
					canmoveleft = false;
					while(destination == 2)
						destination = (int)(Math.random()*4);
					xPos = e2[i].getxVPos() + 25;
				}
				if(Enemy2.contains(xPos + 12 + speed, yPos + 1) || Enemy2.contains(xPos + 12 + speed, yPos - 1)){
					canmoveright = false;
					while(destination == 3)
						destination = (int)(Math.random()*4);
					xPos = e2[i].getxVPos() - 25;
				}
			}
		}
		// Enemy 2 borders vd Enemy3 borders
		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;
					while(destination == 0)
						destination = (int)(Math.random()*4);
					yPos = e3[i].getyVPos() + 25;
				}
				if(Enemy3.contains(xPos + 1, yPos + 12 + speed) || Enemy3.contains(xPos - 1, yPos + 12 + speed)){
					canmovedown = false;
					while(destination == 1)
						destination = (int)(Math.random()*4);
					yPos = e3[i].getyVPos() - 25;
				}
				if(Enemy3.contains(xPos - 12 - speed, yPos + 1) || Enemy3.contains(xPos - 12 - speed, yPos - 1)){
					canmoveleft = false;
					while(destination == 2)
						destination = (int)(Math.random()*4);
					xPos = e3[i].getxVPos() + 25;
				}
				if(Enemy3.contains(xPos + 12 + speed, yPos + 1) || Enemy3.contains(xPos + 12 + speed, yPos - 1)){
					canmoveright = false;
					while(destination == 3)
						destination = (int)(Math.random()*4);
					xPos = e3[i].getxVPos() - 25;
				}
			}
		}
		// Enemy2 border vs. enemie4 borders
		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;
					while(destination == 0)
						destination = (int)(Math.random()*4);
					yPos = e4[i].getyVPos() + 25;
				}
				if(Enemy4.contains(xPos + 1, yPos + 12 + speed) || Enemy4.contains(xPos - 1, yPos + 12 + speed)){
					canmovedown = false;
					while(destination == 1)
						destination = (int)(Math.random()*4);
					yPos = e4[i].getyVPos() - 25;
				}
				if(Enemy4.contains(xPos - 12 - speed, yPos + 1) || Enemy4.contains(xPos - 12 - speed, yPos - 1)){
					canmoveleft = false;
					while(destination == 2)
						destination = (int)(Math.random()*4);
					xPos = e4[i].getxVPos() + 25;
				}
				if(Enemy4.contains(xPos + 12 + speed, yPos + 1) || Enemy4.contains(xPos + 12 + speed, yPos - 1)){
					canmoveright = false;
					while(destination == 3)
						destination = (int)(Math.random()*4);
					xPos = e4[i].getxVPos() - 25;
				}
			}
		}


		// Enemy2 border vs. obstacles borders
		boolean nottouchobstacles = true;
		Rectangle[] Wallborder = new Rectangle[4];
		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(E1.intersects(Wallborder[i])){
							if(yPos - SIZE - 7 - speed <= Wallborder[i].y + 6 && destination == 0){
								canmoveup = false;
								nottouchobstacles = false;
								break;
							}
							if(yPos + SIZE + 7 + speed >= Wallborder[i].y + 6 && destination == 1){
								canmovedown = false;
								nottouchobstacles = false;
								break;
							}
							if(xPos - SIZE - 7 - speed <= Wallborder[i].x + 6 && destination == 2){
								canmoveleft = false;
								nottouchobstacles = false;
								break;
							}
							if(xPos + SIZE + 7 + speed >= Wallborder[i].x + 6 && destination == 3){
								canmoveright = false;
								nottouchobstacles = false;
								break;
							}
						}
					}
				}
			}
		}
		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(E1.intersects(Wallborder[i])){
							if(yPos - SIZE - 7 - speed <= Wallborder[i].y + 6 && destination == 0){
								canmoveup = false;
								nottouchobstacles = false;
								break;
							}
							if(yPos + SIZE + 7 + speed >= Wallborder[i].y + 6 && destination == 1){
								canmovedown = false;
								nottouchobstacles = false;
								break;
							}
							if(xPos - SIZE - 7 - speed <= Wallborder[i].x + 6 && destination == 2){
								canmoveleft = false;
								nottouchobstacles = false;
								break;
							}
							if(xPos + SIZE + 7 + speed >= Wallborder[i].x + 6 && destination == 3){
								canmoveright = false;
								nottouchobstacles = false;
								break;
							}
						}
					}
				}
			}
		}






		// move
		if(timestop >= 1)
			speed = 0;
		if(notoutofborder && nottouchobstacles){
			if(destination == UP && canmoveup)
				yPos = yPos - speed;
			if(destination == DOWN && canmovedown)
				yPos = yPos + speed;
			if(destination == LEFT && canmoveleft)
				xPos = xPos - speed;
			if(destination == RIGHT && canmoveright)
				xPos = xPos + speed;
			interval = interval - speed;
		}

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

		//change the movement if the enemy tank meet anything that stop its movement
		if(!notoutofborder || !nottouchobstacles || interval <= 0){
			if((int)(Math.random()*10) > 8)
				destination = (int)(Math.random()*4);
			if(destination == 1 || destination == 0)
				xPos = xVPos;
			if(destination == 2 || destination == 3)
				yPos = yVPos;
			interval = (int)(Math.random()*300);
		}

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

		// the virtual border of the tank
		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;
		EV1 =  new Rectangle(xVPos - SIZE, yVPos - SIZE, 25, 25);
	}

//========================================= other help methods ===============================
	public int getImageNo(){
		int ImageNo = destination;
		if(flashing && falshingTime%10 > 4)
			ImageNo = destination + 4;
		return ImageNo;
	}

	public int getDirection(){
		return destination;
	}

	public Rectangle getE1(){
		return E1;
	}

	public Rectangle getEV1(){
		return EV1;
	}

	public int getxPos(){
		return xPos;
	}

	public int getyPos(){
		return yPos;
	}

	public int getxVPos(){
		return xVPos;
	}

	public int getyVPos(){
		return yVPos;
	}

	public void flashing(){
		flashing = true;
	}

	public boolean isFlashing(){
		return flashing;
	}

}

⌨️ 快捷键说明

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