javaSwing坦克大战游戏

在游戏开发领域,坦克大战是一款经典的游戏,其简单而又耐玩的玩法吸引了无数玩家。而今,在Java编程技术的支持下,我们可以用Java Swing技术轻松实现这款经典游戏。本文将介绍如何使用Java Swing技术编写坦克大战游戏,并简要介绍其实现过程和关键代码。

一、游戏简介

坦克大战是一款双人对战的游戏,玩家控制着一辆坦克在游戏地图中移动、射击,目标是摧毁对方坦克。游戏地图通常由障碍物组成,增加了游戏的难度和挑战性。通过不断躲避敌方的攻击、寻找机会进行反击,玩家可以获胜。

二、技术选型

在这个项目中,我们选择使用Java Swing技术来实现游戏的界面和交互逻辑。Java Swing是Java语言的一个GUI库,提供了丰富的组件和功能,非常适合用于开发图形化应用程序。

实现过程

  1. 游戏界面设计
    首先,我们需要设计游戏的界面。通过Java Swing提供的组件,我们可以轻松地创建游戏地图、坦克、子弹等元素,并实现它们的显示和交互。

  2. 游戏逻辑实现
    游戏逻辑是游戏开发的核心部分。在坦克大战中,我们需要实现坦克的移动、射击、碰撞检测等功能。通过Java Swing提供的事件监听器和定时器,我们可以实现这些功能,并确保游戏的流畅性和可玩性。

  3. 用户交互实现
    最后,我们需要实现用户交互功能,包括键盘控制坦克的移动和射击、显示游戏信息等。Java Swing提供了丰富的事件处理机制,我们可以利用这些机制来实现用户交互功能,并提升游戏的体验。

三、关键代码

BeginFrame.java

package hjzgg.main;

import hjzgg.layer.TheLayer;
import hjzgg.map.TankMap;
import hjzgg.set.MySet;
import hjzgg.size.TheSize;
import hjzgg.tank.MyTank;
import hjzgg.tank.ShellThread;
import hjzgg.tank.EnemyTankThread;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneLayout;


class MapDialog extends JDialog{
	public static int chooseMap = -1;
	public MapDialog(Dialog owner, String title, boolean modal){
		super(owner, title, modal);
		int len = TankMap.tankmap.length;
		setSize(500, 500);
		setResizable(false);
		JPanel mapPane = new JPanel();
		mapPane.setLayout(new BoxLayout(mapPane, BoxLayout.Y_AXIS));
		JScrollPane scrollpane = new JScrollPane(mapPane);
		add(scrollpane);
		for(int i=1; i<=len; ++i){
			JPanel p = new JPanel();
			ShapePane tankmap = new ShapePane("tankmap/map" + i + ".jpg", 350, 350);
			tankmap.setPreferredSize(new Dimension(350, 350));
			JButton btnChoose = new JButton("地图"+i);
			final int choose = i-1;
			btnChoose.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					chooseMap = choose;
					MapDialog.this.dispose();
				}
			});
			p.add(tankmap);
			p.add(btnChoose);
			mapPane.add(p);
		}
		setVisible(true);
	}
}

class BeginPanel extends JPanel{
	private String img_path = null;
	private int width, height;
	private boolean isBorder = false;
	public BeginPanel(String path, int w, int h){
		this.img_path = path;
		this.width = w;
		this.height = h;
	}
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);	
		if(img_path==null) return;
		g.drawImage(new ImageIcon(img_path).getImage(), 0, 0, width, height, this);
	}
	
	@Override
	protected void paintBorder(Graphics g) {
		 if(isBorder){
			 float lineWidth = 5.0f;
			 ((Graphics2D)g).setStroke(new BasicStroke(lineWidth));
			 ((Graphics2D)g).setColor(Color.RED);
			 ((Graphics2D)g).drawRect(0, 0, 100, 50);
		 }
	}
	public void setIsBorder(boolean f){
		this.isBorder = f;
	}
	public void setImgPath(String path){
		this.img_path = path;
	}
}

public class BeginFrame extends Frame{
	private int index = 0;
	private BeginPanel pic1 = new BeginPanel("tank_battle.png", 800, 200);
	private BeginPanel[] tank = new BeginPanel[2];
	private BeginPanel begin = new BeginPanel("begin.png", 100, 50);
	private BeginPanel continuing = new BeginPanel("continue.png", 100, 50);
	private JPanel pmenu1 = new JPanel();
	private JPanel pmenu2 = new JPanel();
	private JLabel l1 = new JLabel("");
	private JLabel l2 = new JLabel("");
	private JLabel l3 = new JLabel("");
	private JLabel l4 = new JLabel("");
	private JLabel l5 = new JLabel("");
	
