TankWar1.9

package com.diaodiao;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

public class TankClient extends Frame {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final int GAME_WIDTH = 800;
	public static final int GAME_HEIGHT = 600;
	
	private Image offScreenImage = null;
	Tank myTank = new Tank(50, 50, this, true, Tank.Direction.STOP);
	private List<Missile> missiles = new ArrayList<Missile>();
	private List<Tank> tanks = new ArrayList<Tank>();
	private List<Explode> explodes = new ArrayList<Explode>();
	
	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);
		for(int i = 0; i < missiles.size(); i++) {
			Missile m = missiles.get(i);
			m.hitTanks(tanks);
			if(myTank != null) {
				m.hitTank(myTank);
			}
			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 enemyTank = tanks.get(i);
			enemyTank.draw(g);
		}
		
		if(myTank != null) {
			myTank.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, this, false, Tank.Direction.D));
		}
		
		this.setLocation(300, 100);
		this.setSize(GAME_WIDTH, GAME_HEIGHT);
		this.setTitle("TankWar");
		this.addWindowListener(new WindowAdapter(){

			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
			
		});
		this.addKeyListener(new KeyMonitor());
		this.setBackground(Color.GREEN);
		this.setResizable(false);
		this.setVisible(true);
		new Thread(new paintThread()).start();
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		TankClient tc = new TankClient();
		tc.lauchFrame();
	}
	
	private class paintThread implements Runnable {

		@Override
		public void run() {
			while(true) {
				repaint();
				try {
					Thread.sleep(50);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
	
	private class KeyMonitor extends KeyAdapter{

		@Override
		public void keyPressed(KeyEvent e) {
			if(myTank != null)
				myTank.keyPressed(e);
		}

		@Override
		public void keyReleased(KeyEvent e) {
			if(myTank != null)
				myTank.keyReleased(e);
		}
		
	}

	public List<Missile> getMissiles() {
		return missiles;
	}

	public void setMissiles(List<Missile> missiles) {
		this.missiles = missiles;
	}

	public List<Tank> getTanks() {
		return tanks;
	}

	public void setTanks(List<Tank> tanks) {
		this.tanks = tanks;
	}

	public List<Explode> getExplodes() {
		return explodes;
	}

	public void setExplodes(List<Explode> explodes) {
		this.explodes = explodes;
	}

}



package com.diaodiao;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.Random;

public class Tank {

	public static final int WIDTH = 30;
	public static final int HEIGHT = 30;
	
	public static final int XSPEED = 5;
	public static final int YSPEED = 5;
	
	TankClient tc;
	private int x, y;
	private static Random r = new Random();
	private boolean bL = false, bU = false, bR = false, bD = false;
	private boolean good;
	private boolean live = true;
	enum Direction {L, LU, U, RU, R, RD, D, LD, STOP};
	private int step = r.nextInt(12) + 3;
	
	private Direction dir = Direction.STOP;
	private Direction ptDir = Direction.D;
	
	public Tank(int x, int y, TankClient tc, boolean good, Direction dir) {
		this.tc = tc;
		this.x = x;
		this.y = y;
		this.good = good;
		this.dir = dir;
	}
	
	public void draw(Graphics g) {
		
		if(!live) {
			if(!good)
				tc.getTanks().remove(this);
			else 
				tc.myTank = null;
			return ;
		}
		
		Color c = g.getColor();
		if(good)
			g.setColor(Color.RED);
		else 
			g.setColor(Color.BLUE);
		g.fillOval(x, y, WIDTH, HEIGHT);
		g.setColor(c);
		
		switch(ptDir) {
			case L:
				g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x, y + Tank.HEIGHT / 2);
				break;
			case U:
				g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x + Tank.WIDTH / 2, y);
				break;
			case R:
				g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x + Tank.WIDTH, y + Tank.HEIGHT / 2);
				break;
			case D:
				g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x + Tank.WIDTH / 2, y + Tank.HEIGHT);
				break;
			case LU:
				g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x, y);
				break;
			case RU:
				g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x + Tank.WIDTH, y);
				break;
			case RD:
				g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x + Tank.WIDTH, y + Tank.HEIGHT);
				break;
			case LD:
				g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x, y + Tank.HEIGHT);
				break;
		}
		
		move();
		
	}
	
	public void move() {
		switch(dir) {
			case L:
				x -= XSPEED;
				break;
			case U:
				y -= YSPEED;
				break;
			case R:
				x += XSPEED;
				break;
			case D:
				y += YSPEED;
				break;
			case LU:
				x -= XSPEED;
				y -= YSPEED;
				break;
			case RU:
				x += XSPEED;
				y -= YSPEED;
				break;
			case RD:
				x += XSPEED;
				y += YSPEED;
				break;
			case LD:
				x -= XSPEED;
				y += YSPEED;
				break;
			case STOP:
				break;
		}
		
		if(dir != Direction.STOP) {
			ptDir = dir;
		}
		
		if(x < 0) x = 0;
		if(y < 30) y = 30;
		if(x + Tank.WIDTH > TankClient.GAME_WIDTH) x = TankClient.GAME_WIDTH - Tank.WIDTH;
		if(y + Tank.HEIGHT > TankClient.GAME_HEIGHT) y = TankClient.GAME_HEIGHT - Tank.HEIGHT;
		
		if(!good) {
			Direction[] dirs = Direction.values();
			if(step == 0) {
				step = r.nextInt(12) + 3;
				int rn = r.nextInt(dirs.length);
				dir = dirs[rn];
			}			
			step --;
			
			if(r.nextInt(40) > 38) this.fire();
		}
	}
	
	public void keyPressed(KeyEvent e) {
		int key = e.getKeyCode();
		switch(key) {
			case KeyEvent.VK_LEFT:
				bL = true;
				break;
			case KeyEvent.VK_UP:
				bU = true;
				break;
			case KeyEvent.VK_RIGHT:
				bR = true;
				break;
			case KeyEvent.VK_DOWN:
				bD = true;
				break;
		}
		locateDirection();
	}

	
	public void keyReleased(KeyEvent e) {
		int key = e.getKeyCode();
		switch(key) {
			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();
	}
	
	void locateDirection() {
		if(bL && !bU && !bR && !bD) {
			dir = Direction.L;
		}else if(bL && bU && !bR && !bD) {
			dir = Direction.LU;
		}else if(!bL && bU && !bR && !bD) {
			dir = Direction.U;
		}else if(!bL && bU && bR && !bD) {
			dir = Direction.RU;
		}else if(!bL && !bU && bR && !bD) {
			dir = Direction.R;
		}else if(!bL && !bU && bR && bD) {
			dir = Direction.RD;
		}else if(!bL && !bU && !bR && bD) {
			dir = Direction.D;
		}else if(bL && !bU && !bR && bD) {
			dir = Direction.LD;
		}else if(!bL && !bU && !bR && !bD) {
			dir = Direction.STOP;
		}
	}
	
	public void fire() {
		if(!live) {
			return ;
		}
		int x = this.x + Tank.WIDTH/2 - Missile.WIDTH/2;
		int y = this.y + Tank.HEIGHT/2 - Missile.HEIGHT/2;
		Missile m = new Missile(x, y, ptDir, tc, good);
		tc.getMissiles().add(m);
	}
	
	public Rectangle getRect() {
		return new Rectangle(x, y, WIDTH, HEIGHT);
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	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;
	}
	
	
}


