Sign in to follow this  
juanpaco

Timing in a tetris clone

Recommended Posts

juanpaco    150
Hi everyone. I'm pretty new to game writing. My experience includes ascii-based fighting games in GW-BASIC and one 3d Tron light cycle ripoff I did for graphics class in college. It was basically a hack job. I want to learn how to make games better than that, so I took the advice of an article here, and I'm starting with a Tetris clone. I'm doing this in C#, and just to make things more complicated, I might use Managed DirectX. My question is how to manage how often to update the screen. Tetris of course has blocks falling at a given rate, etc. How does one time the falling of the blocks? In the GW-BASIC days I would just stick a for loop in to slow the computer down, but that's probably the worst method in existence. I've thought about this, and the best I can come up with is to keep track of the last time the screen was updated, and during the render loop check to see if the difference between that time and now is larger than a given delta (determined by current level or whatever). If it is, then make the block fall, check for whether it should settle, if lines are completed, etc. If not, well, don't. Does that sound about right? Has anyone else here made a Tetris clone? Is that last question like asking if anyone at a restaurant review site has eaten out before? Thanks in advance for any input.

Share this post


Link to post
Share on other sites
Zahlman    1682
Ai ai ai.

Don't "make the block fall" during the render process. You will have major design headaches if you try to update the game state in your rendering code. Update and render are separate tasks.

Typically, what you do is have a main loop where you time how long the last iteration took, and pass that as a parameter to the render and update functions. The render function can use the value to do interpolation between frames, but will often just ignore it, and instead just make queries about the game state and make the corresponding graphics-drawing calls. The update function for something as simple as Tetris might just bail out if not enough time has elapsed, and otherwise do game updates. (For a game where things move continuously, i.e. where you need actual physics simulation, you use the time in your formulas for acceleration/velocity/distance etc.)

So for example, something like:


// C++ code because I don't actually know any C#, but it should be
// very similar
class Level {
int blockFallDelay; // the time it takes, in milliseconds, for the block to
// drop one space - is set as a function of what level we're on

const int BLOCK_SIZE = 32; // or maybe it's not const, and depends on the
// screen resolution - but you get the idea

int msSinceLastUpdate;

void Render(int elapsedMilliseconds) {
// If you want the block to fall smoothly:
int dy = (BLOCK_SIZE * elapsedMilliseconds) / blockFallDelay;
// and then add 'dy' to the rendering coordinates when you render the
// quads that make up the piece that's dropping.

// do the rest here
}

void Update(int elapsedMilliseconds) {
msSinceLastUpdate += elapsedMilliseconds;
if (msSinceLastUpdate < blockFallDelay) { return; } // don't update yet
msSinceLastUpdate -= blockFallDelay;
// do update
}

bool Over() {
// return whether or not the level has finished
}
};

int main() {
Level lvl(1);
int ms = 0;
while (!lvl.Over()) {
int time = getCurrentTime(); // from somewhere...
lvl.Render(ms);
lvl.Update(ms);
ms = getCurrentTime() - time;
}
}

Share this post


Link to post
Share on other sites
juanpaco    150
Quote:
Original post by Zahlman

Don't "make the block fall" during the render process. You will have major design headaches if you try to update the game state in your rendering code. Update and render are separate tasks.

Hahahaha. Silly me. I should probably check for user input during the render function right when I get done with updating game state too! ;) And maybe see if there's a way to work in bubble sort...

Thanks for the reply Anonymous poster, it's good to know the idea wasn't completely off the rocker.

Zahlman, thank you for your reply too. I particularly liked the discussion of how different styles of game could use the time delta depending on the desired effect. I might not have thought about that with later projects. I think with Tetris I would follow your suggestion in keeping the movement more discrete rather than continuous, thus staying truer to the original. Might be interesting to play with though... And the code is pretty similar to C#. Main would have to be in a class, but that's about it.

Thanks again all. Gamedev rules! 1st reply came in four minutes. How's that for speed!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this