Time for action – generating new pieces
Add the
GenerateNewPieces()
method to theGameBoard
class:Public Sub GenerateNewPieces(dropSquare As Boolean) Dim x, y As Integer If dropSquare Then For x = 0 To GameBoardWidth For y = GameBoardHeight To 0 Step -1 If GetSquare(x, y) = "Empty" Then FillFromAbove(x, y) End If Next Next End If For y = 0 To GameBoardHeight For x = 0 To GameBoardWidth If GetSquare(x, y) = "Empty" Then RandomPiece(x, y) End If Next Next End Sub
What just happened?
When GenerateNewPieces()
is called with true
passed as dropSquares
, the looping logic processes one column at a time from the bottom up. By using the step 1 in the for
loop for the Y
coordinate, we can make the loop run backwards instead of the default forward direction. When it finds an empty square, it calls FillFromAbove()
to pull a filled square from above into that location.
The reason the processing order is important here...