- XNA 4.0 Game Development by Example Beginner's Guide(Visual Basic Edition)
- Kurt Jaegers
- 268字
- 2021-08-20 15:50:39
Time for action – initializing the game board
- Add a constructor to the
GameBoard
class:Public Sub New(pieceTexture As Texture2D, emptyArea As Rectangle) playingPieces = pieceTexture emptyPiece = emptyArea ClearBoard() End Sub
- Add the
ClearBoard()
helper method to theGameBoard
class:Public Sub ClearBoard() Dim x, y As Integer For x = 0 To GameBoardWidth For y = 0 To GameBoardHeight boardSquares(x, y) = New GamePiece("Empty") Next Next End Sub
What just happened?
When a new instance of the GameBoard
class is created, we store the texture and rectangle values that we will need for drawing, and the constructor calls the ClearBoard()
helper method, which simply creates 80 empty game pieces and assigns them to each element in the array.
Tip
Helper methods
Why not simply put the two for
loops that clear the board into the GameBoard
constructor? Splitting work up into methods that accomplish a single purpose greatly helps to keep your code both readable and maintainable. Additionally, by splitting ClearBoard()
out as its own method, we can call it separately from the constructor. When we add increasing difficulty levels in Chapter 3, Flood Control - Smoothing Out the Rough Edges, we will make use of this call when a new level starts.
Updating GamePieces
The boardSquares
array in the GameBoard
class is declared as a private
member, meaning that code that uses the GameBoard
will not have direct access to the pieces contained on the board.
In order for code in our Game1
class to interact with a GamePiece
, we will need to create public
methods in the GameBoard
class that expose the pieces in boardSquares
.
- Voice Application Development for Android
- Python數(shù)據(jù)分析、挖掘與可視化從入門到精通
- 算法與數(shù)據(jù)中臺(tái):基于Google、Facebook與微博實(shí)踐
- UDK iOS Game Development Beginner's Guide
- Mastering Machine Learning with R(Second Edition)
- 企業(yè)級數(shù)據(jù)與AI項(xiàng)目成功之道
- MySQL 8.x從入門到精通(視頻教學(xué)版)
- 科研統(tǒng)計(jì)思維與方法:SPSS實(shí)戰(zhàn)
- Python數(shù)據(jù)分析與數(shù)據(jù)化運(yùn)營
- 計(jì)算機(jī)組裝與維護(hù)(微課版)
- 大數(shù)據(jù)與機(jī)器學(xué)習(xí):實(shí)踐方法與行業(yè)案例
- Mastering ROS for Robotics Programming(Second Edition)
- 數(shù)據(jù)庫應(yīng)用系統(tǒng)技術(shù)
- Practical Convolutional Neural Networks
- 反饋:化解不確定性的數(shù)字認(rèn)知論