Game Development Reference
In-Depth Information
Since penguins need to move around on the playing board and we need to interact
with them, we will create a class Animal to represent an animal such as a penguin or
a seal. In order to keep track of which animals are in the game, we maintain a list as
a member variable of the Level class:
protected List<Animal> animals;
Inside the switch instruction, we then create a normal tile and a penguin, as follows:
t= new Tile("Sprites/spr_field@2", 0, "", (row + col) % 2);
tilefield.Add(t, col, row);
string assetname = "Sprites/spr_penguin@8";
if ( char .IsUpper(currRow[col]))
assetname = "Sprites/spr_penguin_boxed@8";
Animal a = new Animal(assetname, 2, "", currRow[col]);
a.Position = t.Position;
a.InitialPosition = t.Position;
playingField.Add(a);
animals.Add(a);
break ;
As you can see, we are doing a couple of other things as well. For instance, we make
a difference between normal animals, and boxed animals (e.g., animals that cannot
move). We make this distinction by using either uppercase or lowercase characters.
The char type has a method called IsUpper that checks if a character is uppercase.
We use that method in the condition of an if -instruction to determine the name of the
asset that should be used. After creating the Animal object, we set its position to the
position of the tile we created, so that it is placed correctly. We also set a property
called InitialPosition to that same value. We do this so that if the player gets stuck and
presses the 'retry' button, we know what the original position is of each animal in
the level.
If you look at the Animal constructor, you see that we pass the character along as
a parameter. We do this so that inside the constructor we can decide which element
of the strip should be selected. Furthermore, we can check the character to find out
if we are dealing with a boxed animal or not. The boxed status is stored in a boolean
member variable of the Animal class:
boxed = char .IsUpper(color);
We are using the IndexOf method of the string type in the constructor to determine
which sheet index we should use, depending on the character that was passed along
as a parameter:
string animals = "brgyopmx";
sprite.SheetIndex = animals.IndexOf( char .ToLower(color));
Search WWH ::




Custom Search