Hah, good animation (I’m into ugly, I guess). Anyways, it demonstrates one of my points:
The block is continuing to be signaled to drop while the animation is playing…do we want that? Maybe, maybe not. We defintely don’t want it if the animation involves clearing the row (maybe a very high row, very late in the game) and with the block falling befor ethe row is cleared, I could loose the game simply because the animation din’t finish fast enough before the block collided with a non-clear row (I am getting mad just thinking about it).
So, let’s say, for argument’s sake, we don’t want the brick to drop during the animation…that means your game loop should not let the timer controlling the block’s desent continue while the current ‘tick’ is being processed…
Without going into too much detail, I think your game boils down to 2 threads: A user input thread (that AWT or Swing will provide to recieve user input) and your timer thread that should fire off an event of it’s own to signal the block should be moved down. The game only needs to check it’s state (to see if the game is over or if a row is cleared, or the block just falls to the bottom and nothing happens) when either A) The user clicks up arrow or B) The timer triggers. Once this happens, the following chain of code can execute:
Move Block (either 1 space down or all the way down)
Check for row clear
Perform any animations as necessary **
Restart the Timer trigger.
** This action you can either draw to the screen as fast as possible, or you can draw it at a fixed FPS Either way, nothing further should happen in the game (such as blocks moving downwards, etd) until after this chain of code completes.
Like you said, yes, you need to have a way to ‘cancel’ the trigger if the user does something that should abandon the countdown…maybe your Trigger thread can look something like this:
Public class Trigger extends Thread
{
private int TICK_TIME = 500;
private boolean aborted = false;
public setAbort()
{
aborted = true;
}
public void run()
{
try
{
Thread.sleep(TICK_TIME);
if (!aborted)
fireEvent();
}
}
}
Now, if the player does something that should halt the trigger, just call setAbort() on the trigger. Every time you ‘restart the timer trigger’ you create a new trigger object. Now, this violates rule number 1 of all game programming: do NOT create excessive objects…(especially in this case, thread objects), but it will get the job done. With more work, you could use Thread.inturrupt() to wake up the thread so that it’s in a state to reuse. This would save a lot of resources.
-Chris