2015-04-01 36 views
0

我正在制作这款益智游戏,它会从Scannner(System.in)收到键盘的一封信,它会更改拼图状态。在主逻辑循环中唤醒中断

现在,我正在为游戏制作一个gui。我有一个带有JLabels的JPanel,它显示了这个难题,另一个Jlabel有两个JButton,一个是已经实现的退出按钮,另一个必须是重放按钮。这个Replay按钮必须模拟整个游戏的重新启动。我的问题是如何在按下Replay按钮后立即停止主循环? 当我点击按钮时,是否有任何系统调用可以使用,以便在主循环中生成中断?

这里是主逻辑循环:

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; 
} 

这里是GUI:

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){ 
     } 
    } 

} 

感谢您的帮助提前

+0

最简单的方法是在你的'ReplayHandler'中引用你的游戏,并使用一个特定的方法,例如'game.restartGame()' – 2015-04-01 10:20:17

回答

1

我的问题是如何阻止在我按下回放按钮后马上回滚?当我点击按钮时,是否有任何系统调用可以用来在主循环中生成中断?

你不知道。您不会像使用事件驱动的GUI一样使用线性控制台程序使用的相同类型的“游戏循环”程序结构,就像不使用带有Swing GUI的new Scanner(System.in)一样。相反,当按下按钮时,你会改变游戏的状态。如果你需要某种类型的动画,那么你只能使用游戏循环的时候,这似乎并不是这种情况。

作为侧面推荐,请避免方法名称,如func1(),func2()。当你从现在起6个月后回来调试这个代码时,你会想知道这些代码做了什么。相反,请给出您的变量和方法名称,使代码自我评论。