我正在制作这款益智游戏,它会从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){
}
}
}
感谢您的帮助提前
最简单的方法是在你的'ReplayHandler'中引用你的游戏,并使用一个特定的方法,例如'game.restartGame()' – 2015-04-01 10:20:17