c# - Using thread two part of a program differently -
i'm coding pacman (c# console) know how move ghost each seconds, able move pacman whenever want.
i ghost keep mooving each second whatever pacman do, should able move pacman when ever want, , ghosts have move every second.
i guess have use thread system, know if know how have proceed , how thread works, can't find information :s.
you're confused because console.readkey()
blocking...so game loop stuck waiting user press key right? grab keystroke readkey() if there 1 in queue. can checked console.keyavailable
returns true if key there. way game loop keeps looping around if no key has been pressed...but can still trap , it. try quick example out , see happens:
class program { static void main(string[] args) { while (true) { system.threading.thread.sleep(250); console.write("."); if (console.keyavailable) { consolekeyinfo key = console.readkey(true); // read key without displaying console.writeline(""); console.writeline("key pressed: " + key.keychar.tostring()); } } } }
Comments
Post a Comment