import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Frame
extends JFrame {
JTextField text,text1;
JLabel nowBomb, setBomb,mianji;
int BombNum, BlockNum=81; // 当前雷数,当前方块数
int rightBomb, restBomb, restBlock; // 到的地雷数,剩余雷数,剩余方块数
JButton start = new JButton("中级"); //开始按钮
JButton start1 = new JButton("初级");
JButton start2 = new JButton("自定义");
JButton start3 = new JButton("开始");
JPanel MenuPamel = new JPanel(); //菜单面板
JPanel bombPanel = new JPanel(); //方块面板
Bomb[][] bombButton;//雷类数组
int k=1;
JPanel c; //主面板
BorderLayout borderLayout1 = new BorderLayout(); //边界布局
GridLayout gridLayout1 = new GridLayout(); //网格布局
public Frame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();
}
}
/*主面板属性设置*/
public void jbInit() throws Exception {
c = (JPanel) getContentPane();
setTitle("扫雷");
c.setBackground(Color.WHITE);
MenuPamel.setBackground(Color.YELLOW);
c.setLayout(borderLayout1);
if(BlockNum==81){
setSize(new Dimension(450, 450));
BombNum = 10;
}
setResizable(false);
//方块数
//雷数
if(k==0||k==2)
{
if(k==0){
text = new JTextField("196", 3); //雷数文本显示
text1 = new JTextField("20", 2);
k=2;
}
BombNum=Integer.Text().trim());
BlockNum=Integer.Text().trim());
if (BombNum>BlockNum||BlockNum>225) {
JOptionPane.showMessageDialog(null, "您设置的雷太多或面积过大,请重设!", "错误",
JOptionPane.ERROR_MESSAGE);
BombNum=20;
BlockNum=196;
}
nowBomb = new JLabel("当前雷数" + ":" + BombNum);
setBomb = new JLabel("雷数"+":");
mianji=new JLabel("面积"+":");
setSize(new Dimension(50*(int) Math.sqrt(BlockNum), 50*(int) Math.sqrt(BlockNum)));
MenuPamel.add(setBomb);
MenuPamel.add(text1);
MenuPamel.add(mianji);
MenuPamel.add(text);
MenuPamel.add(start3);
MenuPamel.add(start);
MenuPamel.add(start1);
MenuPamel.add(nowBomb);
}
start.addActionListener(new Frame1_start_actionAdapter(this));
start1.addActionListener(new Frame1_start_actionAdapter(this));
start2.addActionListener(new Frame1_start_actionAdapter(this));
start3.addActionListener(new Frame1_start_actionAdapter(this));//开始按钮
/*增加菜单*/if(k==1){
nowBomb = new JLabel("当前雷数" + ":" + BombNum);
setBomb = new JLabel("雷数"+":"+BombNum);
MenuPamel.add(setBomb);
MenuPamel.add(start1);
MenuPamel.add(start);
MenuPamel.add(start2);
MenuPamel.add(nowBomb);
}
c.add(MenuPamel, java.awt.Border
Layout.NORTH);//菜单加入位置
/*添加方块*/
bombPanel.setLayout(gridLayout1);
gridLayout1.setColumns( (int) Math.sqrt(BlockNum)); //(int) Math.sqrt(BlockNum)是总方块数的平方根
gridLayout1.setRows( (int) Math.sqrt(BlockNum));
bombButton = new Bomb[ (int) Math.sqrt(BlockNum)][ (int) Math.sqrt(BlockNum)];
for (int i = 0; i < (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j < (int) Math.sqrt(BlockNum); j++) {
bombButton[i][j] = new Bomb(i, j);
//bombButton[i][j].setSize(10, 10);
bombButton[i][j].setFont(new Font("", Font.PLAIN, 14)); //设置字体大小
bombButton[i][j].setForeground(Color.white);
bombButton[i][j].addMouseListener(new Bomb_mouseAdapter(this));
bombButton[i][j].addActionListener(new Bomb_actionAdapter(this));
bombPanel.add(bombButton[i][j]);
}
}
c.add(bombPanel, java.awt.BorderLayout.CENTER); //方块加入位置
startBomb();
//开始布雷
}
public void zhongji() throws Exception{
setSize(new Dimension(600, 600));
BlockNum=144;
BombNum = 30;
k=1;
jbInit();
}
public void chuji() throws Exception{
setSize(new Dimension(300, 300));
BlockNum=81;
BombNum =10;
k=1;
jbInit();
}
public void zidingyi() throws Exception{
k=0;
jbInit();
}
/* 开始,布雷 */
public void startBomb() {
nowBomb.setText("当前雷数" + ":" + BombNum);
for (int i = 0; i < (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j < (int) Math.sqrt(BlockNum); j++) {
bombButton[i][j].isBomb = false;
bombButton[i][j].isClicked = false;
bombButton[i][j].isRight = false;
bombButton[i][j].BombFlag = 0;
bombButton[i][j].BombRoundCount = 9;
bombButton[i][j].setEnabled(true); //点击按钮不再使用
bombButton[i][j].setText("");
bombButton[i][j].setFont(new Font("", Font.PLAIN, 14));//设置字体大小
bombButton[i][j].setForeground(Color.BLUE);
rightBomb = 0;
restBomb = BombNum;
restBlock = BlockNum - BombNum;
}
}
for (int i = 0; i < BombNum; ) {
int x = (int) (Math.random() * (int) (Math.sqrt(BlockNum) - 1));
int y = (int) (Math.random() * (int) (Math.sqrt(BlockNum) - 1));
if (bombButton[x][y].isBomb != true) {
bombButton[x][y].isBomb = true;
i++;
}
}
CountRoundBomb();
}
/* 计算方块周围雷数 */
public void CountRoundBomb() {
for (int i = 0; i < (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j < (int) Math.sqrt(BlockNum); j++) {
int count = 0;
// 当需要检测的单元格本身无地雷的情况下,统计周围的地雷个数
if (bombButton[i][j].isBomb != true) {
for (int x = i - 1; x < i + 2; x++) {
for (int y = j - 1; y < j + 2; y++) {
if ( (x >= 0) && (y >= 0)
&& (x < ( (int) Math.sqrt(BlockNum)))
&& (y < ( (int) Math.sqrt(BlockNum)))) {
if (bombButton[x][y].isBomb == true) {
count++;
}
}
}
}
bombButton[i][j].BombRoundCount = count;
}
}
}
}
/
* 是否挖完了所有的雷 */
public void isWin() {
restBlock = BlockNum - BombNum;
for (int i = 0; i < (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j < (int) Math.sqrt(BlockNum); j++) {
if (bombButton[i][j].isClicked == true) {
restBlock--;
}
}
}
if (rightBomb == BombNum || restBlock == 0) {
JOptionPane.showMessageDialog(this, "您挖完了所有的雷,您胜利了!", "胜利",
JOptionPane.INFORMATION_MESSAGE);
startBomb();
}
}
/*当选中的位置为空,则翻开周围的地图*/
public void isNull(Bomb ClickedButton) {
int i, j;
i = ClickedButton.num_x;
j = ClickedButton.num_y;
for (int x = i - 1; x < i + 2; x++) {
for (int y = j - 1; y < j + 2; y++) {
if ( ( (x != i) || (y != j)) && (x >= 0) && (y >= 0)
&& (x < ( (int) Math.sqrt(BlockNum)))
&& (y < ( (int) Math.sqrt(BlockNum)))) {
if (bombButton[x][y].isBomb == false
&& bombButton[x][y].isClicked == false
&& bombButton[x][y].isRight == false) {
turn(bombButton[x][y]);
}
简单的java游戏代码}
}
}
}
/* 翻开 */
public void turn(Bomb ClickedButton) {
ClickedButton.setEnabled(false); //点击按钮不再使用
ClickedButton.isClicked = true;
if (ClickedButton.BombRoundCount > 0) {
ClickedButton.setText(ClickedButton.BombRoundCount + "");
}
else {
isNull(ClickedButton);
}
}
/* 左键点击 */
public void actionPerformed(ActionEvent e) {
if ( ( (Bomb) e.getSource()).isClicked == false
&& ( (Bomb) e.getSource()).isRight == false) {
if ( ( (Bomb) e.getSource()).isBomb == false) {
turn( ( (Bomb) e.getSource()));
isWin();
}
else {
for (int i = 0; i < (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j < (int) Math.sqrt(BlockNum); j++) {
if (bombButton[i][j].isBomb == true) {
bombButton[i][j].setText("雷");
}
}
}
( (Bomb) e.getSource()).setForeground(Color.RED);
( (Bomb) e.getSource()).setFont(new Font("", Font.BOLD, 20));
( (Bomb) e.getSource()).setText("X");
JOptionPane.showMessageDialog(this, "你踩到地雷了,按确定重来", "踩到地雷", 2);
startBomb();
}
}
}
/* 右键点击 */
public void mouseClicked(MouseEvent e) {
Bomb bombSource = (Bomb) e.getSource();
boolean right = SwingUtilities.isRi
ghtMouseButton(e);//??
if ( (right == true) && (bombSource.isClicked == false)) {
bombSource.BombFlag = (bombSource.BombFlag + 1) % 3;
if (bombSource.BombFlag == 1) {
if (restBomb > 0) {
bombSource.setForeground(Color.RED);
bombSource.setText("旗");
bombSource.isRight = true;
restBomb--;
}
else {
bombSource.BombFlag = 0;
}
}
else if (bombSource.BombFlag == 2) {
restBomb++;
bombSource.setText("?");
bombSource.isRight = false;
}
else {
bombSource.setText("");
}
if (bombSource.isBomb == true) {
if (bombSource.BombFlag == 1) {
rightBomb++;
}
else if (bombSource.BombFlag == 2) {
rightBomb--;
}
}
nowBomb.setText("当前雷数" + ":" + restBomb);
isWin();
}
}
public static void main(String[] args) {
Frame frame = new Frame();
frame.setVisible(true);
}
}
class Frame1_start_actionAdapter
implements ActionListener {
private Frame adaptee;
Frame1_start_actionAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
//??
public void actionPerformed(ActionEvent e) {
veAll();
veAll();
Object obj = e.getSource();
if(obj==adaptee.start){
try {
adaptee.zhongji();
} catch (Exception e1) {
e1.printStackTrace();
}//响应button事件
}
if(obj==adaptee.start2){
try {
adaptee.zidingyi();
} catch (Exception e1) {
e1.printStackTrace();
}
}
if(obj==adaptee.start1){
try {
adaptee.chuji();
} catch (Exception e1) {
e1.printStackTrace();
}//响应button事件
}
if(obj==adaptee.start3){
try {
adaptee.jbInit();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
private void zhongji() {
// TODO Auto-generated method stub
}
}
////////////////////////////
class Bomb
extends JButton {
int num_x, num_y; // 第几号方块
int BombRoundCount; // 周围雷数
boolean isBomb; // 是否为雷
boolean isClicked; // 是否被点击
int BombFlag; // 探雷标记
boolean isRight; // 是否点击右键
public Bomb(int x, int y) {
num_x = x;
num_y = y;
BombFlag = 0;
BombRoundCount = 9;
isBomb = false;
isClicked = false;
isRight = false;
}
}
class Bomb_actionAdapter
implements ActionListener {
private Frame adaptee;
Bomb_actionAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.actionPerformed(e);
}
}
class Bomb_mouseAdapter
extends MouseAdapter {
private Frame adaptee;
Bomb_mouseAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
public void mouseClicke
d(MouseEvent e) {
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论