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

📄 tank.java

📁 坦克大战的代码 自己写的 部分内容没有完成
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.KeyEvent;

public class Tank {
	int x, y;
	boolean L = false, R = false, U = false, D = false;
	int tankspeed = 5;
	enum Direction {LEFT, RIGHT, UP, DOWN, LEFT_UP, RIGHT_UP, RIGHT_DOWN, LEFT_DOWN, STOP}
	Direction dir = Direction.STOP;
	Direction ptdir = Direction.DOWN;
	TankClient tk;
	
	public Tank(int x, int y, TankClient tk) {
		this.x = x;
		this.y = y;
		this.tk = tk;
	}
	
	public void draw(Graphics g) {
		Color c =  g.getColor();
		g.setColor(Color.PINK);
		g.fillOval(x, y, 30, 30);
		g.setColor(Color.BLACK);
		ptdraw(g);
		move();
		g.setColor(c);
	}
	
	public void ptdraw(Graphics g) {
		if(dir != Direction.STOP) {ptdir = dir;}
		switch(ptdir) {
		case LEFT :
			g.drawLine(x, y + 15,x + 15, y + 15);
			break;
		case LEFT_UP :
			g.drawLine(x, y, x + 15, y + 15);
			break;
		case UP :
			g.drawLine(x + 15, y, x + 15, y + 15);
			break;
		case RIGHT_UP :
			g.drawLine(x +30, y,x + 15, y + 15);
			break;
		case RIGHT :
			g.drawLine(x + 15, y + 15, x + 30, y + 15);
			break;
		case RIGHT_DOWN :
			g.drawLine(x +30, y + 30,x + 15, y + 15);
			break;
		case DOWN :
			g.drawLine(x + 15, y + 30,x + 15, y + 15);
			break;
		case LEFT_DOWN :
			g.drawLine(x, y + 30,x + 15, y + 15);
			break;
		}
	}
	
	public Missile shoot() {
		Missile m = new Missile(x, y, ptdir);
		return m;
	}
	
	public void keyPressed(KeyEvent e) {
		int i = e.getKeyCode();
		
		if(i == KeyEvent.VK_UP) {U = true;}
		if(i == KeyEvent.VK_DOWN) {D = true;}
		if(i == KeyEvent.VK_LEFT) {L = true;}
		if(i == KeyEvent.VK_RIGHT) {R = true;}
		if(i == KeyEvent.VK_CONTROL) {tk.missiles.add(shoot());}
		
		if(U && L && !D && !R) {dir = Direction.LEFT_UP;}
		else if(U && !L && !D && R) {dir = Direction.RIGHT_UP;}
		else if(!U && !L && D && R) {dir = Direction.RIGHT_DOWN;}
		else if(!U && L && D && !R) {dir = Direction.LEFT_DOWN;}
		else if(U && !L && !D && !R) {dir = Direction.UP;}
		else if(!U && L && !D && !R) {dir = Direction.LEFT;}
		else if(!U && !L && D && !R) {dir = Direction.DOWN;}
		else if(!U && !L && !D && R) {dir = Direction.RIGHT;}
		else if(!U && !L && !D && !R) {dir = Direction.STOP;}
	}
	
	public void move() {
		switch(dir) {
		case LEFT :
			if(x>0) {x -= tankspeed;};
			break;
		case LEFT_UP :
			if((x>0) && (y>30)) {x -= tankspeed; y -= tankspeed;};
			break;
		case UP :
			if(y>30) {y -= tankspeed;};
			break;
		case RIGHT_UP :
			if((x<770) && (y>30)) {x += tankspeed; y -= tankspeed;};
			break;
		case RIGHT :
			if(x<770) {x += tankspeed;};
			break;
		case RIGHT_DOWN :
			if((x<770) && (y<570)) {x += tankspeed; y += tankspeed;};
			break;
		case DOWN :
			if(y<570) {y += tankspeed;};
			break;
		case LEFT_DOWN :
			if((x>0) && (y<570)) {x -= tankspeed; y += tankspeed;};
			break;
		}
	}
	
	public void keyReleased(KeyEvent e) {
		int key = e.getKeyCode();
		if(key == KeyEvent.VK_RIGHT) {R = false;}
		if(key == KeyEvent.VK_LEFT) {L = false;}
		if(key == KeyEvent.VK_UP) {U = false;}
		if(key == KeyEvent.VK_DOWN) {D = false;}
		
		if(U && L && !D && !R) {dir = Direction.LEFT_UP;}
		else if(U && !L && !D && R) {dir = Direction.RIGHT_UP;}
		else if(!U && !L && D && R) {dir = Direction.RIGHT_DOWN;}
		else if(!U && L && D && !R) {dir = Direction.LEFT_DOWN;}
		else if(U && !L && !D && !R) {dir = Direction.UP;}
		else if(!U && L && !D && !R) {dir = Direction.LEFT;}
		else if(!U && !L && D && !R) {dir = Direction.DOWN;}
		else if(!U && !L && !D && R) {dir = Direction.RIGHT;}
		else if(!U && !L && !D && !R) {dir = Direction.STOP;}
	}
}



⌨️ 快捷键说明

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