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

📄 tank.java

📁 基于尚学堂坦克大战的基础上开发,支持多人对战,在线聊天,坦克大战网络版.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

		if (olddir != dir) {
			Msg msg = new TankMoveMsg(this);
			tc.client.send(msg);
			System.out.println("发出一个移动包!");
		}
	}
	
//	 键盘弹起时,初始化bL,bU,bR,bD四个键的值
	public void mouseEvent(MouseEvent e) {
		fire();
		locateDirection();
	}

	// 键盘弹起时,初始化bL,bU,bR,bD四个键的值
	public void rollOutEvent(KeyEvent e) {
		int key = e.getKeyCode();
		switch (key) {
		case KeyEvent.VK_F2:
			this.live = true;
			blood = 100;
			break;
		case KeyEvent.VK_SHIFT:
			superFire();
			break;
		case KeyEvent.VK_CONTROL:
			fire();
			break;
		case KeyEvent.VK_LEFT:
			bL = false;
			break;
		case KeyEvent.VK_UP:
			bU = false;
			break;
		case KeyEvent.VK_RIGHT:
			bR = false;
			break;
		case KeyEvent.VK_DOWN:
			bD = false;
			break;
		}
		locateDirection();
	}

	// 超级炮弹
	private void superFire() {
		// 控制发子弹的速度
		if (controlMissileSpeed == 0) {
			Direction[] dirs = Direction.values();
			for (int i = 0; i < 8; i++) {
				fire(dirs[i]);
			}
			controlMissileSpeed = 5;
		}
	}

	public Missile fire(Direction dir) {
		Missile m = null;
		if (!live)
			return null;
		int x = this.x + Tank.WIDTH / 2 - Missile.WIDTH / 2;
		int y = this.y + Tank.HEIGHT / 2 - Missile.HEIGHT / 2;
		m = new Missile(tankId, missileId++, x, y, dir, good, auto, this.tc);
		tc.ms.add(m);
		Msg msg = new TankFireMsg(m);
		tc.client.send(msg);
		return m;
	}

	// 从坦克的中心开火,产生一个子弹对象,并添加到子弹的数组中
	public Missile fire() {

		Missile m = null;
		if (!live)
			return null;

		// 控制发子弹的速度
		if (controlMissileSpeed == 0) {
			int x = this.x + Tank.WIDTH / 2 - Missile.WIDTH / 2;
			int y = this.y + Tank.HEIGHT / 2 - Missile.HEIGHT / 2;
			m = new Missile(tankId, missileId++, x, y, ptDir, good, auto,
					this.tc);
			tc.ms.add(m);
			Msg msg = new TankFireMsg(m);
			tc.client.send(msg);
			controlMissileSpeed = 15;
		}
		return m;
	}

	public void stay() {
		x = oldX;
		y = oldY;
	}

	public boolean hitTanks(java.util.List<Tank> tanks) {
		for (int i = 0; i < tanks.size(); i++) {
			Tank t = tanks.get(i);
			if (this != t) {
				if (this.live && t.isLive()
						&& this.getRect().intersects(t.getRect())) {
					this.stay();
					t.stay();
					return true;
				}
			}
		}
		return false;
	}

	public boolean hitItem(ArrayList items) {
		for (int i = 0; i < items.size(); i++) {
			Item item = (Item) items.get(i);
			if (this.live && this.getRect().intersects(item.getRect())) {
				switch(item.style){
				case 0: if(this.blood<100)this.blood+=20; if(this.blood>100) this.blood=100; item.live=false; break;
				}
				
				Msg msg= new RemoveItemMsg(tc,item);
				tc.client.send(msg);
				return true;
			}
		}
		return false;
	}
	
	public boolean hitWalls(ArrayList walls) {
		for (int i = 0; i < walls.size(); i++) {
			Wall wall = (Wall) walls.get(i);
			if (this.live && this.getRect().intersects(wall.getRect())&&!this.isAirplane) {
				stay();
				return true;
			}
		}
		return false;
	}
	
	public void gameWinOrLose(){
		int goodcount = 0;
		int badcount = 0;
		// 判断当前活着的坦克有多少辆,如果只有一辆,则返回加入窗口
		if (tc.tank.isLive() && tc.tank.good) {
			goodcount++;
		} else if (tc.tank.isLive() && !tc.tank.good) {
			badcount++;
		}

		for (int i = 0; i < tc.playerTanks.size(); i++) {
			Tank pt = tc.playerTanks.get(i);
			if (pt.isLive() && pt.good) {
				goodcount++;
			} else if (pt.isLive() && !pt.good) {
				badcount++;
			}
		}

		// 判断其中有一方被全部歼灭,则退出游戏
		if (tc.isConnect() && (goodcount < 1 || badcount < 1)) {

			/*
			 * System.out.println("坦克生死!!"); if (tc.tank.isLive()) {
			 * tc.setWin(1); System.out.println("坦克生!!"); } else {
			 * tc.setWin(2); System.out.println("坦克死!!"); }
			 */
			try {
				Thread.sleep(3000);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			// 游戏结束,返回等待其他玩家加入的界面
			tc.tank.blood = 100;
			tc.tank.setLive(true);
			tc.gameOver = true;
			Msg gameOverMsg = new GameOverMsg(tc);
			tc.client.send(gameOverMsg);

		} else if (!tc.isConnect()) {
			// 判断没有玩家的时候的动作
			if (!tc.tank.isLive()) {
				tc.setWin(2);
				int x = javax.swing.JOptionPane.showConfirmDialog(null,
				"要重新开始吗?>>");
				if(x==0){
					tc.setWin(0);
					tc.tank.dir=Direction.STOP;
					tc.tank.bL = false;
					tc.tank.bU = false; 
					tc.tank.bR = false;
					tc.tank.bD = false;
					tc.items.removeAll(tc.items);
					tc.ms.removeAll(tc.ms);
					int badCount = 0;
					for (int i = 0; i < tc.badTanks.size(); i++) {
						Tank badTank = tc.badTanks.get(i);
						if (badTank.isLive()) {
							badCount++;
						}
					}
					
					tc.tank.x=(int)(Math.random()*800);
					tc.tank.y=(int)(Math.random()*600);
					
					tc.tank.blood=100;
					tc.tank.setLive(true);
					tc.createSingleTankandWall();
				}else{
					System.exit(0);
				}
			} else {
				int badCount = 0;
				for (int i = 0; i < tc.badTanks.size(); i++) {
					Tank badTank = tc.badTanks.get(i);
					if (badTank.isLive()) {
						badCount++;
					}
				}
				if (badCount == 0) {
					tc.setWin(1);
				}
				if (tc.getWin() == 1) {
					tc.tank.dir=Direction.STOP;
					tc.tank.bL = false;
					tc.tank.bU = false; 
					tc.tank.bR = false;
					tc.tank.bD = false;
					int x = javax.swing.JOptionPane.showConfirmDialog(null,
							"进入下一关>>");
					if (x == 0) {
						try {
							Thread.sleep(3000);
						} catch (InterruptedException e1) {
							e1.printStackTrace();
						}
						tc.setWin(0);
						tc.items.removeAll(tc.items);
						tc.ms.removeAll(tc.ms);
						for (int i = 0; i < tc.badTanks.size(); i++) {
							Tank badTank = tc.badTanks.get(i);
							if (badTank.isLive()) {
								badCount++;
							}
						}
						tc.tank.x=(int)(Math.random()*800);
						tc.tank.y=(int)(Math.random()*600);
						
						tc.createSingleTankandWall();
					}
				}
			}
			/*
			 * if (tc.tank.isLive()) { tc.setWin(1); } else {
			 *  }
			 */
		}
	}

	// 得到一个矩形的热点区域,用来判断是否碰撞
	public Rectangle getRect() {
		return new Rectangle(x, y, WIDTH, HEIGHT);
	}

	public boolean isLive() {
		return live;
	}

	public void setLive(boolean live) {
		this.live = live;
	}

	public boolean isGood() {
		return good;
	}

	public void setGood(boolean good) {
		this.good = good;
	}

	public void pressKeyEvnet(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	public int getTankId() {
		return tankId;
	}

	public void setTankId(int tankId) {
		this.tankId = tankId;
	}

	public boolean isAuto() {
		return auto;
	}

	public void setAuto(boolean auto) {
		this.auto = auto;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getBadTankColor() {
		return badTankColor;
	}

	public void setBadTankColor(String badTankColor) {
		this.badTankColor = badTankColor;
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}

	public boolean isReady() {
		return ready;
	}

	public void setReady(boolean ready) {
		this.ready = ready;
	}


	/*
	 * public boolean hitTank(Tank t){
	 * 
	 * if(Math.abs(x-t.x)<Tank.WIDTH && Math.abs(y-t.y)<Tank.HEIGHT){ return
	 * true; } return false; }
	 */
}

⌨️ 快捷键说明

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