	private void changTank(int i){
		tank[i].setImgPath("tank/mytank_right.gif");
		tank[i].updateUI();
		
		tank[i^1].setImgPath(null);
		tank[i^1].updateUI();
	}
	
	private void borderPaint(BeginPanel x, boolean f){
		x.setIsBorder(f);
		x.updateUI();
	}
	
	class BorderChange implements Runnable{
		private BeginPanel tmp = null;
		private int index = 0;
		public BorderChange(BeginPanel x, int index){
			tmp = x;
			this.index = index;
		}
		public void run() {
			try {
				borderPaint(tmp, true);
				Thread.sleep(300);
				borderPaint(tmp, false);
				Thread.sleep(300);
				borderPaint(tmp, true);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			if(index == 0){
				//选择地图
				MapDialog md = new MapDialog(null, "请选择地图!", true);
				
				TankFrame tf = new TankFrame(TankMap.tankmap[MapDialog.chooseMap]);
				ShellThread st = new ShellThread();
				new Thread(st).start();//炮弹线程
				MySet.getInstance().setSt(st);
				EnemyTankThread ett = new EnemyTankThread(tf);
				new Thread(ett).start();//坦克出现 线程
				MySet.getInstance().setEtt(ett);
				
				JLayeredPane jlp = tf.getJlp();
				MyTank mt = new MyTank("mytank", "tank/mytank_up.gif", 0, tf);
				MySet.getInstance().getTankSet().add(mt);
				mt.setBounds(280, 630, TheSize.tank_width, TheSize.tank_height);
				jlp.add(mt);
				jlp.setLayer(mt, TheLayer.tank);
				tf.setVisible(true);
				mt.requestFocus();//在窗口显示之后将焦点移到MyTank上,否则不能监听键盘消息
			} else {
				SelfConfigFrame scf = new SelfConfigFrame();
				scf.setResizable(false);
				scf.setVisible(true);
			}
			BeginFrame.this.dispose();
		}
	}
	
	public BeginFrame(){
		
		  addWindowListener(new WindowAdapter() {
				@Override
				public void windowClosing(WindowEvent arg0) {
					System.exit(0);
				}
			  
		  });
		  
		  addKeyListener(new KeyAdapter() {

			@Override
			public void keyPressed(KeyEvent arg0) {
				 switch(arg0.getKeyCode()){
				 	case KeyEvent.VK_UP:
				 		index = (index+1)%2;
				 		changTank(index);
				 		break;
				 	case KeyEvent.VK_DOWN:
				 		index = (index-1 + 2)%2;
				 		changTank(index);
				 		break;
				 	case KeyEvent.VK_ENTER:
				 		if(index==0){
				 			borderPaint(continuing, false);
				 			new Thread(new BorderChange(begin, index)).start();
				 		}
				 		else{
				 			borderPaint(begin, false);
				 			new Thread(new BorderChange(continuing, index)).start();
				 		}
				 		break;
				 	default:
				 		break;
				 }
			}
			  
		  });
		  tank[0] = new BeginPanel("tank/mytank_right.gif", 50, 50);
		  tank[1] = new BeginPanel(null, 50, 50);
		  setBackground(Color.BLACK);
		  setSize(800,600);
		  setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		  pic1.setBackground(Color.BLACK);
		  pic1.setPreferredSize(new Dimension(800, 200));
		  add(pic1);
		  
		  pmenu1.setPreferredSize(new Dimension(800, 50));
		  tank[0].setBackground(Color.BLACK);
		  tank[0].setPreferredSize(new Dimension(100, 50));
		  begin.setBackground(Color.BLACK);
		  begin.setPreferredSize(new Dimension(100, 50));
		  pmenu1.setBackground(Color.BLACK);
		  pmenu1.add(tank[0]);
		  pmenu1.add(begin);
		  add(pmenu1);
		  
		  pmenu2.setBackground(Color.BLACK);
		  pmenu2.setPreferredSize(new Dimension(800, 50));
		  tank[1].setBackground(Color.BLACK);
		  tank[1].setPreferredSize(new Dimension(100, 50));
		  continuing.setBackground(Color.BLACK);
		  continuing.setPreferredSize(new Dimension(100, 50));
		  pmenu2.add(tank[1]);
		  pmenu2.add(continuing);
		  add(pmenu2);
		  
		  l1.setFont(new Font("华文彩云", Font.BOLD, 25));
		  l1.setForeground(Color.RED);
		  JPanel px = new JPanel();
		  px.setBackground(Color.BLACK);
		  px.add(l1);
		  add(px);
		  
		  l2.setFont(new Font("黑体", Font.BOLD, 15));
		  px = new JPanel();
		  px.setBackground(Color.BLACK);
		  px.add(l2);
		  add(px);
		  
		  l3.setFont(new Font("黑体", Font.BOLD, 15));
		  px = new JPanel();
		  px.setBackground(Color.BLACK);
		  px.add(l3);
		  add(px);
		  
		  l4.setFont(new Font("宋体", Font.ITALIC, 15));
		  l4.setForeground(Color.green);
		  px = new JPanel();
		  px.setBackground(Color.BLACK);
		  px.add(l4);
		  add(px);
		  
		  l5.setFont(new Font("黑体", Font.ITALIC, 15));
		  l5.setForeground(Color.green);
		  px = new JPanel();
		  px.setBackground(Color.BLACK);
		  px.add(l5);
		  add(px);
	}
	
