package com.bjsxt.tank; import java.awt.Color; import java.awt.Frame; import java.awt.Graphics; import java.awt.Image; import java.awt.event.*; import java.util.List; import java.util.ArrayList; public class TankClient extends Frame{ public static final int GAME_WIDTH = 800; public static final int GAME_HEIGHT = 800; Tank myTank = new Tank(50, 50, true, Tank.Direction.STOP, this); //myTank是自己的坦克 List<Tank> tanks = new ArrayList<Tank>(); //tanks是敌人的坦克 List<Explode> explodes = new ArrayList<Explode>(); //此为爆炸对象 Wall w1 = new Wall(100, 200, 20, 150, this), w2 = new Wall(300, 100, 300, 20, this); List<Missile> missiles = new ArrayList<Missile>(); Blood b = new Blood(); Image offScreenImage = null; 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); g.drawString("tanks life:" + myTank.getLife(), 10, 110); if(tanks.size() <= 0) { //当坦克全部被消灭后自动增加 for(int i=0; i<=5; i++) { tanks.add(new Tank(50 + 40*(i + 1), 50, false, Tank.Direction.D, this)); } } for(int i =0; i<missiles.size(); i++) { Missile m = missiles.get(i); m.hitTanks(tanks); m.hitTank(myTank); m.hitWall(w1); m.hitWall(w2); 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.collidesWithWall(w1); t.collidesWithWall(w2); t.collidesWithTanks(tanks); t.draw(g); } myTank.draw(g); myTank.eat(b); w1.draw(g); w2.draw(g); b.draw(g); } public void update(Graphics g) { if(offScreenImage == null) { offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT); } Graphics gOffScreen = offScreenImage.getGraphics(); Color c = gOffScreen.getColor(); gOffScreen.setColor(Color.GREEN); gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT); gOffScreen.setColor(c); paint(gOffScreen); g.drawImage(offScreenImage, 0, 0, null); } public void lauchFrame() { for(int i=0; i<10; i++) { tanks.add(new Tank(50 + 40*(i + 1), 50, false, Tank.Direction.D, this)); } this.setLocation(400, 300); this.setSize(GAME_WIDTH, GAME_HEIGHT); this.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { //添加窗口监听器 System.exit(0); } }); this.setResizable(false); //使得窗口不能随意改变 this.setBackground(Color.GREEN); this.addKeyListener(new KeyMonitor()); this.setVisible(true); new Thread(new PaintThread()).start(); //开启一个线程 } public static void main(String[] args) { TankClient tc = new TankClient(); tc.lauchFrame(); } private class PaintThread implements Runnable { //线程类,因为实现了Runnable接口 public void run() { while(true) { repaint(); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } } } private class KeyMonitor extends KeyAdapter { public void keyPressed(KeyEvent e) { myTank.keyPressed(e); } public void keyReleased(KeyEvent e) { myTank.keyReleased(e); } } }