扫雷不能不说一款非常经典的游戏,无聊时候可以打发时间,虽然玩了很久,但还不知道它是怎么写的,所以自己就尝试动手做了个。
众所周知,java的swing采用mvc模式,即模型-视图-控制器,所以如果真的了解了这个模式,较c++,用java做个游戏还是比较容易的。下面是我写的扫雷的代码
import java.awt.*;
import java.io.*;
import java.net.URL;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.concurrent.*;
public class MineSweep
{
public static void main(String[] args)
{
JFrame frame = new MineFrame(www.ttays.com);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class MineFrame extends JFrame
{
private JPanel buttonPanel;
private MinePanel mp;
private int rn,cn;
public static final int DEFAULT_WIDTH = 650;
public static final int DEFAULT_HEIGHT = 450;
public static final int DEFAULT_CN = 9;
public static final int DEFAULT_RN = 9;
public JLabel remainMine;
public JLabel mes;
private JComboBox cb;
public MineFrame()
{
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
setTitle("扫雷");
mp = new MinePanel(DEFAULT_CN,DEFAULT_RN,this);
mp.setMinenum(10);
mp.setRC(9,9);
buttonPanel = new JPanel();
add(mp,BorderLayout.CENTER);
mes = new JLabel("");
mes.setEnabled(false);
add(mes,BorderLayout.EAST);
cb = new JComboBox();
cb.setEditable(true);
cb.addItem("初级");
cb.addItem("中级");
cb.addItem("高级");
cb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int index = cb.getSelectedIndex();
System.out.println(index);
switch(index)
{
case 0:
mp.setMinenum(10);
mp.setRC(9,9);
break;
case 1:
mp.setMinenum(40);
mp.setRC(16,16);
break;
case 2:
mp.setMinenum(99);
mp.setRC(30,16);
break;
}
}
}
);