	public static void main(String[] args){
		BeginFrame bf = new BeginFrame();
		bf.setVisible(true);
	}
}

SelfConfigFrame.java

package hjzgg.main;

import hjzgg.id.IDblock;
import hjzgg.layer.TheLayer;
import hjzgg.set.MySet;
import hjzgg.size.TheSize;
import hjzgg.tank.EnemyTankThread;
import hjzgg.tank.MyTank;
import hjzgg.tank.ShellThread;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Arrays;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

class MyLayeredPane extends JLayeredPane{
	private SelfConfigFrame scf = null;
	public MyLayeredPane(SelfConfigFrame f){
		this.scf = f;
		addMouseListener(new MouseAdapter() {
			@Override
			public void mouseExited(MouseEvent e) {
				scf.getSp().setVisible(false);
			}
		});
	}
}


class BlockPanel extends JPanel{
	private String img_path = null;
	private int height=0, width=0;
	private SelfConfigFrame scf = null;
	
	private void fillShape(MouseEvent e){
		 int x = e.getX();
		 int y = e.getY();
		 Point ptf = SwingUtilities.convertPoint(BlockPanel.this, new Point(x, y), getParent());
		 //块索引
		 int j = ptf.x / TheSize.block_height;
		 int i = ptf.y / TheSize.block_width;
		 
		 ShapePane sp = scf.getSp();
		 MyLayeredPane parent = scf.getMyLayeredPane();
		 String path = null;
		 if(scf.getSta()[i][j] != IDblock.home && scf.getSta()[i][j] != IDblock.tank_appear && SelfConfigFrame.choose != -1){
			 switch(SelfConfigFrame.choose){
			 	case IDblock.grass:
			 		path = "草地.png";
			 		break;
			 	case IDblock.steel_wall:
			 		path = "钢墙.jpg";
			 		break;
			 	case IDblock.wall:
			 		path = "砖墙.jpg";
			 		break;
			 	case IDblock.water:
			 		path = "水.jpg";
			 		break;
			 	case IDblock.not:
			 		path = null;
			 		break;
			 	default:
			 }
			 scf.getSta()[i][j] = SelfConfigFrame.choose;//当前 块 填充的类型
			 scf.getBlock()[i][j].setImgPath(path);
			 scf.getBlock()[i][j].update(scf.getBlock()[i][j].getGraphics());
			 sp.setBounds(ptf.x+5, ptf.y+5, TheSize.shapepane_width, TheSize.shapepane_height);
		 }
	}
	
