In a Tetris game I'm trying to make (using C++/SDL), I need to control the falling speed of, say, one block. I have successfully done this by incramenting its y
value then setting SDL_Delay(200)
. So every 200 ms, the block moves down one unit. However, using SDL_Delay
lags the whole game, and using the controls to move the block around gets delayed as well. I need to control the falling speed in a way that nothing gets delayed except the falling of the block. How can I achieve this? Thanks.
You need to implement some sort of a timer. A basic implementation could work like the following: start by calculating the next time when a block should move, which might look something like nextMoveTime = SDL_GetTicks() + 200;
. Then, each frame/update cycle, you would check if enough time has passed and if so, move the block. So something simple like if (SDL_GetTicks() >= nextMoveTime) block->Move();
. If you wanted to keep the block moving, you would then recalculate nextMoveTime
.