在游戏开发领域,坦克大战是一款经典的游戏,其简单而又耐玩的玩法吸引了无数玩家。而今,在Java编程技术的支持下,我们可以用Java Swing技术轻松实现这款经典游戏。本文将介绍如何使用Java Swing技术编写坦克大战游戏,并简要介绍其实现过程和关键代码。
一、游戏简介
坦克大战是一款双人对战的游戏,玩家控制着一辆坦克在游戏地图中移动、射击,目标是摧毁对方坦克。游戏地图通常由障碍物组成,增加了游戏的难度和挑战性。通过不断躲避敌方的攻击、寻找机会进行反击,玩家可以获胜。
二、技术选型
在这个项目中,我们选择使用Java Swing技术来实现游戏的界面和交互逻辑。Java Swing是Java语言的一个GUI库,提供了丰富的组件和功能,非常适合用于开发图形化应用程序。
实现过程
-
游戏界面设计
首先,我们需要设计游戏的界面。通过Java Swing提供的组件,我们可以轻松地创建游戏地图、坦克、子弹等元素,并实现它们的显示和交互。 -
游戏逻辑实现
游戏逻辑是游戏开发的核心部分。在坦克大战中,我们需要实现坦克的移动、射击、碰撞检测等功能。通过Java Swing提供的事件监听器和定时器,我们可以实现这些功能,并确保游戏的流畅性和可玩性。 -
用户交互实现
最后,我们需要实现用户交互功能,包括键盘控制坦克的移动和射击、显示游戏信息等。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 完整代码