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

📄 tank.java

📁 坦克大战游戏源代码
💻 JAVA
字号:
import java.awt.*;

public class tank implements enemy{
	public int health;
	public int kills;
	public surface Surface;
	public tankBody body;
	public tankTower tower;
	public boolean turnLeft, turnRight, moveUp, moveDown, aimLeft, aimRight, aimUp, aimDown;
	public double speed, rSpeedB, rSpeedT, rSpeedG;
	public vector[] gun;
	public vector gunCentre;
	public double MaxAimAngle, MinAimAngle, currentAimAngle;
	public int fireInterval;
	public paste[] Paste;

	public tank(surface Surface, int xPos, int yPos, vector direction2D){
		health = 100;
		this.Surface = Surface;
		body = new tankBody(Surface, xPos, yPos, 0, 0, direction2D);
		tower = new tankTower(Surface, xPos, yPos, body.height, 2, direction2D);
		speed = 2;
		rSpeedB = Math.PI/96;
		rSpeedT = Math.PI/48;
		rSpeedG = 0.1;
		gun = new vector[6];
		defindGunPosition();
		MaxAimAngle = 20*Math.sin(Math.PI/6);
		MinAimAngle = -20*Math.sin(Math.PI/100);
		currentAimAngle = 4;
	}

	public void move(enemy[] enemys){
		if(turnLeft){
			body.rotate(rSpeedB);
			tower.rotate(rSpeedB);
		}
		if(turnRight){
			body.rotate(-rSpeedB);
			tower.rotate(-rSpeedB);
		}
		if(moveUp){
			checkIfCanMove("up", enemys);
			tower.x = body.x;
			tower.y = body.y;
			tower.defindStatus();
		}
		if(moveDown){
			checkIfCanMove("down", enemys);
			tower.x = body.x;
			tower.y = body.y;
			tower.defindStatus();
		}

		if(aimLeft)
			tower.rotate(rSpeedT);
		if(aimRight)
			tower.rotate(-rSpeedT);
		if(aimUp){
			if(currentAimAngle <MaxAimAngle)
				currentAimAngle+=rSpeedG;
		}
		if(aimDown){
			if(currentAimAngle > MinAimAngle)
				currentAimAngle-=rSpeedG;
		}
		defindGunPosition();

		if(fireInterval >0)
			fireInterval--;
	}

	public void defindGunPosition(){
		gun[0] = tower.centre.add(tower.direction3D.scale(tower.length/2)).add(tower.normal.scale(-tower.height/2));
		gun[1] = gun[0].add(tower.normal.scale(-1));
		gun[2] = gun[0].add(tower.direction3DCrossNor);
		gun[3] = gun[1].add(tower.direction3DCrossNor);
		gun[4] = gun[0].add(tower.direction3DCrossNor.scale(-1));
		gun[5] = gun[1].add(tower.direction3DCrossNor.scale(-1));
		gunCentre = tower.direction3D.scale(20).add(tower.normal.scale(-currentAimAngle));
	}

	public ammunition fireAmmo(){
		fireInterval = 35;
		return new ammunition(Surface, gunCentre, gun[0].add(gunCentre).add(new vector(body.x,body.y)), tower.direction3DCrossNor, 0, 0);
	}

	public void damage(){
		if(health >0)
			health-=(int)(Math.random()*10);
		if(health <=0)
			health = 0;
	}

	public void draw(Graphics g){
		Paste = new paste[11];
		for(int i = 0; i < 10; i++){
			if(i < 5)
				Paste[i] = body.Paste[i];
			else
				Paste[i] = tower.Paste[i-5];
		}
		vector[] v = new vector[]{gun[0], gun[0], gun[0].add(gunCentre), gun[0].add(gunCentre)};
		Paste[10] = new paste(v, tower.findCentre(v), Color.black, "Gun");

		decideWhichToDrawFirst();
		for(int i = 0; i < Paste.length; i++){
			if(Paste[i].s.equals("Gun"))
				drawGun(g);
			else{
				Paste[i].draw(g);
				if(Paste[i].s.equals("tankBody"))
					drawGun(g);
			}
		}
	}

	public void drawGun(Graphics g){
		g.setColor(Color.black);

		for(int i = 0; i < gun.length; i++){
			for(int j = 0; j < gun.length; j++)
				g.drawLine(gun[i].getX(), gun[i].getY(), gun[j].add(gunCentre).getX(), gun[j].add(gunCentre).getY());
		}

		if(fireInterval < 34 && fireInterval > 30){
			g.setColor(Color.orange);
			g.fillOval(gun[0].add(gunCentre).getX()- 2, gun[0].add(gunCentre).getY()- 2, 5, 5);
		}

	}

	public void decideWhichToDrawFirst(){
		paste temp;
		int j  = 1;
		while(j<Paste.length){
			for(int i = 0; i <Paste.length - j; i++){
				if(Paste[i].position > Paste[i+1].position){
					temp = Paste[i+1];
					Paste[i+1] = Paste[i];
					Paste[i] = temp;
				}
			}
			j++;
		}
	}

	public void checkIfCanMove(String s, enemy[] enemys){
		boolean canMove = true;
		vector nextPosition = new vector(1,1);
		if(s.equals("up"))
			nextPosition = centre().add(body.direction3D.scale(speed));
		else if(s.equals("down"))
			nextPosition = centre().add(body.direction3D.scale(-speed));
		for(int i = 0; i < enemys.length; i++){
			double distance = enemys[i].centre().subtract(nextPosition).length();
			if(distance < 30 && distance > 2.05){
				canMove = false;
				break;
			}
		}
		if(s.equals("up") && canMove){
			body.move(speed);
		}
		else if(s.equals("down") && canMove)
			body.move(-speed);
	}

	public double position(){
		vector c = new vector(0, 0, tower.z);
		return c.dot(new vector(1,-2,1));
	}

	public vector centre(){
		return new vector(tower.x, tower.y, tower.z + 10);
	}

	public boolean isAttacking(){
		return false;
	}

	public double distanceFromTarget(){
		return 0;
	}

	public int StuckTime(){
		return 0;
	}

	public vector getDirection3D(){
		return body.direction3D;
	}

	public vector miniMapPosition(){
		return new vector(0,0,0);
	}

}

⌨️ 快捷键说明

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