扫雷游戏(主界面)

package cleanMine; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JCheckBoxMenuItem; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; /** * 扫雷游戏主界面 * @author tony.tang * */ public class CleanMine extends JFrame implements ActionListener{ private JLabel text1,text2; public static JLabel remainMine;//剩余地雷数 private JLabel time;//消耗时间 private JButton reset;//重新开始 private JPanel center; private int row,col,mine; public CleanMine(){ text1=new JLabel("剩余地雷:"); text2=new JLabel("消耗时间:"); remainMine=new JLabel("10"); time=new JLabel("0"); reset=new JButton("重新开始"); reset.addActionListener(this); JMenuBar bar=new JMenuBar(); JMenu game=new JMenu("游戏"); JMenu help=new JMenu("帮助"); JMenuItem item; game.add(item=new JMenuItem("开局"));item.addActionListener(this); game.addSeparator(); ButtonGroup bg=new ButtonGroup(); game.add(item=new JCheckBoxMenuItem("初级",true));bg.add(item);item.addActionListener(this); game.add(item=new JCheckBoxMenuItem("中级"));bg.add(item);item.addActionListener(this); game.add(item=new JCheckBoxMenuItem("高级"));bg.add(item);item.addActionListener(this); game.add(item=new JCheckBoxMenuItem("自定义..."));bg.add(item);item.addActionListener(this); game.addSeparator(); game.add(item=new JMenuItem("退出"));item.addActionListener(this); help.add(item=new JMenuItem("查看帮助"));item.addActionListener(this); help.add(item=new JMenuItem("关于扫雷..."));item.addActionListener(this); bar.add(game); bar.add(help); this.setJMenuBar(bar); init(); } private void init(){ JPanel north=new JPanel(); north.add(text1); north.add(remainMine); north.add(reset); north.add(text2); north.add(time); this.add(north,BorderLayout.NORTH); this.row=9; this.col=9; this.mine=10; restart(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); new Thread(){ public void run(){ while(Integer.parseInt(remainMine.getText())>0){ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } time.setText((Integer.parseInt(time.getText())+1)+""); } } }.start(); } public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("初级")){ this.row=9; this.col=9; this.mine=10; restart(); return; } if(e.getActionCommand().equals("中级")){ this.row=16; this.col=16; this.mine=40; restart(); return; } if(e.getActionCommand().equals("高级")){ this.row=16; this.col=30; this.mine=99; restart(); return; } if(e.getActionCommand().equals("重新开始")){ restart(); return; } } private void restart(){ if(center!=null){ this.remove(center); } center=new AllButtonPanel(row,col,mine); this.add(center,BorderLayout.CENTER); this.remainMine.setText(mine+""); this.time.setText("0"); this.setSize(col*30,row*30+10); this.setResizable(false); this.setVisible(true); } public static void main(String[] args) { new CleanMine(); } }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以给您提供一些思路和步骤: 1. 首先,您需要设计游戏的界面,可以使用Java的Swing或JavaFX来实现。您可以创建一个JFrame窗口,并添加JPanel面板作为游戏的界面。 2. 接下来,您需要设计游戏的逻辑。您可以定义一个二维数组来表示游戏面板,每个元素代表一个格子,包含以下属性: - 是否被揭开(isRevealed) - 是否被标记为地雷(isFlagged) - 是否是地雷(isMine) - 附近的地雷数量(numOfNeighbours) 3. 实现游戏的初始化。在游戏开始前,您需要随机生成一些地雷,并将它们分布在游戏面板上。同时,您需要计算每个格子周围的地雷数量。 4. 实现游戏的要逻辑。当玩家点击一个格子时,您需要根据当前格子的状态来执行不同的操作,例如: - 如果当前格子已经被揭开,则不做任何处理; - 如果当前格子被标记为地雷,则取消标记; - 如果当前格子没有被揭开,也没有被标记为地雷,则揭开该格子,并根据需要递归揭开周围的格子。 5. 实现游戏结束的判断。当玩家揭开一个地雷时,游戏结束,您需要显示游戏结束的界面,并提供重新开始游戏的选项。 6. 最后,您需要实现一些辅助功能,例如: - 计时器:记录玩家完成游戏所用的时间; - 雷计数器:显示玩家标记的地雷数量; - 按钮:提供重新开始游戏的选项。 希望这些步骤可以帮助您完成扫雷游戏的设计和实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值