- XNA 4.0 Game Development by Example Beginner's Guide(Visual Basic Edition)
- Kurt Jaegers
- 610字
- 2021-08-20 15:50:39
Time for action – creating the GameBoard.cs class
- As you did to create the
GamePiece
class, right-click on Flood Control in Solution Explorer and select Add | Class.... Name the new class fileGameBoard.vb.
- Add the following declarations to the
GameBoard
class:Private rand As New Random() Public Const GameBoardWidth As Integer = 7 Public Const GameBoardHeight As Integer = 9 Private playingPieces As Texture2D Private emptyPiece As Rectangle Private boardSquares(GameBoardWidth, GameBoardHeight) As GamePiece Private waterTracker As New List(Of Vector2)()
What just happened?
We used the Random
class in SquareChase
to generate random numbers. Since we will need to randomly generate pieces to add to the game board, we need an instance of Random
in the GameBoard
class.
The Texture2D
(playingPieces
) and the Rectangle
(emptyPiece
) will be passed in when the class is created and hold the sprite sheet used to draw the board and the location of the sheet's empty piece square.
The two constants and the boardSquares
array provide the storage mechanism for the GamePiece
objects that make up the 8x10
piece board. Note that since arrays start counting at 0
instead of 1
, we use numbers for the width and height that are one lower than the actual number of board squares. This is a personal preference, but we will be doing a lot of looping over the items in the boardSquares
array, and we can either add a -1
to all of the references to our width and height, or declare them as one item smaller to take zero-based arrays into account.
Tip
Visual Basic arrays are a bit quirky in order to maintain some compatibility with pre-.NET versions of VB. In prior versions of VB, arrays could be designated as starting with either element 0
or element 1
. If you declared an array with 5
elements when you had your Option
Base
set to 0
, you actually got 6
elements, numbered 0
through 5
. Visual Basic .NET no longer allows arrays to be one-based, but does continue to allocate the extra array element at the end of the array just like older versions did. For this reason, our code will not actually fail if we had our width and height set to 8
and 10
, but it would produce some strange results. Go ahead and try it out after you have completed the chapter!
Finally, a list of Vector2
objects is declared that we will use to identify scoring pipe combinations. The List
class is one of the .NET Framework's generic collection classes – classes that support the Of
notation that we first saw when loading a texture for SquareChase
. Each of the Collection
classes can be used to store multiple items of the same type, with different methods to access the entries in the collection. We will use several of the Collection
classes in our projects. The List
class is much like an array, except that we can add any number of values at runtime, and remove values in the List
, if necessary.
A Vector2
is a structure defined by the XNA framework that holds two floating point (single) values, X
and Y
. Together, the two values represent a vector pointing in any direction from an imaginary origin (0, 0)
point. We will use Vector2
structures to represent the locations on our game board in Flood Control and off-setting all of our additional drawing based on this location.
- 計算機組成原理與接口技術:基于MIPS架構實驗教程(第2版)
- 圖解機器學習算法
- 基于Apache CXF構建SOA應用
- 大數據架構和算法實現之路:電商系統的技術實戰
- 數據挖掘原理與SPSS Clementine應用寶典
- Oracle 12c云數據庫備份與恢復技術
- 大數據精準挖掘
- Apache Kylin權威指南
- SQL Server深入詳解
- 深入理解InfluxDB:時序數據庫詳解與實踐
- 數據應用工程:方法論與實踐
- Artificial Intelligence for Big Data
- 深入理解Flink:實時大數據處理實踐
- MySQL技術內幕:InnoDB存儲引擎(第2版)
- INSTANT Windows Powershell 3.0 Windows management Instrumentation Starter