当前位置: 首页 > 工具软件 > Tank Game > 使用案例 >

tank game V0.1

丘向荣
2023-12-01

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

*/

package cn.demo;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
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);
		System.out.println(random);
		EnemyTank tank1 = new EnemyTank(0, 0);
		Thread t1 = new Thread(tank1);
		t1.start();
		tank1.setColor(Color.GREEN);
		tank1.setDirection(random);
		tank1.setSpeed(2);

		random = (int) (Math.random() * 10 % 4);
		System.out.println(random);
		EnemyTank tank2 = new EnemyTank(70, 0);
		Thread t2 = new Thread(tank2);
		t2.start();
		tank2.setColor(Color.GREEN);
		tank2.setDirection(random);
		tank2.setSpeed(2);

		random = (int) (Math.random() * 10 % 4);
		System.out.println(random);
		EnemyTank tank3 = new EnemyTank(140, 0);
		Thread t3 = new Thread(tank3);
		t3.start();
		tank3.setColor(Color.GREEN);
		tank3.setDirection(random);
		tank3.setSpeed(2);

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

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

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 {

	int bulletNumber;

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

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

}

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

	}

}


 类似资料:

相关阅读

相关文章

相关问答