Tank Game V0.2

/**
 * 功能:坦克游戏
 * 1.画出坦克.
 * 2.我的坦克可以上下左右移动
 * 3.可以发射子弹,子弹连发
 * 4.当我的坦克击中敌人坦克时,敌人就消失(爆炸的效果)
 * 5.我被击中后,显示爆炸效果
 * 6.防止敌人坦克重叠运动

*/

package cn.demo;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

class MyPanel extends JPanel implements KeyListener, Runnable {
	Tank tank = null;

	Vector<Bomb> bombs = new Vector<Bomb>();

	Image image1 = null;
	Image image2 = null;
	Image image3 = null;

	Vector<EnemyTank> ets = new Vector<EnemyTank>();

	public MyPanel(Tank tank) {
		this.tank = tank;
		ets.clear();
		int random = (int) (Math.random() * 10 % 4);
		EnemyTank tank1 = new EnemyTank(0, 0);
		
		tank1.setColor(Color.GREEN);
		tank1.setDirection(random);
		tank1.setSpeed(1);

		random = (int) (Math.random() * 10 % 4);
		EnemyTank tank2 = new EnemyTank(70, 0);
		
		tank2.setColor(Color.GREEN);
		tank2.setDirection(random);
		tank2.setSpeed(1);

		random = (int) (Math.random() * 10 % 4);
		EnemyTank tank3 = new EnemyTank(140, 0);
		
		tank3.setColor(Color.GREEN);
		tank3.setDirection(random);
		tank3.setSpeed(1);

		ets.add(tank1);
		ets.add(tank2);
		ets.add(tank3);

		tank1.setEts(ets);
		tank2.setEts(ets);
		tank3.setEts(ets);
		
		Thread t1 = new Thread(tank1);
		t1.start();
		Thread t2 = new Thread(tank2);
		t2.start();
		Thread t3 = new Thread(tank3);
		t3.start();

		try {
			image1 = ImageIO.read(new File("bomb_1.gif"));
			image2 = ImageIO.read(new File("bomb_2.gif"));
			image3 = ImageIO.read(new File("bomb_3.gif"));
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
	}

	public void paint(Graphics g) {
		super.paint(g);

		// draw hero
		drawTank(tank, g, tank.getDirection());

		// draw emery tank
		for (int i = 0; i < ets.size(); i++) {
			Tank tank = ets.get(i);
			if (tank != null && tank.isLive) {
				drawTank(tank, g, tank.getDirection());
			}
			if (!tank.isLive) {
				ets.remove(tank);
			}
		}

		// draw bullet
		Vector<Bullet> bulletList = tank.getBulletList();
		for (int i = 0; i < bulletList.size(); i++) {
			Bullet bullet = bulletList.get(i);
			if (bullet != null && bullet.islive) {
				drawBullet(g, tank);
			}
			if (!bullet.islive) {
				bulletList.remove(bullet);
			}

			for (int j = 0; j < ets.size(); j++) {
				Tank tank = ets.get(j);
				if (tank != null && tank.isLive) {
					hitEmery(bullet, tank);
				}
			}
		}

		// draw bomb
		for (int i = 0; i < bombs.size(); i++) {
			Bomb b = bombs.get(i);
			if (b != null && b.life > 6) {
				g.drawImage(image1, b.x, b.y, 30, 30, this);
			} else if (b != null && b.life > 3) {
				g.drawImage(image2, b.x, b.y, 30, 30, this);
			} else {
				g.drawImage(image3, b.x, b.y, 30, 30, this);
			}
			b.decreaseLife();

			if (!b.isLive) {
				bombs.remove(b);
			}
		}

	}

	public void drawBullet(Graphics g, Tank tank) {
		g.setColor(Color.BLACK);
		Vector<Bullet> bulletList = tank.getBulletList();
		for (int i = 0; i < bulletList.size(); i++) {
			Bullet bullet = bulletList.get(i);
			if (bullet.islive) {
				g.fill3DRect(bulletList.get(i).getX(), bulletList.get(i).getY(), 2, 2, false);
			}

		}
	}

	public void drawTank(Tank tank, Graphics g, int direction) {
		g.setColor(tank.getColor());
		switch (direction) {
		case 0:
			g.fill3DRect(tank.getX(), tank.getY(), 5, 30, false);
			g.fill3DRect(tank.getX() + 5, tank.getY() + 5, 20, 20, false);
			g.fill3DRect(tank.getX() + 25, tank.getY(), 5, 30, false);
			g.drawOval(tank.getX() + 10, tank.getY() + 10, 10, 10);
			g.drawLine(tank.getX() + 15, tank.getY() - 5, tank.getX() + 15, tank.getY() + 15);
			break;
		case 1:
			g.fill3DRect(tank.getX(), tank.getY(), 5, 30, false);
			g.fill3DRect(tank.getX() + 5, tank.getY() + 5, 20, 20, false);
			g.fill3DRect(tank.getX() + 25, tank.getY(), 5, 30, false);
			g.drawOval(tank.getX() + 10, tank.getY() + 10, 10, 10);
			g.drawLine(tank.getX() + 15, tank.getY() + 15, tank.getX() + 15, tank.getY() + 35);
			break;
		case 2:
			g.fill3DRect(tank.getX(), tank.getY(), 30, 5, false);
			g.fill3DRect(tank.getX() + 5, tank.getY() + 5, 20, 20, false);
			g.fill3DRect(tank.getX(), tank.getY() + 25, 30, 5, false);
			g.drawOval(tank.getX() + 10, tank.getY() + 10, 10, 10);
			g.drawLine(tank.getX() - 5, tank.getY() + 15, tank.getX() + 15, tank.getY() + 15);
			break;
		case 3:
			g.fill3DRect(tank.getX(), tank.getY(), 30, 5, false);
			g.fill3DRect(tank.getX() + 5, tank.getY() + 5, 20, 20, false);
			g.fill3DRect(tank.getX(), tank.getY() + 25, 30, 5, false);
			g.drawOval(tank.getX() + 10, tank.getY() + 10, 10, 10);
			g.drawLine(tank.getX() + 15, tank.getY() + 15, tank.getX() + 35, tank.getY() + 15);
			break;
		}

	}

	public void hitEmery(Bullet bullet, Tank t) {

		if (bullet.getX() >= t.getX() && bullet.getX() <= t.getX() + 30 && bullet.getY() >= t.getY()
				&& bullet.getY() <= t.getY() + 30) {
			t.isLive = false;
			bullet.islive = false;
			// create bomb
			Bomb b = new Bomb(t.getX(), t.getY());
			bombs.add(b);
		}

	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		if (e.getKeyCode() == KeyEvent.VK_A) {
			tank.setDirection(Direction.LEFT);
			tank.MoveLeft();
		} else if (e.getKeyCode() == KeyEvent.VK_D) {
			tank.setDirection(Direction.RIGHT);
			tank.MoveRight();
		} else if (e.getKeyCode() == KeyEvent.VK_W) {
			tank.setDirection(Direction.UP);
			tank.MoveUp();
		} else if (e.getKeyCode() == KeyEvent.VK_S) {
			tank.setDirection(Direction.DOWN);
			tank.MoveDown();
		}

		if (e.getKeyCode() == KeyEvent.VK_J) {
			tank.shotEmery();
		}
		this.repaint();

	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (true) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			this.repaint();
		}
	}
}

