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

📄 tankwarclient.java

📁 简单的坦克大战 可以打敌人 更多功能还在编写当中
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;

public class TankWarClient {

	public static void main(String[] args) {
		new TankFrame().launchFrame();
	}
}

class TankFrame extends Frame {
	
	public static final int GAME_WIDTH = 800;
	public static final int GAME_HEIGHT = 600;

	Tank tank = new Tank(400, 600, true, Tank.Direction.STOP, Tank.Direction.U, this);
	
	List<Explode> explodes = new ArrayList<Explode>();
	List<Missile> missiles = new ArrayList<Missile>();
	List<Tank> tanks = new ArrayList<Tank>();
	
	Wall wall1 = new Wall(100, 400, 300, 50, this);
	Wall wall2 = new Wall(500, 100, 100, 300, this);
	
	Image backScreen = null;
	
	public void launchFrame() {
		
		for(int i=0; i<10; i++) {
			tanks.add(new Tank(50+50*(i+1), 50, false, Tank.Direction.D, Tank.Direction.D, this));
		}
		
		setTitle("TankWar");
		setBounds(255,100,GAME_WIDTH,GAME_HEIGHT);
		setBackground(new Color(0,255,50));
		setResizable(false);
		
		MoveThread mt = new MoveThread();
		Thread t = new Thread(mt);
		t.start();
		
		addKeyListener(new KeyMonital());
		
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				setVisible(false);
				System.exit(0);
			}
		});
		
		setVisible(true);
	}
	
	public void paint(Graphics g) {
		g.drawString("Missiles count :" + missiles.size(), 10, 50);
		g.drawString("Explodes count :" + explodes.size(), 10, 70);
		g.drawString("Tanks    count :" + tanks.size(), 10, 90);
		
		tank.paint(g);
		wall1.draw(g);
		wall2.draw(g);
		
		for(int i=0; i<missiles.size(); i++) {
			Missile m = missiles.get(i);
			m.hitTank(tank);
			m.hitTanks(tanks);
			m.hitWall(wall1);
			m.hitWall(wall2);
			m.draw(g);
		}
		
		for(int i=0; i<explodes.size(); i++) {
			Explode e = explodes.get(i);
			e.draw(g);
		}
	
		for(int i=0; i<tanks.size(); i++) {
			Tank t = tanks.get(i);
			t.hitWall(wall1);
			t.hitWall(wall2);
			t.paint(g);
		}
		/*
		Iterator i = explodes.iterator();
		while(i.hasNext()) {
			Explode e = (Explode)i.next();
			e.draw(g);
		*/
	} 
	
	public void update(Graphics g) {
		if(backScreen == null) {
			backScreen = createImage(GAME_WIDTH,GAME_HEIGHT);
		}
		Graphics gBackScreen = backScreen.getGraphics();
		Color c = gBackScreen.getColor();
		gBackScreen.setColor(new Color(0,255,50));
		gBackScreen.fillRect(0, 0, GAME_WIDTH,GAME_HEIGHT);
		gBackScreen.setColor(c);
		paint(gBackScreen);
		g.drawImage(backScreen, 0, 0, null);
	}

	
	class MoveThread implements Runnable {

		public void run() {
			try {
				while(true) {
					repaint();
					Thread.sleep(50);
				}
			} catch(InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	class KeyMonital extends KeyAdapter {
	
		public void keyReleased(KeyEvent k) {
			tank.keyReleased(k);
			
		}

		public void keyPressed(KeyEvent k) {
			tank.keyPressed(k);
		}
		
	}
}

⌨️ 快捷键说明

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