java - Text based game with a separate timer loop? -
i have started java coding short course @ university 5 months ago. have learnt quite amount of things regards java coding, still lacking in other things such threads, handling exceptions, or making jframe
games. decided embark on text based game learn , figure out how game loop should work (kind of), , how logic should work (still, "kind of"). game wrote runs if-else
commands, displayed screen, type in command of option want pick, , bumps next menu, standard of course. run these if-else
statements within nested for
loops.
my nested for
loops looks following:
// example, they're lot more cluttered // in actual source code. mainmenu.writeoutput(); reply = keyboardinput.nextline(); (int = 0; <= 10; i--) { (int ii = 0; <= 10; i--) { if (reply.equalsignorecase("/help") { system.out.println("here have separate call class file (excuse me, forgot exact wording), call help.writeoutput(); display menu"); reply = keyboardinput.nextline(); if (reply.equalsignorecase("/makegameeasy") { // example. gamedifficultyeasy.writeoutput(); reply = keyboardinput.nextline(); if (reply.equalsignorecase("/back") { mainmenu.writeoutput(); reply = keyboardinput.nextline(); break; } } else if (reply.equalsignorecase("/makegamedifficult") { // example. gamedifficulthard.writeoutput(); reply = keyboardinput.nextline(); if (reply.equalsignorecase("/back") { mainmenu.writeoutput(); reply = keyboardinput.nextline(); break; } } else if (reply.equalsignorecase("/back") { mainmenu.writeoutput(); reply = keyboardinput.nextline(); break; } } else { system.out.println("here print out error incorrect input received, standard fare."); mainmenu.writeoutput(); reply = keyboard.nextline(); break; } } }
as mentioned, above example, it's not elegant, , can use exceptions incorrect info submitted user, not know of exceptions comfortably add them, i'll @ later time, main issue @ moment part of game "resource mining" has done on regular intervals. have been on google, still can't quite catch how set thread
or timer
game mining automatically, , player can go on game.
the game 1 of games build base, upgrade mining tools, , generate more "stuff". have pasted few blocks of code "mining" class file below run how of 1 thing should mined. in game able buy upgrades of course, factored mining speed.
// initiate variables lot earlier, // clarity, have initiated variables in below methods, // not work correctly anyway, aware of that, // didn't want add other "get, set , initiate" // variables , methods everywhere, not spam block of code. // upgradeos, upgradehf, , upgradelm have own respective // set , methods. int variables. public void handleos() { // os = short oxygen silo int mineos = os.getstoredo2() + (1 * upgradeos); os.setstoredo2(mineos); } public void handlehf() { // hf = short hydrogen fuel int minehf = hf.getstoredo2() + (1 * upgradehf); hf.setstoredo2(minehf); } public void handlelm() { // lm = short liquid minerals int minelm = lm.getstoredminerals() + (1 * upgradelm); lm.setstoredminerals(minelm); } // what's going run whole time on set intervals. public void mine() { mineos = os.getstoredo2() + (1 * upgradeos); minehf = hf.getstoredo2() + (1 * upgradehf); minelm = lm.getstoredminerals() + (1 * upgradelm); os.setstoredo2(mineos); hf.setstoredo2(minehf); lm.setstoredminerals(minelm); } // using 10 seconds here have update possible can // see changes. here write output. public void getupgradeinfo() { system.out.println("oxygen: " + (1 * upgradeos) + " / per 10 seconds."); system.out.println("hydrogen: " + (1 * upgradehf) + " / per 10 seconds."); system.out.println("liquid mineral: " + (1 * upgradelm) + " / per 10 seconds."); }
i'm not best naming schemes materials...
tl;dr: can't figure out how implement thread
or timer
above mentioned mine()
method since not have appropriate amount of knowledge. if-else
rules aren't elegant, i'll work on of course. if-else
rules should run separately mine()
method, , can afking without game updating system.out
output, can floating in, example, oxygen silo upgrade menu, , won't bounced different menu due thread "waking up", such being bounced main menu, mine()
method still generate resources in background should.
any on this, or nudge in right direction appreciated.
to answer question asked, can this:
import java.util.*; timertask tt = new timertask() { public void run() { mine(); } } timer t = new timer(); t.scheduleatfixedrate(tt, 0, 1000);
alternatively, can use actionlistener
, swing timer in similar way. has advantage of being thread-safe in case build swing gui on top
lastly, should check out usage of synchronized
, volatile
make sure variable(s) updated in mine()
done in thread-safe way
Comments
Post a Comment