- XNA 4.0 Game Development by Example Beginner's Guide(Visual Basic Edition)
- Kurt Jaegers
- 323字
- 2021-08-20 15:50:40
Time for action – making the connection
- Add the
PropagateWater()
method to theGameBoard
class:Public Sub PropagateWater(x As Integer, y As Integer, fromDirection As String) If (y >= 0) And (y <= GameBoardHeight) And (x >= 0) And (x <= GameBoardWidth) Then If boardSquares(x, y).HasConnector(fromDirection) And Not (boardSquares(x, y).PieceSuffix.Contains("W")) Then FillPiece(x, y) waterTracker.Add(New Vector2(x, y)) For Each pipeEnd As String In boardSquares(x, y).GetOtherEnds(fromDirection) Select Case pipeEnd Case "Left" PropagateWater(x - 1, y, "Right") Case "Right" PropagateWater(x + 1, y, "Left") Case "Top" PropagateWater(x, y - 1, "Bottom") Case "Bottom" PropagateWater(x, y + 1, "Top") End Select Next End If End If End Sub
- Add the
GetWaterChain()
method to theGameBoard
class:Public Function GetWaterChain(y As Integer) As List(Of Vector2) waterTracker.Clear() PropagateWater(0, y, "Left") Return waterTracker End Function
What just happened?
Together, GetWaterChain()
and PropagateWater()
are the keys to the entire Flood Control game, so understanding how they work is vital. When the game code wants to know if the player has completed a scoring row, it will call the GetWaterChain()
method once for each row on the game board:

The WaterTracker
list is cleared and GetWaterChain()
calls PropagateWater()
for the first square in the row, indicating that the water is coming from the Left
direction.
The PropagateWater()
method checks to make sure that the x
and y
coordinates passed to it exist within the board and, if they do, it checks to see if the piece at that location has a connector matching the fromDirection
parameter and that the piece is not already filled with water. If all of these conditions are met, that piece gets filled with water and added to the WaterTracker
list.
Finally, PropagateWater()
gets a list of all other directions that the piece contains (in other words, all directions the piece contains that do not match fromDirection
). For each of these directions PropagateWater()
recursively calls itself, passing in the new x
and y
location as well as the direction the water is coming from.
- 數據庫技術與應用教程(Access)
- MySQL從入門到精通(第3版)
- Live Longer with AI
- INSTANT Cytoscape Complex Network Analysis How-to
- Lego Mindstorms EV3 Essentials
- INSTANT Apple iBooks How-to
- 數據科學實戰指南
- TextMate How-to
- Power BI智能數據分析與可視化從入門到精通
- Web Services Testing with soapUI
- Node.js High Performance
- Visual Studio 2012 and .NET 4.5 Expert Development Cookbook
- 大數據技術體系詳解:原理、架構與實踐
- SQL Server 數據庫教程(2008版)
- Redis 6開發與實戰