- XNA 4.0 Game Development by Example Beginner's Guide(Visual Basic Edition)
- Kurt Jaegers
- 384字
- 2021-08-20 15:50:41
Time for action – scores and scoring chains
- Add a method to the
Game1
class to calculate a score based on the number of pipes used:Private Function DetermineScore(squareCount As Integer) As Integer Return CInt( ((Math.Pow((squareCount / 5), 2) + squareCount) * 10)) End Function
- Add a method to evaluate a chain to determine if it scores and process it:
Private Sub CheckScoringChain(WaterChain As List(Of Vector2)) If (WaterChain.Count > 0) Then Dim LastPipe As Vector2 = WaterChain(WaterChain.Count - 1) If LastPipe.X = gameBoard.GameBoardWidth Then If _gameBoard.HasConnector( CInt(LastPipe.X), CInt(LastPipe.Y), "Right") Then playerScore += DetermineScore(WaterChain.Count) For Each thisPipe As Vector2 In WaterChain _gameBoard.SetSquare( CInt(thisPipe.X), CInt(thisPipe.Y), "Empty") Next End If End If End If End Sub
What just happened?
DetermineScore()
accepts the number of squares in a scoring chain and returns a score value for that chain. The number of squares in the chain is divided by 5
, and that number is squared. The initial number of squares is added to the result, and the final amount is multiplied by 10
.
Score = (((Squares / 5) ^ 2) + Squares) * 10
For example, a minimum scoring chain would be 8 squares (forming a straight line across the board). This chain would result in 1 squared plus 8 times 10, or 90
points. If a chain had 18 squares, the result would be 3 squared plus 18 times 10, or 270
points. This scoring equation makes longer scoring chains (especially increments of five squares) award much higher scores than a series of shorter chains.
The CheckScoringChain()
method makes sure that there are entries in the WaterChain
list, and then examines the last piece in the chain and checks to see if it has an X
value of 7
(the right-most column on the board). If it does, the HasConnector()
method is checked to see if the last pipe has a connector to the right, indicating that it completes a chain across the board.
After updating playerScore
for the scoring row, CheckScoringChain()
sets all of the pieces in the scoring chain to Empty
. They will be refilled by a subsequent call to the GenerateNewPieces()
method.