For my Tetris game, I wanted it to behave so that if you hold [right] on the keyboard, for example, the piece will move continually to the right, but at a reasonable rate. I considered using Sys.wait() somehow, but this would cause the entire application to pause while waiting for the piece to move sideways, which would stop it from free-falling and everything. Obviously not the desired intent, since I wish it to continue falling at the same rate whether or not a sideways key is held.
So, I implemented Shape.move() as follows:
method void move(Grid g, int direction) {
[some code]
if (moveWait < 2000) {
let moveWait = moveWait + 1;
return;
} else {
let moveWait = 0;
[code to move shape]
}
}
It worked pretty much as I had hoped, but it felt kind of hokey to me. Though I could come up with no better option. My concern was that since I was relying on addition to control my time delay, it could be unreliable when run on a different hardware platform that performed faster or slower, or when run simultaneously with other tasks competing for CPU (though, I guess neither is a valid concern here since this program ONLY runs on Hack which does not have multitasking). I did run this in the VMEmulator on a couple different real physical PC's of varying specs, and didn't really notice a difference. I'm guessing this is because the VMEmulator implementation probably has some control in it tied to real clock time, to control the speed of its instruction execution?
Also, the thought about about multitasking got me thinking about multithreading for the Tetris game. On a real platform with much more features than the Hack, would threads be the correct way to solve my dilemma about how to move the shape sideways in a clock-time controlled manner (i.e. using x-millisecond wait commands), without having those wait commands affect the other things such as the constant free-falling of the shape? I don't have much application/game writing experience so I really don't know.
Thanks!