class Direction {
	public final static int UP = 0;
	public final static int DOWN = 1;
	public final static int LEFT = 2;
	public final static int RIGHT = 3;
}

class ClientWindow {
	public final static int WIDTH = 300;
	public final static int HEIGHT = 400;
}

class MyTestJFrame extends JFrame {
	MyPanel panel = null;

	MyTestJFrame(Tank tank) {
		panel = new MyPanel(tank);
		Thread thread = new Thread(panel);
		thread.start();
		this.addKeyListener(panel);

		this.add(panel);
		this.setSize(ClientWindow.WIDTH, ClientWindow.HEIGHT);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		this.setLocation(100, 200);
	}
}

public class TestMain {

	public static void main(String[] str) {
		Hero hero = new Hero(150, 300);
		hero.setSpeed(5);
		hero.setColor(Color.RED);
		MyTestJFrame frame = new MyTestJFrame(hero);

	}

}

package cn.demo;

import java.awt.Color;
import java.util.Vector;

class Tank {
	int x;
	int y;
	int speed;
	int direction;
	Color color;

	boolean isLive = true;
	private Vector<Bullet> bulletList = new Vector<Bullet>();

	public Vector<Bullet> getBulletList() {
		return bulletList;
	}

	public Color getColor() {
		return color;
	}

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

	public int getDirection() {
		return direction;
	}

	public void setDirection(int direction) {
		this.direction = direction;
	}

	public int getSpeed() {
		return speed;
	}

	public void setSpeed(int speed) {
		this.speed = speed;
	}

