Game Development Reference
In-Depth Information
this
.position =
new
Vector2(
−
10, grid.CellHeight
∗
selectedRow
−
10);
We place the row selection object 10 pixels to the left and to the top to account for
the space between the jewels and the row selection object.
If the player presses the left or right arrow key, we call the
ShiftRowLeft
or
ShiftRowRight
method, and we pass along the currently selected row to these methods
so that they know which row to shift. These instructions are rather straightforward:
if
(inputHelper.KeyPressed(Keys.Left))
grid.ShiftRowLeft(selectedRow);
else if
(inputHelper.KeyPressed(Keys.Right))
grid.ShiftRowRight(selectedRow);
14.5 Creating the Game Objects
Now that we have shown how different game object classes can be constructed,
the only thing left to do is create these objects, and the rest will be dealt with by the
objects themselves. First we create the
gameWorld
object, and we add the background
image at layer 0 (which is the default layer value). Then we create the
playingField
object and add it to the
gameWorld
object as well. For this object, we set the layer
index to 1, to ensure that it is drawn on top of the background:
gameWorld =
new
GameObjectList();
Texture2D background = Content.Load<Texture2D>("spr_background");
gameWorld.Add(
new
SpriteGameObject(background));
GameObjectList playingField =
new
GameObjectList(1);
playingField.Position =
new
Vector2(85, 150);
gameWorld.Add(playingField);
Then, we create the grid and the row selection object. Both of these objects are
added to the
playingField
object:
JewelGrid grid =
new
JewelGrid(10, 5);
playingField.Add(grid);
Texture2D selectorFrame = Content.Load<Texture2D>("spr_selector_frame");
RowSelectGameObject rowSelector =
new
RowSelectGameObject(grid,
selectorFrame, 1);
playingField.Add(rowSelector);
Finally, we create all the
Jewel
instances that need to be added to the grid. Every
time we create an instance, we randomly select between three different sprites, in