c# - How to count seconds and then use them in a conditional? -
i newbie programmer , i've been trying deal problem on 2 hours now. want program count seconds , if gets past second second (what?), something, example show "game over" or that. problem is, program doesn't after these 2 seconds have passed. might problem ?
edit: ok here whole info behind this. user has press key corresponding character shown on screen in 2 seconds. if user doesn't press key in 2 seconds or presses wrong key, game has over, doesn't work expected lol
here whole code far (yes know goto sucks , change loop later):
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using system.globalization; using system.threading; using system.diagnostics; class game { static void main() { start: console.cursorvisible = false; random rnd = new random(); int row = rnd.next(1, 80); int col = rnd.next(1, 25); int chars = rnd.next(0, 62); string lettersandchars = ""; lettersandchars = lettersandchars.toupper(); console.setcursorposition(row, col); if (chars <= 10) { lettersandchars += (char)(chars + '0'); } else { lettersandchars += (char)(chars + 'a'); } lettersandchars = lettersandchars.toupper(); console.writeline(lettersandchars); datetime endtime = datetime.now.addseconds(2); var keypress = console.readkey(); string keypressstring = keypress.keychar.tostring(); keypressstring = keypressstring.toupper(); if (keypressstring == lettersandchars && datetime.now < endtime) { console.clear(); goto start; } else if (keypressstring != lettersandchars || datetime.now > endtime) { console.clear(); console.writeline("game over"); } } }
i wouldn't use timer. can following:
datetime endtime = datetime.now.addseconds(2); while (!console.keyavailable && datetime.now < endtime) thread.sleep(1); if (console.keyavailable) { var keypress = console.readkey(); string keypressstring = keypress.keychar.tostring(); keypressstring = keypressstring.toupper(); if (keypressstring == lettersandchars) { console.clear(); goto start; } } console.clear(); console.writeline("game over");
Comments
Post a Comment