0

我正在制作这个益智游戏,它使用 Scanner(System.in) 从键盘接收一封信,它会改变拼图状态。

现在,我正在为游戏制作一个 GUI。我有一个带有 JLabels 的 JPanel 显示拼图,另一个带有 2 个 JButton 的 JLabel,一个是已实现的退出按钮,另一个必须是重播按钮。这个重播按钮必须模拟整个游戏的重新开始。我的问题是如何在按下重放按钮后立即停止主循环?

当我单击按钮时,是否可以使用任何系统调用以在主循环上产生中断?

这是主要的逻辑循环:

    public static void main(String args[]){

    Cliente i=new Cliente();

    int labSize;
    labSize=i.func1();

    boolean b1;
    b1=i.func2();
    boolean b2;
    b2=i.func3();

    int nDarts;
    nDarts=i.func4();

    int nDrags;
    nDrags=i.func5();

    Game g=new Game(labSize,nDarts,nDrags);

    g.boolInit(b1, b2);

    g.initGame();

    i.setTab(g.getLab(), g.getLabSize());

    do{
        g.updateGame();

        i.setTab(g.getLab(), g.getLabSize());
        i.showTab();

        g.moveHeroObj(i);

        g.firingDartsLogic(i);

        g.weaponCaught();
        g.shieldCaught();
        g.dartLogic();

        g.flamesLogic();
        if(!g.didGameEnd()){
            g.dragonKillLogic();
            g.dragonLogic();
        }

        g.updateHeroSymbol();
        g.updateDragonSymbol();

        if(g.finishedGame())
            g.changeEndBool();

    }while(!g.didGameEnd());

    g.updateGame();

    i.setTab(g.getLab(), g.getLabSize());
    i.showTab();

    i.exitFunc();
    return;
}

这是图形用户界面:

public GameGUI(int n) {
    labSize=n;

    mainFrame = new JFrame("Maze Game");
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainFrame.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
    mainFrame.setVisible(false);
    
    choicesPanel=new JPanel();
    choicesPanel.setLayout(new GridLayout(0, 1));

    labPanel=new JPanel(new GridLayout(labSize,labSize));
    
    replayButton=new JButton("Replay");
    replayButton.setAlignmentY(Component.TOP_ALIGNMENT);
    replayButton.setAlignmentX(Component.LEFT_ALIGNMENT);
    replayButton.addActionListener(new ReplayHandler());
    
    exitButton=new JButton("Exit");
    exitButton.setAlignmentY(Component.TOP_ALIGNMENT);
    exitButton.setAlignmentX(Component.LEFT_ALIGNMENT);
    exitButton.addActionListener(new ExitHandler());
    
    choicesPanel.add(replayButton);
    choicesPanel.add(exitButton);
    
    mainFrame.getContentPane().add(choicesPanel);
    mainFrame.getContentPane().add(labPanel);
}

private class ExitHandler implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        String[] holder={"Yes","Cancel"};
        if(JOptionPane.showOptionDialog(null, "Are you sure you want to exit?", "", 0, 0, null, holder, 0)==0){
            System.exit(0);
        }
    }
    
}

private class ReplayHandler implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        String[] holder={"Yes","No"};
        if(JOptionPane.showOptionDialog(null, "Start a new game?", "", 0, 0, null, holder, 0)==0){
        }
    }
    
}
4

1 回答 1

1

我的问题是如何在按下重放按钮后立即停止主循环?当我单击按钮时我可以使用任何系统调用以便它在主循环上产生中断?

你没有。您不会使用用于线性控制台程序的相同类型的“游戏循环”程序结构,因为您不会使用事件驱动的 GUI,就像您不会使用new Scanner(System.in)Swing GUI 一样。相反,您会在按下按钮时更改游戏的状态。唯一使用游戏循环的情况是您需要某种动画,这里似乎并非如此。

作为附带建议,请避免使用 , 之类的方法func1()名称func2()。当你在 6 个月后回来调试这段代码时,你会想知道这些到底是做什么的。相反,给你的变量和方法起有意义的名字,让代码自我注释的名字。

于 2015-04-01T10:20:01.300 回答