	public BlockPanel(SelfConfigFrame f, String path, int w, int h){
		this.img_path = path;
		height = h;
		width = w;
		scf = f;
		addMouseMotionListener(new MouseMotionListener() {
			@Override
			public void mouseMoved(MouseEvent e) {
				 int x = e.getX();
				 int y = e.getY();
				 Point ptf = SwingUtilities.convertPoint(BlockPanel.this, new Point(x, y), getParent());
				 ShapePane sp = scf.getSp();
				 MyLayeredPane parent = scf.getMyLayeredPane();
				 String path = null;
				 if(SelfConfigFrame.choose != -1){
					 if(!sp.isVisible()){
						 switch(SelfConfigFrame.choose){
						 	case IDblock.grass:
						 		path = "草地.png";
						 		break;
						 	case IDblock.steel_wall:
						 		path = "钢墙.jpg";
						 		break;
						 	case IDblock.wall:
						 		path = "砖墙.jpg";
						 		break;
						 	case IDblock.water:
						 		path = "水.jpg";
						 		break;
						 	case IDblock.not:
						 		path = null;
						 		break;
						 	default:
						 }
						 sp.setVisible(true);
						 sp.setImgPath(path);
					 }
					 sp.setBounds(ptf.x+5, ptf.y+5, TheSize.shapepane_width, TheSize.shapepane_height);
					 parent.updateUI();
				 }
			}
			
			@Override
			public void mouseDragged(MouseEvent e) {
				fillShape(e);
			}
		});
		
		addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				fillShape(e);
			}
		});
	}
	
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);	
		if(img_path==null) return;
		g.drawImage(new ImageIcon(img_path).getImage(), 0, 0, width, height, this);
	}
	
	@Override
	protected void paintBorder(Graphics g) {
		 float lineWidth = 2.0f;
		 ((Graphics2D)g).setStroke(new BasicStroke(lineWidth));
		 ((Graphics2D)g).setColor(Color.GREEN);
		 ((Graphics2D)g).drawRect(0, 0, width, height);
	}
 
	public void setImgPath(String path){
		this.img_path = path;
	}
}

class ShapeButton extends JButton{
	private String img_path = null;
	public ShapeButton(String path){
		this.img_path = path;
		addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SelfConfigFrame.choose = Integer.parseInt(e.getActionCommand());
			}
		});
	}
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);	
		if(img_path==null) return;
		g.drawImage(new ImageIcon(img_path).getImage(), 0, 0, TheSize.shapebtn_width, TheSize.shapebtn_height, this);
	}
	
}

public class SelfConfigFrame extends JFrame{
	
	public static int choose = -1;
	private ShapePane sp = new ShapePane(null, TheSize.shapepane_width, TheSize.shapepane_height);
	private MyLayeredPane p = null;
	private JPanel pl = null;
	private JPanel pr = null;
	private JPanel pu = null;
	
	private JButton beginGame = new JButton("开始游戏");
	private JButton reset = new JButton("重置自定义");
	public ShapePane getSp() {
		return sp;
	}

	public MyLayeredPane getMyLayeredPane() {
		return p;
	}

	public  int[][] getSta() {
		return sta;
	}

	private JPanel pd = null;
	
	private JPanel home = null;
	
	public  int[][] sta = new int[20][28];
	private BlockPanel[][] block = new BlockPanel[20][28];
	
	public BlockPanel[][] getBlock() {
		return block;
	}
	
	public void staInit(){
		//家所在的位置不能填充
		sta[17][12]=sta[17][13]=sta[17][14]=sta[17][15]=
		sta[18][12]=sta[18][13]=sta[18][14]=sta[18][15]=
		sta[19][12]=sta[19][13]=sta[19][14]=sta[19][15]=IDblock.home;
		
		//坦克出现的位置不能填充
		sta[0][0]=sta[0][1]=sta[1][1]=sta[1][0]=IDblock.tank_appear;
		sta[0][26]=sta[0][27]=sta[1][27]=sta[1][26]=IDblock.tank_appear;
		
		sta[18][9]=sta[19][9]=sta[19][8]=sta[18][8]=IDblock.tank_appear;
	}

	public void init(){
		for(int i=0; i<block.length; ++i)
			for(int j=0; j<block[i].length; ++j){
				block[i][j] = new BlockPanel(this, null, TheSize.block_width, TheSize.block_height);
				block[i][j].setBounds(j*TheSize.block_width, i*TheSize.block_height, TheSize.block_width, TheSize.block_height);
				block[i][j].setBackground(Color.BLACK);
				p.add(block[i][j]);
				p.setLayer(block[i][j], 1);
			}
		//图片随鼠标移动
		sp.setVisible(false);
		sp.setBounds(0, 0, TheSize.shapepane_width, TheSize.shapepane_height);
		p.add(sp);
		p.setLayer(sp, 3);
		//home图片
		home = new BlockPanel(this, "家.jpg", TheSize.block_width*4, TheSize.block_height*3);
		home.setBounds(12*TheSize.block_width, 17*TheSize.block_height, TheSize.block_width*4, TheSize.block_height*3);
		p.add(home);
		p.setLayer(home, 2);
		
		staInit();
	}
	