	public Tank(int x, int y) {
		super();
		this.x = x;
		this.y = y;
	}

	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 void MoveLeft() {
		this.x -= speed;
	}

	public void MoveRight() {
		this.x += speed;
	}

	public void MoveUp() {
		this.y -= speed;
	}

	public void MoveDown() {
		this.y += speed;
	}

	public void shotEmery() {
		Bullet bullet = null;
		if (direction == Direction.LEFT) {
			bullet = new Bullet(x - 5, y + 15, Direction.LEFT);
		} else if (direction == Direction.RIGHT) {
			bullet = new Bullet(x + 35, y + 15, Direction.RIGHT);
		} else if (direction == Direction.UP) {
			bullet = new Bullet(x + 15, y - 5, Direction.UP);
		} else if (direction == Direction.DOWN) {
			bullet = new Bullet(x + 15, y + 35, Direction.DOWN);
		}
		bullet.setSpeed(2);
		bulletList.add(bullet);

		Thread thread = new Thread(bullet);
		thread.start();

	}
}

class Bullet implements Runnable {

	@Override
	public String toString() {
		return "Bullet [x=" + x + ", y=" + y + ", speed=" + speed + ", direction=" + direction + ", islive=" + islive
				+ "]";
	}

	private int x;
	private int y;
	private int speed;
	private int direction;

	boolean islive = true;

	public Bullet(int x, int y, int direction) {
		super();
		this.x = x;
		this.y = y;
		this.direction = direction;
	}

	public int getDirection() {
		return direction;
	}

	public void setDirection(int direction) {
		this.direction = direction;
	}

	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 int getSpeed() {
		return speed;
	}

	public void setSpeed(int speed) {
		this.speed = speed;
	}

	public void MoveLeft() {
		this.x -= speed;
	}

	public void MoveRight() {
		this.x += speed;
	}

	public void MoveUp() {
		this.y -= speed;
	}

	public void MoveDown() {
		this.y += speed;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (true) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (direction == Direction.UP) {
				this.MoveUp();
			} else if (direction == Direction.LEFT) {
				this.MoveLeft();
			} else if (direction == Direction.RIGHT) {
				this.MoveRight();
			} else if (direction == Direction.DOWN) {
				this.MoveDown();
			}
			if (this.x <= 0 || this.y <= 0 || this.x >= ClientWindow.WIDTH || this.y >= ClientWindow.HEIGHT) {
				islive = false;
				break;
			}

		}

	}
}

class Hero extends Tank {

	public Hero(int x, int y) {
		super(x, y);
	}
}

class EnemyTank extends Tank implements Runnable {

	Vector<EnemyTank> ets = new Vector<EnemyTank>();

	public void setEts(Vector<EnemyTank> ets1) {
		this.ets = ets1;
	}

	public EnemyTank(int x, int y) {
		super(x, y);
	}

