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() {@Overridepublic 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);}@Overrideprotected 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() {@Overridepublic void windowClosing(WindowEvent arg0) {System.exit(0);}});addKeyListener(new KeyAdapter() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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();}}@Overridepublic void mouseDragged(MouseEvent e) {fillShape(e);}});addMouseListener(new MouseAdapter() {@Overridepublic 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);}@Overrideprotected 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() {@Overridepublic 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() {@Overridepublic 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;}@Overridepublic 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() {@Overridepublic 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 完整代码

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

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[flask]执行上下文的四个全局变量

flask上下文全局变量&#xff0c;程序上下文、请求上下文、上下文钩子 -- - 夏晓旭 - 博客园 (cnblogs.com) 执行上下文 执行上下文&#xff1a;即语境&#xff0c;语意&#xff0c;在程序中可以理解为在代码执行到某一行时&#xff0c;根据之前代码所做的操作以及下文即将要…

2024年腾讯云4核8G服务器多少钱一年?买1年送3个月

2024年腾讯云4核8G服务器租用优惠价格&#xff1a;轻量应用服务器4核8G12M带宽646元15个月&#xff0c;CVM云服务器S5实例优惠价格1437.24元买一年送3个月&#xff0c;腾讯云4核8G服务器活动页面 txybk.com/go/txy 活动链接打开如下图&#xff1a; 腾讯云4核8G服务器优惠价格 轻…

华为数通 HCIP-Datacom H12-831 题库补充(3/27)

2024年 HCIP-Datacom&#xff08;H12-831&#xff09;最新题库&#xff0c;完整题库请扫描上方二维码&#xff0c;持续更新。 如图所示&#xff0c;关于R4路由器通过IS-IS计算出来的IPv6路由&#xff0c;哪一选项的描述是错误的&#xff1f; A&#xff1a;R4通过IS—IS只学习到…

取消springboot中的Test类中的日志打印

使用SpringBootTest注解进行单元测试时, 打印东西的时候,总会伴随很多的无关紧要的日志信息&#xff0c;影响观感。去掉这些日志应该怎么做呢 ? 两个步骤: 第一步、修改application.properties logging.level.rootoff logging.level.org.springframeworkoff # 关闭baner信息…

SpringMvc执行流程源码解析

一、简介 Spring web Mvc是基于ServletApi构建的原始Web模块&#xff0c;从一开始就包含在Spring框架中&#xff1b; 从Servlet到SpringMvc 最典型的MVc就是JSPServletjavaBean的模式&#xff1b; 弊端&#xff1a; 1、xml下配置Servlet的映射非常麻烦&#xff0c;效率低&…

vue3+threejs新手从零开发卡牌游戏(十三):上场手牌添加攻击力文字

在utils/common.ts下新建渲染场上手牌文字方法&#xff1a; // 渲染场上手牌文字 const renderSiteCardText (mesh: any, font: any) > {return new Promise((resolve, reject) > {let pos mesh.positionconst geometry new TextGeometry( ATK ${mesh.userData._ATK}…

Windows直接运行python程序

Windows直接运行python程序 一、新建bat脚本二、新建vbs脚本 一、新建bat脚本 新建bat批处理脚本&#xff0c;写入以下内容 echo off call conda activate pytorch python app.pyecho off&#xff1a;在此语句后所有运行的命令都不显示命令行本身&#xff0c;但是本身的指令是…

ETIM -国际贸易的产品分类标准

ETIM 是除了XML 国际交流标准BMEcat之外的国际贸易的产品分类标准。 什么是ETIM &#xff1f; ETIM是一种基于分类识别共享和交换产品数据的格式。这种广泛使用的技术产品分类标准是为了构建 B2B 专业人员之间的信息流而制定的。 为什么选择ETIM&#xff1f; ETIM分类模型的开…

力扣454. 四数相加 II

思路&#xff1a;把四个数组拆成两对&#xff0c;两个分别相加&#xff0c;记录第一对的相加结果进map里&#xff0c;再把第二对数组 0-nums2-nums4 去map里面找出现了几次&#xff0c;这题不用对重复的四元组去重&#xff0c;所以出现多次都有效。 class Solution {public int…

Linux系统使用Docker部署Jupyter Notebook结合内网穿透实现公网访问本地笔记

文章目录 1. 选择与拉取镜像2. 创建容器3. 访问Jupyter工作台4. 远程访问Jupyter工作台4.1 内网穿透工具安装4.2 创建远程连接公网地址4.3 使用固定二级子域名地址远程访问 本文主要介绍如何在Ubuntu系统中使用Docker本地部署Jupyter Notebook&#xff0c;并结合cpolar内网穿透…

Vue 3中ref和reactive的区别

&#x1f90d; 前端开发工程师、技术日更博主、已过CET6 &#x1f368; 阿珊和她的猫_CSDN博客专家、23年度博客之星前端领域TOP1 &#x1f560; 牛客高级专题作者、打造专栏《前端面试必备》 、《2024面试高频手撕题》 &#x1f35a; 蓝桥云课签约作者、上架课程《Vue.js 和 E…

【C++】拆分详解 - 类与对象

文章目录 一、面向过程和面向对象初步认识二、类的引入  2.1 类的定义  2.2 类的访问限定符及封装   2.2.1  访问限定符   2.2.2  C中struct和class的区别是什么&#xff1f;   2.2.3  封装 2.3 类的作用域  2.4 类的实例化  2.5 类对象模型   2.5.1  …