	public SelfConfigFrame(){
		p = new MyLayeredPane(this);
		pl = new JPanel();
		pr = new JPanel();
		pu = new JPanel();
		pd = new JPanel();
		
		setSize(1127, 800);
		p.setOpaque(true);
		p.setBackground(Color.BLACK);
		pl.setPreferredSize(new Dimension(0, 0));
		pr.setPreferredSize(new Dimension(130, 0));
		pu.setPreferredSize(new Dimension(0, 20));
		pd.setPreferredSize(new Dimension(0, 30));
		p.setPreferredSize(new Dimension(990, 700));
		p.setLayout(null);
		
		//添加障碍物图片
		JPanel title = new JPanel();
		title.setPreferredSize(new Dimension(100,50));
		title.add(new JLabel("自定义障碍物:"));
		pr.add(title);
		ShapeButton btn = new ShapeButton("草地.png");
		btn.setPreferredSize(new Dimension(TheSize.shapebtn_width, TheSize.shapebtn_height));
		btn.setActionCommand("2");
		pr.add(btn);
		pr.add(new JLabel("草地"));
		btn = new ShapeButton("钢墙.jpg");
		btn.setPreferredSize(new Dimension(TheSize.shapebtn_width, TheSize.shapebtn_height));
		btn.setActionCommand("3");
		pr.add(btn);
		pr.add(new JLabel("钢墙"));
		btn = new ShapeButton("水.jpg");
		btn.setPreferredSize(new Dimension(TheSize.shapebtn_width, TheSize.shapebtn_height));
		btn.setActionCommand("5");
		pr.add(btn);
		pr.add(new JLabel("    水"));
		btn = new ShapeButton("砖墙.jpg");
		btn.setPreferredSize(new Dimension(TheSize.shapebtn_width, TheSize.shapebtn_height));
		btn.setActionCommand("4");
		pr.add(btn);
		pr.add(new JLabel("   砖墙     "));
		btn = new ShapeButton(null);
		btn.setBackground(Color.BLACK);
		btn.setText("擦      除");
		btn.setPreferredSize(new Dimension(TheSize.shapebtn_width, TheSize.shapebtn_height/2));
		btn.setActionCommand("0");
		pr.add(btn);
		
		beginGame.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				TankFrame tf = new TankFrame(sta);//将创建的地图传递到游戏中去
				ShellThread st = new ShellThread();
				new Thread(st).start();//炮弹线程
				MySet.getInstance().setSt(st);
				EnemyTankThread ett = new EnemyTankThread(tf);
				new Thread(ett).start();//坦克出现 线程
				MySet.getInstance().setEtt(ett);
				
				JLayeredPane jlp = tf.getJlp();
				MyTank mt = new MyTank("mytank", "tank/mytank_up.gif", 0, tf);
				MySet.getInstance().getTankSet().add(mt);
				mt.setBounds(280, 630, TheSize.tank_width, TheSize.tank_height);
				jlp.add(mt);
				jlp.setLayer(mt, TheLayer.tank);
				tf.setVisible(true);
				mt.requestFocus();//在窗口显示之后将焦点移到MyTank上,否则不能监听键盘消息
				SelfConfigFrame.this.dispose();
			}
		});
		
		reset.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				 for(int i=0; i<sta.length; ++i) Arrays.fill(sta[i], 0);
				 staInit();
				 for(int i=0; i<block.length; ++i)
						for(int j=0; j<block[i].length; ++j){
							block[i][j].setImgPath(null);
							block[i][j].updateUI();
						}
			}
		});
		
		pr.add(beginGame);
		pr.add(reset);
		
		setLayout(new BorderLayout());
		add(pl, BorderLayout.WEST);
		add(pr, BorderLayout.EAST);
		add(pd, BorderLayout.SOUTH);
		add(pu, BorderLayout.NORTH);
		add(p, BorderLayout.CENTER);
		
		init();
	}
	
//	public static void main(String[] args){
//		SelfConfigFrame scf = new SelfConfigFrame();
//		scf.setResizable(false);
//		scf.setVisible(true);
//	}
}

ShapePane.java

package hjzgg.main;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class ShapePane extends JPanel implements Comparable{
	private String img_path = null;
	private int w, h, id=-1;
	public ShapePane(String path, int w, int h){
		this.img_path = path;
		this.w = w;
		this.h = h;
	}
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		if(img_path != null)
			g.drawImage(new ImageIcon(img_path).getImage(), 0, 0, w, h, this);
		else{
			g.setColor(Color.BLACK);
			g.fillRect(0, 0, w, h);
		}
	}
	public void setImgPath(String path){
		this.img_path = path;
	}
	@Override
	public int compareTo(Object o) {
		return o.hashCode() - this.hashCode();
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
}