	public boolean isTouchOtherEnemy() {
		boolean b = false;

		switch (this.direction) {
		case 0:// UP
			for (int i = 0; i < ets.size(); i++) {
				EnemyTank et = ets.get(i);
				if (et != this) {
					if (et.direction == 0 || et.direction == 1) {
						if (this.x >= et.x && this.x <= et.x + 30 && this.y >= et.y && this.y <= et.y + 30) {
							System.out.println(1);
							return true;
						}
						if (this.x + 30 >= et.x && this.x + 30 <= et.x + 30 && this.y >= et.y && this.y <= et.y + 30) {
							System.out.println(2);
							return true;
						}
					}
					if (et.direction == 2 || et.direction == 3) {
						if (this.x >= et.x && this.x <= et.x + 30 && this.y >= et.y && this.y <= et.y + 30) {
							System.out.println(3);
							return true;
						}
						if (this.x + 30 >= et.x && this.x + 30 <= et.x + 30 && this.y >= et.y && this.y <= et.y + 30) {
							System.out.println(4);
							return true;
						}
					}
				}
			}

			break;
		case 1:// DOWN
			for (int i = 0; i < ets.size(); i++) {
				EnemyTank et = ets.get(i);
				if (et != this) {
					if (et.direction == 0 || et.direction == 1) {
						if (this.x >= et.x && this.x <= et.x + 30 && this.y + 30 >= et.y && this.y + 30 <= et.y + 30) {
							System.out.println(5);
							return true;
						}
						if (this.x + 30 >= et.x && this.x + 30 <= et.x + 30 && this.y + 30 >= et.y
								&& this.y + 30 <= et.y + 30) {
							System.out.println(6);
							return true;
						}
					}
					if (et.direction == 2 || et.direction == 3) {
						if (this.x >= et.x && this.x <= et.x + 30 && this.y + 30 >= et.y && this.y + 30 <= et.y + 30) {
							System.out.println(7);
							return true;
						}

						if (this.x + 30 >= et.x && this.x + 30 <= et.x + 30 && this.y + 30 >= et.y
								&& this.y + 30 <= et.y + 30) {
							System.out.println(8);
							return true;
						}
					}
				}
			}

			break;
		case 2:// LEFT
			for (int i = 0; i < ets.size(); i++) {
				EnemyTank et = ets.get(i);
				if (et != this) {
					if (et.direction == 0 || et.direction == 1) {
						if (this.x >= et.x && this.x <= et.x + 30 && this.y >= et.y && this.y <= et.y + 30) {
							System.out.println(9);
							return true;
						}
						if (this.x >= et.x && this.x <= et.x + 30 && this.y + 30 >= et.y && this.y + 30 <= et.y + 30) {
							System.out.println(10);
							return true;
						}
					}
					if (et.direction == 2 || et.direction == 3) {
						if (this.x >= et.x && this.x <= et.x + 30 && this.y >= et.y && this.y <= et.y + 30) {
							System.out.println(11);
							return true;
						}
						if (this.x >= et.x && this.x <= et.x + 30 && this.y + 30 >= et.y && this.y + 30 <= et.y + 30) {
							System.out.println(12);
							return true;
						}
					}
				}
			}

			break;
		case 3:// RIGHT
			for (int i = 0; i < ets.size(); i++) {
				EnemyTank et = ets.get(i);
				if (et != this) {
					if (et.direction == 0 || et.direction == 1) {
						if (this.x + 30 >= et.x && this.x + 30 <= et.x + 30 && this.y >= et.y && this.y <= et.y + 30) {
							System.out.println(13);
							return true;
						}
						if (this.x + 30 >= et.x && this.x + 30 <= et.x + 30 && this.y + 30 >= et.y
								&& this.y + 30 <= et.y + 30) {
							System.out.println(14);
							return true;
						}
					}
					if (et.direction == 2 || et.direction == 3) {
						if (this.x + 30 >= et.x && this.x + 30 <= et.x + 30 && this.y >= et.y && this.y <= et.y + 30) {
							System.out.println(15);
							return true;
						}
						if (this.x + 30 >= et.x && this.x + 30 <= et.x + 30 && this.y + 30 >= et.y
								&& this.y + 30 <= et.y + 30) {
							System.out.println(16);
							return true;
						}
					}
				}
			}

			break;
		}

		return b;
	}

	public void run() {
		// TODO Auto-generated method stub

		while (true) {

			switch (this.direction) {
			case 0:// UP
				for (int i = 0; i < 30; i++) {
					if (y > 0 && !this.isTouchOtherEnemy()) {
						y -= speed;
					}
					try {
						Thread.sleep(50);
					} catch (Exception e) {
						e.printStackTrace();
						// TODO: handle exception
					}
				}
				break;
			case 1:// DOWN
				for (int i = 0; i < 30; i++) {
					// minus tank.size
					if (y < (ClientWindow.HEIGHT - 30) && !this.isTouchOtherEnemy()) {
						y += speed;
					}
					try {
						Thread.sleep(50);
					} catch (Exception e) {
						e.printStackTrace();
						// TODO: handle exception
					}
				}
				break;
			case 2:// LEFT
				for (int i = 0; i < 30; i++) {
					if (x > 0 && !this.isTouchOtherEnemy()) {
						x -= speed;
					}
					try {
						Thread.sleep(50);
					} catch (Exception e) {
						e.printStackTrace();
						// TODO: handle exception
					}
				}
				break;
			case 3:// RIGHT
				for (int i = 0; i < 30; i++) {
					// minus tank.size
					if (x < (ClientWindow.WIDTH - 30) && !this.isTouchOtherEnemy()) {
						x += speed;
					}
					try {
						Thread.sleep(50);
					} catch (Exception e) {
						e.printStackTrace();
						// TODO: handle exception
					}
				}
				break;

			}

			this.direction = (int) (Math.random() * 4);

			if (this.isLive == false) {
				break;
			}

		}

	}
}

class Bomb {
	int x;
	int y;

	int life = 9;

	boolean isLive = true;

	public Bomb(int x, int y) {
		this.x = x;
		this.y = y;
	}

	public void decreaseLife() {
		if (life > 0) {
			life--;
		} else {
			isLive = false;
		}
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值