java - Provoke an interruption in a main logic loop -
i making puzzle game wich receives letter keyboard scannner(system.in) , changes puzzle state.
now, making gui game. have jpanel jlabels wich shows puzzle , jlabel 2 jbuttons, 1 it's exit button implemented , has replay button. replay button has simulate restart of whole game. question how can stop mainloop after press replay button? there system call can use when click button generates interruption on main loop?
here main logic loop:
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; }
and here 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 sure want 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 new game?", "", 0, 0, null, holder, 0)==0){ } } }
thanks in advance
my question how can stop mainloop after press replay button? there system call can use when click button generates interruption on main loop?
you don't. don't use same type of "game loop" program structure use linear console program event driven gui, wouldn't use new scanner(system.in)
swing gui. rather change state of game when buttons pressed. time you'd use game loop if needed animation of sort, doesn't seem case here.
as side recommendation, avoid method names func1()
, func2()
. when come debug code 6 months now, wondering heck these do. instead, give variables , methods names make sense, names make code self-commenting.
Comments
Post a Comment