Design and implementation of maze game based on JavaSwing

Table of Contents
1 Problem Description 1

  1. 1 Course Design Topic 1

  2. 2 Curriculum Design Requirements 1
    2 Requirements Analysis 2
    3 Outline Design 3

  3. 1 Program interface design 3

  4. 2 Overall Functional Framework 3
    Four Detailed Design 5

  5. 1 Data structure design 5

  6. 2 Specific module design 6

    1. 1 game module 6
    1. 2 File operation module 9
    1. 3 Editing module 9
    1. 4 Path prompt module 9
    1. 5 Help module 11
      5 Debugging and running analysis 12
  7. 1 Comparison of time and space efficiency between the classic maze shortest path algorithm and the new algorithm of this program 12

  8. 2 Demonstration of program operation effect 13
    6 Course design summary 18
    7 References 19
    2 Requirements analysis

    The maze game, as a classic game, has a dazzling array of programs related to it. In this case, if you want the program you designed to be interesting, you cannot rely solely on a black console. You must at least have a decent graphical interface and simple and convenient operations.
    After just learning C++ last semester, I completed the course design with a graphical interface through self-study MFC. While learning data structure this semester, I also learned another important high-level language - java. Therefore, I also wanted to try my hand at java's graphical interface design.
    In order to meet the requirements in the question, if implemented in Java, it is obviously necessary to use its graphical interface, event-driven, multi-threading and other knowledge. The first two parts of the textbook can basically help me complete the relevant design. The knowledge of multi-threading is not in the textbook, but I can consult relevant information (including paper books in the library and various forum information on the Internet). It's also easy to find ways to set time limits.
    The problem of Java tools has been solved. After all, the masters have designed these tools to be very convenient and beautiful, and of course they are not too difficult to use. Therefore, what is left is the core part - the important algorithms that truly realize the programming requirements.
    It is inconvenient that users can only design their own mazes to play. Of course, this function cannot be eliminated, but I want to add a function to generate random mazes while retaining this function. Of course, this kind of randomness cannot be simply calling the random method provided by java, so it is difficult to ensure that there is a path in the maze obtained. If you want to design a random maze with at least one path, you naturally need to use a special and clever algorithm. In addition, finding the shortest path also requires good algorithm design. If the time complexity of the algorithm is too high, users will be impatient to wait.

//迷宫界面


import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.RandomAccessFile;

import javax.swing.*;

@SuppressWarnings("serial")
public class map extends JFrame implements ActionListener, KeyListener, Runnable {
    
    
    static int m, n;
    static wrmPane[][] tp = null;        //显示动画,同一包内类均可访问

    //时间限制
    static Thread timeThread;                //时间控制线程
    static int timelimit, remaintime;
    static JPanel timePanel = new JPanel() {
    
         //剩余时间显示面板
        public void paintComponent(Graphics g) {
    
    
            super.paintComponent(g);
            String rt;
            if (timelimit == 0) {
    
    
                rt = "无限制";
                setForeground(Color.GREEN);    //青色表示无时间限制
            } else {
    
    
                rt = remaintime / 3600 + " : " + (remaintime - (remaintime / 3600) * 3600) / 60 + " : " + remaintime % 60;
                if (remaintime > 10)
                    setForeground(Color.BLUE);      //剩余时间充足时为绿色
                else
                    setForeground(Color.RED);      //剩余时间很少时为红色
            }
            g.drawString("剩余时间:  " + rt, 220, 16);
        }
    };

    // 菜单项
    private JMenuItem m_start = new JMenuItem("开始新游戏(S)");
    private JMenuItem m_time = new JMenuItem("游戏时间限制(L)");
    private JMenuItem m_return = new JMenuItem("返回主界面(R)");
    private JMenuItem m_exit = new JMenuItem("退出游戏(Q)");
    private JMenuItem m_savefile = new JMenuItem("保存迷宫结构(W)");
    private JMenuItem m_importfile = new JMenuItem("导入迷宫结构(I)");
    private JMenuItem m_selfconfig = new JMenuItem("编辑当前迷宫(E)");
    private JMenuItem m_randommake = new JMenuItem("随机生成迷宫(Z)");
    private JMenuItem m_sortpath = new JMenuItem("显示最短路径(T)");
    private JMenuItem m_DFSpath = new JMenuItem("随意显示一个路径(K)");
    private JMenuItem m_help = new JMenuItem("游戏使用说明(H)");
    private JMenuItem m_about = new JMenuItem("关于迷宫游戏(A)");


    @SuppressWarnings("deprecation")
    map(int x, int y) {
    
    
        m = x;
        n = y;
        tp = new wrmPane[m][n];
        timelimit = remaintime = 0;                //初始化时,时间为0,代表没有时间限制

        timeThread = new Thread(this);
        timeThread.start();
        timeThread.suspend();

        //菜单
        JMenu game = new JMenu("游戏");
        JMenu file = new JMenu("文件");
        JMenu edit = new JMenu("编辑");
        JMenu tip = new JMenu("提示");
        JMenu help = new JMenu("帮助");
        game.add(m_start);
        game.add(m_time);
        game.add(m_return);
        game.add(m_exit);
        file.add(m_savefile);
        file.add(m_importfile);
        edit.add(m_selfconfig);
        edit.add(m_randommake);
        tip.add(m_sortpath);
        tip.add(m_DFSpath);
        help.add(m_help);
        help.add(m_about);

        //菜单栏
        JMenuBar menu = new JMenuBar();
        menu.add(game);
        menu.add(file);
        menu.add(edit);
        menu.add(tip);
        menu.add(help);

        //初始化迷宫组件,并生成随机路径
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++) {
    
    
                tp[i][j] = new wrmPane();
            }
        Operations.creatMaze();  //深度优先遍历生成至少有一条随机通道的迷宫

        //迷宫地图
        JPanel mazePane = new JPanel();
        mazePane.setLayout(new GridLayout(m, n, 0, 0));
        for (int i = 0; i < m; i++) {
    
    
            for (int j = 0; j < n; j++) {
    
    
                mazePane.add(tp[i][j]);
            }
        }

        //菜单和时间显示放在同一面板上
        JPanel northPanel = new JPanel();
        northPanel.setLayout(new GridLayout(1, 1));
        northPanel.add(menu);
        northPanel.add(timePanel);
        timePanel.setBackground(new Color(245, 240, 245));
        menu.setBackground(new Color(245, 240, 245));

        //添加到框架
        setLayout(new BorderLayout());
        add(northPanel, BorderLayout.NORTH);
        add(mazePane, BorderLayout.CENTER);
        add(new JPanel(), BorderLayout.SOUTH);

        //注册监听器
        m_start.addActionListener(this);
        m_time.addActionListener(this);
        m_return.addActionListener(this);
        m_exit.addActionListener(this);
        m_savefile.addActionListener(this);
        m_importfile.addActionListener(this);
        m_selfconfig.addActionListener(this);
        m_randommake.addActionListener(this);
        m_sortpath.addActionListener(this);
        m_DFSpath.addActionListener(this);
        m_help.addActionListener(this);
        m_about.addActionListener(this);
        addKeyListener(this);

        //基本设置
        setTitle("迷宫游戏");
        setSize(850, 650);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setResizable(false);

    }

    map() {
    
    
        this(25, 25);
    }

Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here

Guess you like

Origin blog.csdn.net/newlw/article/details/132868311