官术网_书友最值得收藏!

Time for action – generating new pieces

  1. Add the GenerateNewPieces() method to the GameBoard 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 is that by filling a lower square from a higher position, that higher position will become empty. It, in turn, will need to be filled from above.

After the holes are filled (or if dropSquares is set to false), GenerateNewPieces() examines each square in boardSquares and asks it to generate random pieces for each square that contains an empty piece.

Water-filled pipes

Whether or not a pipe is filled with water, it is managed separately from its orientation. Rotating a single pipe could change the water-filled status of any number of other pipes, without changing their rotation.

Instead of filling and emptying individual pipes, it is easier to empty all of the pipes and then re-fill the pipes that need to be marked as having water in them.

主站蜘蛛池模板: 横峰县| 成都市| 额尔古纳市| 南丰县| 稻城县| 常宁市| 昌乐县| 锡林郭勒盟| 新河县| 永胜县| 洛宁县| 浦江县| 新源县| 嘉兴市| 化隆| 武陟县| 东乌| 南召县| 平顶山市| 七台河市| 开远市| 陵川县| 莱州市| 永昌县| 台中市| 马鞍山市| 怀集县| 仁寿县| 铁力市| 新干县| 西峡县| 寿光市| 蚌埠市| 衢州市| 凤城市| 乐陵市| 祁门县| 长泰县| 缙云县| 兴城市| 黄大仙区|