Java 编写点灯游戏 窗体程序 完整源码

今天为大家分享电灯游戏的开发与制作,目前系统已经完成了初步功能,后续会进一步完善。整个系统界面漂亮,有完整得源码,希望大家可以喜欢。喜欢的帮忙点赞和关注。一起编程、一起进步

开发环境

开发语言为Java,开发环境Eclipse或者IDEA都可以。运行主程序,或者执行打开JAR文件即可以运行本程序。

系统框架

利用JDK自带的SWING框架开发,不需要安装第三方JAR包。纯窗体模式,直接运行Main文件即可以。同时带有详细得设计文档。

主要功能

游戏运行规则

一、点击游戏----》开始,启动游戏主界面;

二、主界面中的方格相当于一盏盏的灯,但现在都是黑色的——即灯全灭了。如果一盏灯是白色的,那么就表示这盏灯是亮的。

三、每当改变一盏灯的状态(点亮或熄灭)时,它附近(上下左右及本身)的灯的状态也随之改变。游戏任务:点亮所有的灯。

四 当所有游戏都完成的时候,即表示当前关已经通过

游戏主要功能

1 游戏总共分为5局,每一局都通关,表示游戏完成

2 在游戏的任何过程中都可以点重新开始按钮,重新开始按钮;或者点关闭按钮,点击退出程序

系统运行效果

关键代码

package diandeng;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.URL;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.Line;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * 
 * @author 斌斌
 * 
 */

public class Lighting extends JFrame
{
    private Container c;

    private int level;

    private final int TOTALLEVEL = 5;//游戏总关卡数

    private final int WIDTH, HEIGHT;//当前屏幕的大小(宽度、高度)

    private JButton buttons[][];

    private boolean buttonValue[][];

    static Icon light, unlight;//灯亮和灯灭时的图标

    private JMenuItem exit;

    private JPanel centerPanel = new JPanel(), southPanel = new JPanel();

    private JButton gameState, newGameButton, exitButton;

    private boolean isWin;

    private ButtonHandler lis;