TankFrame.java

package hjzgg.main;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import hjzgg.id.IDblock;
import hjzgg.layer.TheLayer;
import hjzgg.set.MySet;
import hjzgg.size.TheSize;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

public class TankFrame extends JFrame{
	public static int ID = 0;
	public  int[][] sta = null;//地图状态标记
	
	public int[][] getSta() {
		return sta;
	}

	public void setSta(int[][] sta) {
		this.sta = sta;
	}

	private JLayeredPane jlp = null;
	private ShapePane pl = null;
	private JPanel pr = null;
	private JPanel pu = null;
	private JPanel pd = null;
	
	public JLayeredPane getJlp(){
		return jlp;
	}
	
	public JPanel getPr(){
		return pr;
	}
	
	public void initMap(JLayeredPane xjlp){
		//home图片
		ShapePane home = new ShapePane("家.jpg", TheSize.block_width*4, TheSize.block_height*3);
		home.setBounds(12*TheSize.block_width, 17*TheSize.block_height, TheSize.home_width, TheSize.home_height);
		home.setId(IDblock.home);
		xjlp.add(home, TheLayer.wall, -1);
		MySet.getInstance().getOtherSet().add(home);
		if(sta != null){
			for(int i=0; i<sta.length; ++i){
				//System.out.print("{");//打印生成的地图
				for(int j=0; j<sta[i].length; ++j){
					ShapePane sp = null;
					int layer = -1;
//					System.out.print(sta[i][j]);
//					if(j==sta[i].length-1) System.out.print("}");
//					System.out.print(",");
					switch(sta[i][j]){
						case IDblock.grass:
							sp = new ShapePane("草地.png", TheSize.block_width, TheSize.block_height);
							sp.setOpaque(false);
							sp.setId(IDblock.grass);
							layer = TheLayer.grass;
							break;
						case IDblock.steel_wall:
							sp = new ShapePane("钢墙.jpg", TheSize.block_width, TheSize.block_height);
							sp.setId(IDblock.steel_wall);
							layer = TheLayer.wall;
							break;
						case IDblock.wall:
							sp = new ShapePane("砖墙.jpg", TheSize.block_width, TheSize.block_height);
							sp.setId(IDblock.wall);
							layer = TheLayer.wall;
							break;
						case IDblock.water:
							sp = new ShapePane("水.jpg", TheSize.block_width, TheSize.block_height);
							sp.setId(IDblock.water);
							layer = TheLayer.water;
							break;
						default:
					}
					if(sp != null){
						MySet.getInstance().getOtherSet().add(sp);
						sp.setBounds(j*TheSize.block_width, i*TheSize.block_height, TheSize.block_width, TheSize.block_height);;
						xjlp.add(sp, layer, -1);
					}
				}
				//System.out.println();
			}
		}
	}
	
	public TankFrame(int[][] sta){
		super("HJZGG-TankBattle");
		this.sta = sta;
		jlp = new JLayeredPane();
		pl = new ShapePane("左标题.png", 100, 700);
		pr = new JPanel();
		pu = new JPanel();
		pd = new JPanel();
		pu.add(new JLabel("坦克大战-------hjzg"));
		JLabel tankCnt = new JLabel("击杀坦克数量:");
		pr.add(tankCnt);
		setSize(1217, 800);
		jlp.setOpaque(true);
		jlp.setBackground(Color.BLACK);
		initMap(jlp);
		pl.setPreferredSize(new Dimension(100, 0));
		pr.setPreferredSize(new Dimension(120, 0));
		pu.setPreferredSize(new Dimension(0, 20));
		pd.setPreferredSize(new Dimension(0, 20));
		jlp.setPreferredSize(new Dimension(980, 700));
		jlp.setLayout(null);
		
		setLayout(new BorderLayout());
		add(pl, BorderLayout.WEST);
		add(pr, BorderLayout.EAST);
		add(pd, BorderLayout.SOUTH);
		add(pu, BorderLayout.NORTH);
		add(jlp, BorderLayout.CENTER);
		
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				System.exit(0);// 终止当前正在运行的 Java 虚拟机。
			}
		});
	}
	
}

Rect.java

package hjzgg.Rect;

