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

📄 enemytank.java

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

public class EnemyTank {
	private static Random r = new Random();
	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[] dirs = Direction.values();
	Direction dir = dirs[r.nextInt(dirs.length)];
	Direction ptdir = dir;
	TankClient tk;
	boolean live = true;
	int step = r.nextInt(17) + 3;
	int mstep = r.nextInt(12) + 3;
	
	public EnemyTank(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.BLUE);
		g.fillOval(x, y, 30, 30);
		g.setColor(Color.BLACK);
		ptdraw(g);
		g.setColor(c);
		move();
		//Missile m = new Missile (x, y, dir);
		//tk.missiles.add(m);
		step--;
		if(step == 0) {dir = dirs[r.nextInt(dirs.length)]; step = r.nextInt(17) + 3;}
	}
	
	public Rectangle getRect() {
		return new Rectangle(x, y, 30, 30);
	}
	
	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 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;
		}
	}
}

⌨️ 快捷键说明

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