java 坦克大战

本文介绍了一个基于Java的坦克大战游戏的设计与实现细节。游戏包括玩家控制的坦克、敌人坦克、子弹、炸弹等元素,实现了玩家与敌人坦克的交互、子弹碰撞检测等功能。文章详细展示了各个类的设计与功能实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mebers.java

package com.text3;

import java.io.*;
import java.util.Vector;

class Node{
	int x;
	int y;
	int direct;
	public Node(int x, int y, int direct){
		this.x = x;
		this.y = y; 
		this.direct = direct;
	}
}


//记录类,同时也可以保存玩家的设置
class Recorder{
	
	//记录没关有多少敌人
	private static int enNum = 20;
	//设置我有多少可以用的人
	private static int myLife = 3;
	//记录敌人的总数
	private static int allEnNum = 0;
	
	//从文件中恢复记录点
 	static Vector<Node> nodes = new Vector<>();
	
	//把玩家击毁敌人坦克数量保存到文件中
	
	private static FileWriter fw = null;
	private static BufferedWriter bw = null;
	
	private  static FileReader fr = null;
	private static BufferedReader  br = null;
	
	private   Vector<EnemyTank> ets = new Vector<>();
	
	//完成读取任务
	
	public  Vector<Node> getNodesAndEnNums(){
		try {
			fr = new FileReader("e:\\myRecording.txt");
			br = new BufferedReader(fr);
			String n = null;
			n = br.readLine();
			allEnNum = Integer.parseInt(n);
			while((n=br.readLine())!=null){
				String[] xyz = n.split(" ");
			
				Node node =  
						new Node(Integer.parseInt(xyz[0]),Integer.parseInt(xyz[1]),Integer.parseInt(xyz[2]));
				nodes.add(node);
			}
				
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				br.close();
				fr.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return nodes;
	}
	
	
	public  Vector<EnemyTank> getEts() {
		return ets;
	}

	public  void setEts(Vector<EnemyTank> ets) {
		this.ets = ets;
	}
	//保存击毁敌人的数量和和敌人坦克的坐标
	public  void keepRecAndEnemyYank(){
		try {
			fw = new FileWriter("e:\\myRecording.txt");
			bw = new BufferedWriter(fw);
			bw.write(allEnNum+"\r\n");
			
			//保存当前活的敌人坦克的坐标和方向
			for(int i=0; i<ets.size(); i++){
				//取出第一个坦克
				EnemyTank et = ets.get(i);
				if(et.isLive){
					//活的保存
					String recode = et.x+" "+et.y+" "+et.direct;
					
					//写入
					bw.write(recode+"\r\n");
					
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				bw.close();
				fw.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		
		
	}
	
	//读取记录
	public static void getRecoring(){
		try {
			fr = new FileReader("e:\\myRecording.txt");
			br = new BufferedReader(fr);
			String n = br.readLine();
			allEnNum = Integer.parseInt(n);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				br.close();
				fr.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}
	
	//记录
	public static void keepRecording(){
		try {
			fw = new FileWriter("e:\\myRecording.txt");
			bw = new BufferedWriter(fw);
			bw.write(allEnNum+"\r\n");
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				bw.close();
				fw.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}
	
	
	public static int getEnNum() {
		return enNum;
	}
	public static void setEnNum(int enNum) {
		Recorder.enNum = enNum;
	}
	public static int getMyLife() {
		return myLife;
	}
	public static void setMyLife(int myLife) {
		Recorder.myLife = myLife;
	}
	//减少敌人数目
	public static void reduceEnNum(){
		enNum--;
	}
	
	public static void addEnNum(){
		allEnNum++;
	}
	public static int getAllEnNum() {
		return allEnNum;
	}
	public static void setAllEnNum(int allEnNum) {
		Recorder.allEnNum = allEnNum;
	}

	
}


//炸弹类
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 lifeDown() {
		if (life > 0) {
			life--;
		} else {
			this.isLive = false;
		}
	}
}

// 子弹类
class Shot implements Runnable {
	int x;
	int y;
	int direct;
	int speed = 1;
	// 是否或者
	boolean isLive = true;

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

	@Override
	public void run() {
		while (true) {
			try {
				Thread.sleep(50);
			} catch (Exception e) {

			}
			switch (direct) {
			case 0:
				// 上
				y -= speed;
				break;
			case 1:
				// 右
				x += speed;
				break;
			case 2:
				// 下
				y += speed;
				break;
			case 3:
				// 左
				x -= speed;
				break;

			}

			// 判断该子弹是否碰到边缘
			if (x < 0 || x > 400 || y < 0 || y > 300) {
				this.isLive = false;
				break;
			}
		}
	}
}

// 坦克类
class TanK {
	// 表示坦克横坐标
	int x = 0;
	// 表示坦克的纵坐标
	int y = 0;
	boolean isLive = true;
	// 坦克方向
	// 0表示上 1表示 右 2 表示下 3表示左
	int direct = 0;
	// 速度
	int speed = 2;
	int color = 0;

	public int getColor() {
		return color;
	}

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

	public int getSpeed() {
		return speed;
	}

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

	public int getDirect() {
		return direct;
	}

	public void setDirect(int direct) {
		this.direct = direct;
	}

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

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

// 敌人坦克
class EnemyTank extends TanK implements Runnable {

	int times =0;
	
	//定义一个向量,可以访问到MyPanel上所有的敌人的坦克
	Vector<EnemyTank> ets = new Vector<>();
	//定义一个向量,可以存放敌人的子弹
	Vector<Shot> ss = new Vector<>();
	//敌人添加子弹,应当在刚刚创建坦克和敌人子弹消失
	EnemyTank(int x, int y) {
		super(x, y);
	}
	
	//得到MyPanel的敌人的坦克向量
	public void setEts(Vector<EnemyTank> vv){
		this.ets = vv;
	}
	
	//判断是否碰到了别的敌人的坦克
	public boolean isTouchOtherEnemy(){
		boolean b = false;
		
		switch(this.direct){
		case 0:
			//我的坦克向上
			//取出所以敌人坦克
			for(int i=0; i<ets.size();i++){
				//取出第一个坦克
				EnemyTank et = ets.get(i);
				//如果不是自己
				if(et!=this){
					//敌人的方向是向下或者向上
					if(et.direct==0 || et.direct==2){
						if(this.x>=et.x && this.x<=et.x+20 && this.y>=et.y && this.y<=et.y+30){
							return true;
						}
						if(this.x+20>=et.x&&this.x+20<=et.x+20&&this.y>=et.y&&this.y<=et.y+30){
							return true;
						}
					}
					if(et.direct==1 || et.direct==3){
						if(this.x>=et.x && this.x<=et.x+30 && this.y>=et.y && this.y<=et.y+20){
							return true;
						}
						if(this.x+20>=et.x&&this.x+20<=et.x+30&&this.y>=et.y&&this.y<=et.y+20){
							return true;
						}
					}
				}
			}
			break;
		case 1:
			//坦克向右
			for(int i=0; i<ets.size();i++){
				//取出第一个坦克
				EnemyTank et = ets.get(i);
				//如果不是自己
				if(et!=this){
					//敌人的方向是向下或者向上
					if(et.direct==0 || et.direct==2){
						if(this.x+30>=et.x && this.x+30<=et.x+20 && this.y>=et.y && this.y<=et.y+30){
							return true;
						}
						if(this.x+30>=et.x&&this.x+30<=et.x+20&&this.y+20>=et.y&&this.y+20<=et.y+30){
							return true;
						}
					}
					if(et.direct==1 || et.direct==3){
						if(this.x+30>=et.x && this.x+30<=et.x+30 && this.y+20>=et.y && this.y+20<=et.y+20){
							return true;
						}
						if(this.x+30>=et.x&&this.x+30<=et.x+30&&this.y>=et.y&&this.y<=et.y+20){
							return true;
						}
					}
				}
			}
			break;
		case 2:
			for(int i=0; i<ets.size();i++){
				//取出第一个坦克
				EnemyTank et = ets.get(i);
				//如果不是自己
				if(et!=this){
					//敌人的方向是向下或者向上
					if(et.direct==0 || et.direct==2){
						if(this.x>=et.x && this.x<=et.x+20 && this.y+30>=et.y && this.y+30<=et.y+30){
							return true;
						}
						if(this.x+20>=et.x&&this.x+20<=et.x+20&&this.y+30>=et.y&&this.y+30<=et.y+30){
							return true;
						}
					}
					if(et.direct==1 || et.direct==3){
						if(this.x>=et.x && this.x<=et.x+30 && this.y+30>=et.y && this.y+30<=et.y+20){
							return true;
						}
						if(this.x+20>=et.x&&this.x+20<=et.x+30&&this.y+30>=et.y&&this.y+30<=et.y+20){
							return true;
						}
					}
				}
			}
			break;
		case 3:
			//向左
			for(int i=0; i<ets.size();i++){
				//取出第一个坦克
				EnemyTank et = ets.get(i);
				//如果不是自己
				if(et!=this){
					//敌人的方向是向下或者向上
					if(et.direct==0 || et.direct==2){
						if(this.x>=et.x && this.x<=et.x+20 && this.y>=et.y && this.y<=et.y+30){
							return true;
						}
						if(this.x>=et.x&&this.x<=et.x+20&&this.y+20>=et.y&&this.y+20<=et.y+30){
							return true;
						}
					}
					if(et.direct==1 || et.direct==3){
						if(this.x>=et.x && this.x<=et.x+30 && this.y>=et.y && this.y<=et.y+20){
							return true;
						}
						if(this.x>=et.x&&this.x<=et.x+30&&this.y+20>=et.y&&this.y+20<=et.y+20){
							return true;
						}
					}
				}
			}
			break;
		}
		return b;
	}

	@Override
	public void run() {
		while (true) {

			switch (this.direct) {
			case 0:
				for (int i = 0; i < 30; i++) {
					if(y>0 && !this.isTouchOtherEnemy()){
						y -= speed;
					}
					try {
						Thread.sleep(50);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				break;
			case 1:
				for (int i = 0; i < 30; i++) {
					if(x<350 && !this.isTouchOtherEnemy()){
						x += speed;
					}
					try {
						Thread.sleep(50);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				break;
			case 2:
				for (int i = 0; i < 30; i++) {
					if(y<230 && !this.isTouchOtherEnemy()){
						y += speed;
					}
					try {
						Thread.sleep(50);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				break;
			case 3:
				for (int i = 0; i < 30; i++) {
					if(x>0 && !this.isTouchOtherEnemy()){
						x -= speed;
					}
					try {
						Thread.sleep(50);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				break;
			}
			this.times++;
			if(times%2==0){
				if(isLive){
					if(ss.size()<5){
						//判断是否需要给坦克加入新的子弹
							Shot s = null;
									//没有子弹
									//添加
									switch(direct){
									case 0:
										s = new Shot(x + 10, y, 0);
										ss.add(s);
										break;
									case 1:
										s = new Shot(x + 30, y + 10, 1);
										ss.add(s);
										break;
									case 2:
										s = new Shot(x + 10, y + 30, 2);
										ss.add(s);
										break;
									case 3:
										s = new Shot(x, y + 10, 3);
										ss.add(s);
										break;
									}
									Thread t = new Thread(s);
									t.start();
								}
						}
					}
			
			
			// 让坦克随机产生一个新的方向
			this.direct = (int) (Math.random() * 4);
			// 判断敌人是否死亡
			if (this.isLive == false) {
				// 让坦克死亡后,推出线程
				break;
			}
			
			
		}
	}

}

// 我的坦克
class Hero extends TanK {

	//子弹
	Shot s = null;
	Vector<Shot> ss = new Vector<>();

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

	// 开火
	public void shotEnemy() {

		switch (this.direct) {
		case 0:
			s = new Shot(x + 10, y, 0);
			ss.add(s);
			break;
		case 1:
			s = new Shot(x + 30, y + 10, 1);
			ss.add(s);
			break;
		case 2:
			s = new Shot(x + 10, y + 30, 2);
			ss.add(s);
			break;
		case 3:
			s = new Shot(x, y + 10, 3);
			ss.add(s);
			break;
		}
		Thread t = new Thread(s);
		t.start();
	}

	// 上移
	public void moveUp() {
		y -= speed;
	}

	// 右移
	public void moveRight() {
		x += speed;
	}

	// 下移
	public void moveDown() {
		y += speed;
	}

	// 左移
	public void moveLeft() {
		x -= speed;
	}

}
MyTankeGame3.java

package com.text3;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;

public class MyTankeGame3 extends JFrame implements ActionListener{

	MyPanel mp = null;
	MyStartPanel msp = null;
	JMenuBar jmb = null;
	JMenu jm1 = null;
	JMenuItem jmil = null;
	JMenuItem jmi2 = null;
	JMenuItem jmi3 = null;
	JMenuItem jmi4 = null;

	public static void main(String[] args) {
		MyTankeGame3 mtg = new MyTankeGame3();
	}

	MyTankeGame3() {
		// mp = new MyPanel();
		// Thread t = new Thread(mp);
		// t.start();
		// this.add(mp);
		// 监听
		// this.addKeyListener(mp);
		//创建菜单及菜单选项
		jmb = new JMenuBar();
		jm1 = new JMenu("游戏(G)");
		jm1.setMnemonic('G');
		jmil = new JMenuItem("开始游戏(N)");
		jmi2 = new JMenuItem("退出游戏(E)");
		jmi3 = new JMenuItem("存盘退出游戏(C)");
		jmi4 = new JMenuItem("继续上局游戏(S)");
		jmi4.addActionListener(this);
		jmi4.setActionCommand("conGame");
		jmi3.addActionListener(this);
		jmi3.setActionCommand("saveExit");
		jmi2.setMnemonic('E');
		jmil.addActionListener(this);
		jmil.setActionCommand("newgame");
		jmi2.addActionListener(this);
		jmi2.setActionCommand("exit");
		jm1.add(jmil);
		jm1.add(jmi2);
		jm1.add(jmi3);
		jm1.add(jmi4);
		jmb.add(jm1);
		msp = new MyStartPanel();
		new Thread(msp).start();
		this.setJMenuBar(jmb);
		this.add(msp);
		this.setSize(600, 500);
		this.addWindowListener(new WindowAdapter() {

			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(1);
			}

		});
		this.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("newgame")){
			mp = new MyPanel("newGame");
			Thread t = new Thread(mp);
			t.start();
			//先删除旧的面板
			this.remove(msp);
			this.add(mp);
			// 监听
			this.addKeyListener(mp);
			//显示
			this.setVisible(true);
		}else if(e.getActionCommand().equals("exit")){
			
			//保存击毁敌人数量
			 Recorder.keepRecording();
			//退出
			System.exit(0);
		}else if(e.getActionCommand().equals("saveExit")){
			Recorder recoder = new Recorder();
			recoder.setEts(mp.ets);
			recoder. keepRecAndEnemyYank();
			//退出
			System.exit(0);
		}else if(e.getActionCommand().equals("conGame")){
			//读出数据
			mp = new MyPanel("con");
			
			Thread t = new Thread(mp);
			t.start();
			//先删除旧的面板
			this.remove(msp);
			this.add(mp);
			// 监听
			this.addKeyListener(mp);
			//显示
			this.setVisible(true);
		}
	}
}

// 就是一个提示作用
class MyStartPanel extends JPanel implements Runnable {
	int times = 0;

	public void paint(Graphics g) {
		super.paint(g);
		g.fillRect(0, 0, 400, 300);
		
		
		// 提示信息
		if (times % 2 == 0) {
			g.setColor(Color.yellow);
			// 开关信息的字体
			Font myFont = new Font("华文新魏", Font.BOLD, 30);
			g.setFont(myFont);
			g.drawString("stage: 1", 130, 130);
		}

	}

	@Override
	public void run() {
		while (true) {
			try {
				Thread.sleep(500);

			} catch (Exception e) {
				e.printStackTrace();
			}
			times++;
			this.repaint();
		}
	}
}

// 我的面板
class MyPanel extends JPanel implements KeyListener, Runnable {
	// 定义一个坦克
	Hero hero = null;
	
	// 定义敌人的坦克
	Vector<EnemyTank> ets = new Vector<>();
	Vector<Node> nodes = new Vector<>();
	Vector<Bomb> bombs = new Vector<>();
	// 定义炸弹集合

	int enSize = 3;
	// 第义三张图片
	Image image1 = null;
	Image image2 = null;
	Image image3 = null;

	public MyPanel(String flag) {
		
		//恢复记录
		Recorder.getRecoring();
		hero = new Hero(100, 100);
		if(flag.equals("newGame")){
			
		// 初始化敌人的坦克
		for (int i = 0; i < enSize; i++) {
			EnemyTank et = new EnemyTank((i + 1) * 50, 0);
			et.setColor(0);
			et.setDirect(2);
			// 将MyPanel的敌人坦克交给敌人坦克
			et.setEts(ets);
			// 启动敌人的坦克
			Thread t = new Thread(et);
			t.start();
			// 给敌人添加一颗子弹
			Shot s = new Shot(et.x + 10, et.y + 30, 2);
			et.ss.add(s);
			new Thread(s).start();
			ets.add(et);
			}
		}else{
			nodes = new Recorder().getNodesAndEnNums();
			for (int i = 0; i < nodes.size(); i++) {
				Node node = nodes.get(i);
				EnemyTank et = new EnemyTank(node.x, node.y);
				et.setColor(0);
				et.setDirect(node.direct);
				// 将MyPanel的敌人坦克交给敌人坦克
				et.setEts(ets);
				// 启动敌人的坦克
				Thread t = new Thread(et);
				t.start();
				// 给敌人添加一颗子弹
				Shot s = new Shot(et.x + 10, et.y + 30, 2);
				et.ss.add(s);
				new Thread(s).start();
				ets.add(et);
			}
		}

		// 初始化图片
		image1 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/1.png"));
		image2 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/2.png"));
		image3 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/3.png"));

	}
	
	//提示信息
	public void showInfo(Graphics g){
		//画出提示信息坦克,(该坦克不参与战斗)
				this.drawTank(80, 330, g, 0, 1);
				g.setColor(Color.black);
				g.drawString(Recorder.getEnNum()+"", 110, 350);
				this.drawTank(130, 330, g, 0, 0);
				g.setColor(Color.black);
				g.drawString(Recorder.getMyLife()+"", 165, 350);
				
				//画出玩家的总成绩
				g.setColor(Color.black);
				Font f = new Font("宋体",Font.BOLD,20);
				g.setFont(f);
				g.drawString("您的总成绩  ", 420, 30);
				this.drawTank(420, 60, g, 0, 1);
				g.setColor(Color.black);
				g.drawString(Recorder.getAllEnNum()+"", 460, 80);
	}

	public void paint(Graphics g) {
		super.paint(g);
		g.fillRect(0, 0, 400, 300);
		//画出提示信息坦克,
		showInfo(g);
		// 画出自己的坦克
		if (hero.isLive) {
			this.drawTank(hero.getX(), hero.getY(), g, this.hero.direct, 0);
		}

		// 从ss中取出每一颗子弹,并画出
		for (int i = 0; i < this.hero.ss.size(); i++) {

			Shot myShot = hero.ss.get(i);
			// 画出子弹
			if (myShot != null && myShot.isLive == true) {
				g.draw3DRect(myShot.x, myShot.y, 1, 1, false);
			}
			if (myShot.isLive == false) {
				// 从向量中去掉该子弹
				hero.ss.remove(myShot);
			}
		}

		// 画出炸弹
		for (int i = 0; i < bombs.size(); i++) {
			// 取出炸弹
			Bomb b = bombs.get(i);
			if (b.life > 6) {
				g.drawImage(image1, b.x, b.y, 30, 30, this);
			} else if (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的生命值减少
			b.lifeDown();
			// 生命值为0,就把bombs向量去掉
			if (b.life == 0) {
				bombs.remove(b);
			}
		}

		// 画出敌人的坦克
		for (int i = 0; i < ets.size(); i++) {
			EnemyTank e = ets.get(i);
			if (e.isLive) {
				this.drawTank(e.getX(), e.getY(), g, e.getDirect(), 1);

				// 再画出敌人的子弹
				for (int j = 0; j < e.ss.size(); j++) {
					// 取出敌人的子弹
					Shot enemyShot = e.ss.get(j);
					if (enemyShot.isLive) {
						g.draw3DRect(enemyShot.x, enemyShot.y, 1, 1, false);
					} else {
						// 如果敌人的坦克死亡就从vector中删除
						e.ss.remove(enemyShot);
					}

				}
			}
		}
	}

	// 敌人的子弹是否击中我
	public void hitMe() {
		for (int i = 0; i < this.ets.size(); i++) {
			EnemyTank et = ets.get(i);
			// 取出每一颗子弹
			for (int j = 0; j < et.ss.size(); j++) {
				// 取出子弹
				Shot enemyShot = et.ss.get(j);
				if (hero.isLive) {
					this.hitTanK(enemyShot, hero);
				}
			}
		}
	}

	// 判断是否击中 敌人的坦克
	public void hitEnemyTank() {

		for (int i = 0; i < hero.ss.size(); i++) {
			// 取出子弹
			Shot myShot = hero.ss.get(i);
			// 判断子弹是否有效
			if (myShot.isLive) {
				// 取出每个坦克,与它判断
				for (int j = 0; j < ets.size(); j++) {
					// 取出坦克
					EnemyTank et = ets.get(j);
					if (et.isLive) {
						if(this.hitTanK(myShot, et)){
							//减少敌人数量
							Recorder.reduceEnNum();
							//增加我的记录
							Recorder.addEnNum();
						}
					}
				}
			}
		}

	}

	// 写出一个函数专门判断子弹是否击中敌人坦克

	public boolean hitTanK(Shot s, TanK et) {
		boolean b2 = false;

		// 判断该坦克的方向
		switch (et.direct) {
		case 0:
		case 2:
			if (s.x > et.x && s.x < et.x + 20 && s.y > et.y && s.y < et.y + 30) {
				// 击中
				// 子弹死亡
				s.isLive = false;
				// 坦克死亡
				et.isLive = false;
				b2 = true;
				// 创建一颗炸弹,放入vector中
				Bomb b = new Bomb(et.x, et.y);
				bombs.add(b);

			}
			break;
		case 1:
		case 3:
			if (s.x > et.x && s.x < et.x + 30 && s.y > et.y && s.y < et.y + 20) {
				// 击中
				// 子弹死亡
				s.isLive = false;
				// 坦克死亡
				et.isLive = false;
				b2 = true;
				// 创建一颗炸弹,放入vector中
				Bomb b = new Bomb(et.x, et.y);
				bombs.add(b);
			}
			break;
		}
		return b2;
	}

	// 画出坦克类
	public void drawTank(int x, int y, Graphics g, int direct, int type) {
		switch (type) {
		case 0:
			g.setColor(Color.cyan);
			break;
		case 1:
			g.setColor(Color.yellow);
			break;
		}
		switch (direct) {
		case 0:

			// 画出坦克
			// 1.画出左边的矩形
			g.fill3DRect(x, y, 5, 30, false);
			// 2.画出右边的矩形
			g.fill3DRect(x + 15, y, 5, 30, false);
			// 3.画出中间的矩形
			g.fill3DRect(x + 5, y + 5, 10, 20, false);
			// 4.画出圆形
			g.fillOval(x + 5, y + 10, 10, 10);
			// 5.画出线
			g.drawLine(x + 10, y + 15, x + 10, y);
			break;
		case 1:
			// 炮筒向右
			// 上面矩形
			g.fill3DRect(x, y, 30, 5, false);
			// 下面矩形
			g.fill3DRect(x, y + 15, 30, 5, false);
			// 中间矩形
			g.fill3DRect(x + 5, y + 5, 20, 10, false);
			// 圆形
			g.fillOval(x + 10, y + 5, 10, 10);
			// 炮筒
			g.drawLine(x + 15, y + 10, x + 30, y + 10);
			break;
		case 2:
			// 向下
			// 1.画出左边的矩形
			g.fill3DRect(x, y, 5, 30, false);
			// 2.画出右边的矩形
			g.fill3DRect(x + 15, y, 5, 30, false);
			// 3.画出中间的矩形
			g.fill3DRect(x + 5, y + 5, 10, 20, false);
			// 4.画出圆形
			g.fillOval(x + 5, y + 10, 10, 10);
			// 5.画出线
			g.drawLine(x + 10, y + 15, x + 10, y + 30);
			break;
		case 3:
			// 向左
			// 上面矩形
			g.fill3DRect(x, y, 30, 5, false);
			// 下面矩形
			g.fill3DRect(x, y + 15, 30, 5, false);
			// 中间矩形
			g.fill3DRect(x + 5, y + 5, 20, 10, false);
			// 圆形
			g.fillOval(x + 10, y + 5, 10, 10);
			// 炮筒
			g.drawLine(x + 15, y + 10, x, y + 10);
			break;
		}
	}

	// a左 s下 w上 d右
	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyPressed(KeyEvent e) {
		// 设置我的坦克方向
		if (e.getKeyCode() == KeyEvent.VK_W) {
			// 上
			this.hero.setDirect(0);
			this.hero.moveUp();
		} else if (e.getKeyCode() == KeyEvent.VK_D) {
			// 右
			this.hero.setDirect(1);
			this.hero.moveRight();
		} else if (e.getKeyCode() == KeyEvent.VK_S) {
			// 下
			this.hero.setDirect(2);
			this.hero.moveDown();
		} else if (e.getKeyCode() == KeyEvent.VK_A) {
			// 左
			this.hero.setDirect(3);
			this.hero.moveLeft();
		}

		if (e.getKeyCode() == KeyEvent.VK_J) {
			// 判断玩家是否按下J
			// 开火
			if (this.hero.ss.size() < 5) {
				this.hero.shotEnemy();
			}

		}
		// 重新绘制panel
		this.repaint();
	}

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

	}

	@Override
	public void run() {
		while (true) {
			try {
				Thread.sleep(100);
			} catch (Exception e) {
				e.printStackTrace();
			}
			this.hitEnemyTank();
			// 判断敌人的子弹是否击中我了
			this.hitMe();

			this.repaint();
		}
	}
}
运行结果:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值