public class Rect {
	public int x1,x2, y1,y2;
	public Rect(int x1, int y1, int x2, int y2){
		this.x1 = x1;
		this.x2 = x2;
		this.y1 = y1;
		this.y2 = y2;
	}
	
	public static boolean isCorss(Rect r1, Rect r2){
		 int cx1 = Math.max(r1.x1, r2.x1);
		 int cy1 = Math.max(r1.y1, r2.y1);
		 int cx2 = Math.min(r1.x2, r2.x2);
		 int cy2 = Math.min(r1.y2, r2.y2);
		 
		 if(cx1 > cx2) return false;
		 if(cy1 > cy2) return false;
		 if( (cx2-cx1)*(cy2-cy1) == 0) return false;
		 
		 return true;
	}
}

四、游戏截图

请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述

五、联系与交流

q:969060742 完整代码

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/493152.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

某对象存储元数据集群改造流水账

软件产品&#xff1a;某厂商提供的不便具名的对象存储产品&#xff0c;核心底层技术源自HDFS和Amazon S3&#xff0c;元数据集群采用了基于MongoDB的NOSQL数据库产品和MySQL数据库产品相结合。 该产品的元数据逻辑示意图如下&#xff1a; 业务集群现状&#xff1a;当前第3期建…

Qt 窗口MainWindow(上)

Qt 窗口是通过 QMainWindow 类来实现的。 QMainWindow 是一个为用户提供主窗口程序的类&#xff0c;继承自 QWidget 类&#xff0c;并且提供了⼀个预定义的布局。QMainWindow 包含一个菜单栏&#xff08;menubar&#xff09;、多个工具栏(toolbars)、多个浮动窗口&#xff08;…

JVM第八讲:GC - Java 垃圾回收基础知识

GC - Java 垃圾回收基础知识 本文是JVM第八讲&#xff0c; Java 垃圾回收基础知识。垃圾收集主要是针对堆和方法区进行&#xff1b;程序计数器、虚拟机栈和本地方法栈这三个区域属于线程私有的&#xff0c;只存在于线程的生命周期内&#xff0c;线程结束之后也会消失&#xff0…

Vue3尚硅谷张天禹笔记

1. Vue3简介 2020年9月18日&#xff0c;Vue.js发布版3.0版本&#xff0c;代号&#xff1a;One Piece&#xff08;n 经历了&#xff1a;4800次提交、40个RFC、600次PR、300贡献者 官方发版地址&#xff1a;Release v3.0.0 One Piece vuejs/core 截止2023年10月&#xff0c;最…

Java零基础入门到精通_Day 3

37 switch default&#xff1a; 后面的break;可以省略 38 春夏秋冬 注意事项:在switch语句中&#xff0c;如果case控制的语句体后面不写break&#xff0c;将出现穿透现象&#xff0c;在不判断下一个case值的情况下&#xff0c;向下运行 直到遇到break&#xff0c;或者整体swi…

在Python中进行封装

在Python中&#xff0c;封装是一种面向对象编程&#xff08;OOP&#xff09;的特性&#xff0c;它允许我们将数据&#xff08;属性&#xff09;和操作这些数据的方法&#xff08;函数&#xff09;捆绑在一起&#xff0c;形成一个独立的对象。封装的主要目的是隐藏对象的内部状态…

如何保证缓存与数据库的双写一致性?

如何保证缓存与数据库的双写一致性&#xff1f; 概述同步策略更新缓存还是删除缓存&#xff1a;先操作数据库还是缓存&#xff1a;案例一、先删除缓存&#xff0c;在更新数据库案例二 先操作数据库&#xff0c;再删除缓存 延时双删策略&#xff08;不推荐&#xff09;使用分布式…

Java拆装箱及128陷阱

有以下一段代码&#xff1a; Integer a 123; Integer b 123; int c 123; int d 123; System.out.println(c d); System.out.println(a b); System.out.println(a c); 这段代码运行的结果是什么呢&#xff1f; c d 一定为True。 由于Java中存在自动拆装箱&#xff0…

刷到一个问题还请道友们解疑

问题如上&#xff0c;题目挺简单的&#xff0c;就是插入后排序的思路&#xff0c;我的代码如下&#xff1a; #include <bits/stdc.h>using namespace std; int f(int x,int y){return x < y;//其实要这个没有用&#xff0c;默认是就是从小到大排序 }int main(){int n…

【MySQL】详谈约束

