Game Development Reference
In-Depth Information
string [] stringlist = reader.ReadLine().Split();
int width = int .Parse(stringlist[0]);
int height = int .Parse(stringlist[1]);
We then create a GameObjectList instance that will contain the playing field, just like
we did in the Jewel Jam game. We position this playing field in such a way that it is
nicely centered on the screen:
GameObjectList playingField = new GameObjectList(1, "playingField");
playingField.Position = new Vector2((PenguinPairs.Resolution.X
width
73) / 2, 100);
this .Add(playingField);
The next step is to read the hint information and add it to the level. For now, we
will not yet add the hint arrow to the playing field, but we will already load it. After
loading the x and y position and the direction of the arrow (top, left, down or right),
we create a SpriteGameObject instance representing the arrow. All four directions of
the arrow are stored in a sprite strip of length 4, and with the hint direction index that
we read from the file, we select the sprite strip index that should be used. Finally,
we position the hint arrow using the width and height of one tile in the playing
field:
stringlist = reader.ReadLine().Split();
int hintx = int .Parse(stringlist[0])
1;
int hinty = int .Parse(stringlist[1])
1;
int hintdirection = int .Parse(stringlist[2]);
SpriteGameObject hint = new SpriteGameObject("Sprites/spr_arrow_hint@4", 2,
"hint", hintdirection);
hint.Position = new Vector2(hintx, hinty)
new Vector2(73, 72);
playingField.Add(hint);
Now we need to read the actual tile information from the text file. We will reuse
the GameObjectGrid class to represent a grid of tiles. Reading this grid from the text
file can be done using a nested for -instruction. Have a look at the following lines of
code:
GameObjectGrid tilefield = new GameObjectGrid(height, width, 1, "tilefield");
tilefield.ObjectHeight = 72;
tilefield.ObjectWidth = 73;
for ( int row = 0; row < height; row++)
{
string currRow = reader.ReadLine();
for ( int col = 0; col < currRow.Length; col++)
{
// handle the tile 'currRow[col]' here
}
}
Search WWH ::




Custom Search