package com.diaodiao;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.List;

public class Missile {

	public static final int WIDTH = 10;
	public static final int HEIGHT = 10;
	
	public static final int XSPEED = 10;
	public static final int YSPEED = 10;
	
	private int x, y;
	private boolean good;
	private boolean live = true;
	TankClient tc;
	Tank.Direction dir;
	
	public Missile(int x, int y, Tank.Direction dir, TankClient tc, boolean good) {
		this.x = x;
		this.y = y;
		this.dir = dir;
		this.tc = tc;
		this.good = good;
	}
	
	public void draw(Graphics g) {
		
		if(!live) {
			tc.getMissiles().remove(this);
			return ;
		}
		
		Color c = g.getColor();
		if(good)
			g.setColor(Color.RED);
		else 
			g.setColor(Color.BLUE);
		g.fillOval(x, y, WIDTH, HEIGHT);
		g.setColor(c);
		move();
	}
	
	public void move() {
		switch(dir) {
			case L:
				x -= XSPEED;
				break;
			case U:
				y -= YSPEED;
				break;
			case R:
				x += XSPEED;
				break;
			case D:
				y += YSPEED;
				break;
			case LU:
				x -= XSPEED;
				y -= YSPEED;
				break;
			case RU:
				x += XSPEED;
				y -= YSPEED;
				break;
			case RD:
				x += XSPEED;
				y += YSPEED;
				break;
			case LD:
				x -= XSPEED;
				y += YSPEED;
				break;
		}
		
		if(x < 0 || y < 0 || x > TankClient.GAME_WIDTH || y > TankClient.GAME_HEIGHT) {
			live = false;
		}
	
	}
	
	public Rectangle getRect() {
		return new Rectangle(x, y, WIDTH, HEIGHT);
	}
	
	public boolean hitTank(Tank t) {
		if(this.getRect().intersects(t.getRect()) && this.good != t.isGood()&& t.isLive()) {			
			t.setLive(false);
			this.live = false;
			Explode e = new Explode(t.getX(), t.getY(), tc);
			tc.getExplodes().add(e);
			return true;
		}
		return false;
	}
	
	public boolean hitTanks(List<Tank> tanks) {
		
		for(int i = 0; i < tanks.size(); i++) {
			Tank t = tanks.get(i);
			if(this.hitTank(t)) {
				return true;
			}
		}
		
		return false;
	}

	public boolean isGood() {
		return good;
	}

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


package com.diaodiao;

import java.awt.*;

public class Explode {

	private TankClient tc;
	int[] diameter = { 4, 7, 12, 18, 26, 32, 49, 30, 14, 6 };
	int step = 0;
	int x, y;

	public Explode(int x, int y, TankClient tc) {
		this.x = x;
		this.y = y;
		this.tc = tc;
	}

	public void draw(Graphics g) {

		if (step == diameter.length) {
			tc.explodes.remove(this);
			return;
		}

		Color c = g.getColor();
		g.setColor(Color.ORANGE);
		g.fillOval(x, y, diameter[step], diameter[step]);
		g.setColor(c);

		step++;
	}
}

你可能感兴趣的:(java)