&#x1f466;个人主页&#xff1a;Weraphael ✍&#x1f3fb;作者简介&#xff1a;目前学习计网、mysql和算法 ✈️专栏&#xff1a;MySQL学习 &#x1f40b; 希望大家多多支持&#xff0c;咱一起进步&#xff01;&#x1f601; 如果文章对你有帮助的话 欢迎 评论&#x1f4ac…

PostgreSQL中控制文件的解析与恢复

最近遇到有人问起PG中控制文件的一些使用问题,总结了一下。 1、PG控制文件简介 1.1、存储的位置 它的路径位于: 相关信息,可以用命令pg_controldata得到: [10:41:27-postgres@centos2:/var/lib/pgsql/14/data/global]$ pg_controldata -D $PGDATA pg_control version …

git提交和回退

目录 一. git 提交二. git commit 后准备回退&#xff0c;尚未 git push三. git add 添加多余文件 撤销操作四. 更改 Git commit 的默认编辑器五. 撤销某个commit的变更六. 回退到之前的commit状态总结&#xff1a; 一. git 提交 git pull # 更新代码 git status # 查看代码状…

【保姆级讲解如何Stable Diffusion本地部署】

&#x1f308;个人主页:程序员不想敲代码啊&#x1f308; &#x1f3c6;CSDN优质创作者&#xff0c;CSDN实力新星&#xff0c;CSDN博客专家&#x1f3c6; &#x1f44d;点赞⭐评论⭐收藏 &#x1f91d;希望本文对您有所裨益&#xff0c;如有不足之处&#xff0c;欢迎在评论区提…

rancher2.6部署

rancher2.6部署 1、准备环境镜像 2、部署3、密码获取密码设置新密码 4、设置语言5、导入已有集群 1、准备 环境 docker-ce-20.10.23-3.el8.x86_64.rpm以及依赖rpm kubernetes&#xff1a;v1.23.17 镜像 &#xff08;rancher和k8s有个版本对应关系&#xff0c;rancher2.5就不…

OSCP靶场--GLPI

OSCP靶场–GLPI 考点(CVE-2022-35914 php执行函数绕过ssh端口转发jetty xml RCE) 1.nmap扫描(ssh端口转发) ## ┌──(root㉿kali)-[~/Desktop] └─# nmap 192.168.194.242 -sV -sC --min-rate 2500 Starting Nmap 7.92 ( https://nmap.org ) at 2024-03-26 22:22 EDT Nmap…

第一个JDBC程序

一、JDBC的概念&#xff1a; JDBC 是 Java DataBase Connectivity (Java 数据连接)技术的简称&#xff0c;是一种可用于执行 SQL 语句的 Java API。它由一些 java 语言编写的类和接口组成&#xff1b;程序员通过使用 jdbc 可以方便地将 SQL 语句传送给几乎任何一种数据库。 二…

C++ :STL中vector扩容机制

vector是STL提供的动态数组&#xff0c;它会在内部空间不够用时动态的调整自身的大小&#xff0c;调整过程中会有大量的数据拷贝&#xff0c;为了减少数据拷贝的次数vector会在调整空间的时候尽量多申请一些空间&#xff0c;这些预留出的空间可以很大程度上减少拷贝的发生。 在…

Shadow Tactics

本题链接&#xff1a; 题目&#xff1a; 样例&#xff1a; 输入 1 1 3 3 U 2 2 2 输出 YES 思路&#xff1a; 根据题意&#xff0c;隼人的坐标是不会动的&#xff0c;并且士兵只能直线来回行动。 所以这里我们需要分成三种情况。 1、隼人坐标在士兵走动路线之间&#xff0c;…

linux如何查看编译器支持的C++版本(支持C++11、支持C++14、支持C++17、支持C++20)(编译时不指定g++版本,默认使用老版本编译)

参考:https://blog.csdn.net/Dontla/article/details/129016157 C各个版本 C11 C11是一个重要的C标准版本&#xff0c;于2011年发布。C11带来了许多重要的改进&#xff0c;包括&#xff1a; 智能指针&#xff1a;引入了shared_ptr和unique_ptr等智能指针&#xff0c;用于更好地…

http认证

1.Digest认证 各字段含义&#xff1a; Nonce 服务器直接返回的数据 H1MD5(user”:”realmpassword) H2MD5(method”:”url) method为请求类型、url不包括域名 Nc 指当前的第几次请求&#xff0c;使用8位16进制显示 Cnonce 8位随机字符串 ResponseMD5(H1”:”nonce”:”…