    public Lighting()
    {
        super("点灯");
        
        addBgMusic();

        c = getContentPane();
        c.setVisible(false);

        level = 1;
        centerPanel.setLayout(new GridLayout(level + 2, level + 2));
        southPanel.setLayout(new GridLayout(1, 2));

        JMenuBar menu = new JMenuBar();//菜单栏

        JMenu game = new JMenu("游戏(G)");
        game.setMnemonic('G');
        JMenuItem start = new JMenuItem("开始(S)");
        start.setMnemonic('S');
        start.addActionListener(new startListener());
        exit = new JMenuItem("关闭(W)");
        exit.setMnemonic('W');
        exit.setEnabled(false);
        exit.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                exit.setEnabled(false);
                c.setVisible(false);
            }
        });
        game.add(start);
        game.add(exit);

        JMenu help = new JMenu("帮助(H)");
        help.setMnemonic('H');
        JMenuItem instruction = new JMenuItem("游戏说明(I)");
        instruction.setMnemonic('I');
        instruction.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                JOptionPane.showMessageDialog(null, "一、点击\"游戏\" \"开始\"启动游戏主界面;"
                        + "\n二、主界面中的方格相当于一盏盏的灯,但现在都是黑色的——即灯全灭了。"
                        + "如果一盏灯是白色的,那么就表示这盏灯是亮的。" + "\n三、每当改变一盏灯的状态(点亮或熄灭)时,"
                        + "它附近(上下左右及本身)的灯的状态也随之改变。" + "\n\n游戏任务:点亮所有的灯。");
            }
        });
        JMenuItem aboutUs = new JMenuItem("关于我们(U)");
        aboutUs.setMnemonic('U');
        aboutUs.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                JOptionPane.showMessageDialog(null, "QQ:2497737951,帮写代码网!\n");
            }
        });
        JMenuItem aboutGame = new JMenuItem("关于点灯(G)");
        aboutGame.setMnemonic('G');
        aboutGame.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                JOptionPane.showMessageDialog(null,"                                 点灯\n\n"
                + "版本:1.3\n公告:本计算机程序是个人作品,在保留\n作者的前提下,您可以自由传播本程序。");
            }
        });
        help.add(instruction);
        help.addSeparator();
        help.add(aboutUs);
        help.add(aboutGame);

        menu.add(game);
        menu.add(help);
        setJMenuBar(menu);

        gameState = new JButton("第 " + level + " 局,共 " + TOTALLEVEL + " 局");
        southPanel.add(gameState);
        newGameButton = new JButton("重开(N)");
        newGameButton.setMnemonic('N');
        southPanel.add(newGameButton);
        newGameButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent arg0)
            {
                for (int i = 0; i < buttonValue.length; i++)
                {
                    for (int j = 0; j < buttonValue[i].length; j++)
                    {
                        buttonValue[i][j] = false;
                        buttons[i][j].setIcon(unlight);
                    }
                }
            }
        });

        exitButton = new JButton("关闭(W)");
        exitButton.setMnemonic('W');
        southPanel.add(exitButton);
        exitButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent arg0)
            {
                exit.setEnabled(false);
                c.setVisible(false);
            }
        });

        c.add(southPanel, BorderLayout.SOUTH);

        Toolkit tk = Toolkit.getDefaultToolkit();
        WIDTH = tk.getScreenSize().width;
        HEIGHT = tk.getScreenSize().height;
        Image img = tk.getImage(this.getClass().getClassLoader().getResource(
                "images/y.gif"));
        this.setIconImage(img);

        light = new ImageIcon(this.getClass().getClassLoader().getResource(
                "images/light.gif"));
        unlight = new ImageIcon(this.getClass().getClassLoader().getResource(
                "images/unlight.gif"));

        buttons = new JButton[((GridLayout) (centerPanel.getLayout()))
                .getRows()][((GridLayout) (centerPanel.getLayout()))
                .getColumns()];
        buttonValue = new boolean[buttons.length][buttons[0].length];

        lis = new ButtonHandler();
        for (int i = 0; i < buttons.length; i++)
        {
            for (int j = 0; j < buttons[i].length; j++)
            {
                buttons[i][j] = new JButton(unlight);
                buttonValue[i][j] = false;
                buttons[i][j].addActionListener(lis);
                centerPanel.add(buttons[i][j]);
            }
        }

        c.add(centerPanel);

        this.setResizable(false);
        this.setBounds(WIDTH / 2 - 300, HEIGHT / 2 - 300, 600, 600);// 窗体居中显示,大小为600*600
        this.setVisible(true);
    }

    /**
     * 添加背景音乐
     */
    private void addBgMusic()
    {
        URL soundURL = this.getClass().getClassLoader().getResource(
                "sounds/bgsound.wav");
        Line.Info linfo = new Line.Info(Clip.class);
        try
        {
            Line line = AudioSystem.getLine(linfo);
            Clip buhClip = (Clip) line;
            AudioInputStream ais = AudioSystem.getAudioInputStream(soundURL);
            buhClip.open(ais);
            buhClip.start();
        } catch (LineUnavailableException e1)
        {
            e1.printStackTrace();
        } catch (UnsupportedAudioFileException e1)
        {
            e1.printStackTrace();
        } catch (IOException e1)
        {
            e1.printStackTrace();
        }
    }

    private class startListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            exit.setEnabled(true);
            c.setVisible(true);
            centerPanel.removeAll();
            level = 1;
            // 初始化南部
            southPanel.removeAll();
            gameState = new JButton("第 " + level + " 局,共 " + TOTALLEVEL + " 局");
            southPanel.add(gameState);
            southPanel.add(newGameButton);
            southPanel.add(exitButton);
            c.add(southPanel, BorderLayout.SOUTH);

            // 初始化中部
            centerPanel.setLayout(new GridLayout((level - 1) % TOTALLEVEL + 3,
                    (level - 1) % TOTALLEVEL + 3));
            buttons = new JButton[((GridLayout) (centerPanel.getLayout()))
                    .getRows()][((GridLayout) (centerPanel.getLayout()))
                    .getColumns()];
            buttonValue = new boolean[buttons.length][buttons[0].length];
            for (int i = 0; i < buttons.length; i++)
            {
                for (int j = 0; j < buttons[i].length; j++)
                {
                    buttons[i][j] = new JButton(unlight);
                    buttonValue[i][j] = false;
                    buttons[i][j].addActionListener(lis);
                    centerPanel.add(buttons[i][j]);
                }
            }

            c.add(centerPanel);

            c.validate();//验证此容器及其所有子组件。
        }
    }
    
    /**
     * @author binbin
     * 
     * 按键监听器
     *
     */
    private class ButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            /**
             * 核心算法
             */
            for (int i = 0; i < buttons.length; i++)
            {
                for (int j = 0; j < buttons[i].length; j++)
                {
                    if (e.getSource() == buttons[i][j])
                    {
                        buttonValue[i][j] = changeValue(buttonValue[i][j]);
                        
                        if(i!=0)
                            buttonValue[i-1][j] = changeValue(buttonValue[i-1][j]);
                        if(j!=0)
                            buttonValue[i][j-1] = changeValue(buttonValue[i][j-1]);
                        if(i!=buttons.length-1)
                            buttonValue[i+1][j] = changeValue(buttonValue[i+1][j]);
                        if(j!=buttons[i].length-1)
                            buttonValue[i][j+1] = changeValue(buttonValue[i][j+1]);
                    }
                }
            }

            //判断是否获胜
            isWin = true;

            for (int i = 0; i < buttons.length; i++)
            {
                for (int j = 0; j < buttons[i].length; j++)
                {
                    if (buttonValue[i][j])
                    {
                        buttons[i][j].setIcon(light);
                    }
                    else
                    {
                        isWin = false;
                        buttons[i][j].setIcon(unlight);
                    }
                }
            }

            //如果获胜,弹出相应过关信息,增加游戏难度并初始化主界面
            if (isWin)
            {
                String passMessage = "";//过关信息
                
                for(int i=1;i<=TOTALLEVEL;i++)
                {
                    if(level==i)
                    passMessage = "恭喜您已经通过了第 " + i + " 关";
                }
                
                //弹出过关信息
                JOptionPane.showMessageDialog(null, passMessage, "消息",
                        JOptionPane.PLAIN_MESSAGE);

                for (int i = 0; i < buttonValue.length; i++)
                {
                    for (int j = 0; j < buttonValue[i].length; j++)
                    {
                        buttonValue[i][j] = false;
                        buttons[i][j].setIcon(unlight);
                    }
                }
                buttons[0][0].grabFocus();

                level = level % TOTALLEVEL + 1;

                // 初始化南部
                southPanel.removeAll();//移除南部面板中的所有组件。
                gameState = new JButton("第 " + level + " 局,共 " + TOTALLEVEL
                        + " 局");
                southPanel.add(gameState);
                southPanel.add(newGameButton);
                southPanel.add(exitButton);
                c.add(southPanel, BorderLayout.SOUTH);

                // 初始化中部
                centerPanel.removeAll();//移除中部面板中的所有组件。
                centerPanel.setLayout(new GridLayout((level - 1) % TOTALLEVEL
                        + 3, (level - 1) % TOTALLEVEL + 3));
                buttons = new JButton[((GridLayout) (centerPanel.getLayout()))
                        .getRows()][((GridLayout) (centerPanel.getLayout()))
                        .getColumns()];
                buttonValue = new boolean[buttons.length][buttons[0].length];
                for (int i = 0; i < buttons.length; i++)
                {
                    for (int j = 0; j < buttons[i].length; j++)
                    {
                        buttons[i][j] = new JButton(unlight);
                        buttonValue[i][j] = false;
                        buttons[i][j].addActionListener(lis);
                        centerPanel.add(buttons[i][j]);
                    }
                }

                c.add(centerPanel);
            }
            c.validate();
        }
    }

    private boolean changeValue(boolean value)
    {
        return value ? false : true;
    }

    //程序入口
    public static void main(String args[])
    {
        //创建Lighting对象
        Lighting l = new Lighting();

        //设置窗体的关闭方式
        l.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        l.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                int eValue = JOptionPane.showConfirmDialog(null, "你确定要退出吗?",
                        "关闭提示", JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);
                if (eValue == JOptionPane.OK_OPTION)
                {
                    System.exit(1);// 与此等效:Runtime.getRuntime().exit(n)
                }
            }
        });
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

计算机程序

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值