We are now ready to have the falling piece respond to time events by dropping one row on each call to timerFired.
To start, we can have our timerFired function call moveFallingPiece(data, +1, 0), just as we do in response to a down-arrow
key press.
This is not a bad start, but when it gets to the bottom, it just sits there (until we hit a
non-arrow key to artificially reset the falling piece).
Once again, when the fallingPiece gets to the bottom, it simply stops falling and sits there -- it does not get placed on the board. To remedy this, we first need to know that it happened! That requires a small change to some code we have written. We will change moveFallingPiece so that now it returns True if the move occurred, and False otherwise for any reason (the piece moved off the board, or the piece collided with something on the board). This requires only a small change to our code. Once we have done this, our timerFired can test the return value from moveFallingPiece to determine if the move failed. In that case, we will use top-down design and call a function (that we are about to write) to place the falling piece on the board, followed by a call to newFallingPiece, to start a new piece falling from the top.
Writing placeFallingPiece:
This
function (which only takes data, and not canvas) is quite similar to drawFallingPiece, only rather than draw
the cells, we load the corresponding positions on the board with the
fallingPieceColor. In this way, the piece is placed on the board.
David Kosbie |