Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Objects are only useful when they work together.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Now that we have all of
the classes implemented for
0:00
each of the types of objects
we have in Treehouse Defense,
0:02
we can use these classes
together to finish the game.
0:06
We have a really nice set of classes,
but nothing ties them all together yet.
0:09
We haven't yet coded up how all these
objects work together to create a game.
0:14
So now we need to write the code for
how the game runs.
0:19
Instead of putting the code that
runs the game in the game class,
0:23
let's introduce the idea of a level.
0:26
In Treehouse Defense,
a level is composed of a map, a path, and
0:29
a number of invaders.
0:33
Then we can create different levels
with unique maps, paths, and
0:35
collection of invaders.
0:39
What we do in this video is mostly review
of many of the things we've learned so
0:41
far about C Sharp.
0:45
So we're going to move through it quickly.
0:47
Let's start by creating
a new file called Level.cs.
0:50
In the Treehouse Defense namespace
we'll create a class named Level.
0:56
The level constructor only
needs the array of invaders.
1:05
Remember each invader already has
an instance of the path object.
1:08
So we don't need to set the path
as a field in the level class.
1:13
Towers aren't actually required
in order to construct a level.
1:24
But, eventually the level
does need to know about them.
1:27
Let's add a tower's property with
the public getter and a setter.
1:31
So the players can set this array
after the level is created.
1:34
Now let's make a method called play.
1:41
It will return true if the player wins
the level and false if they lose.
1:44
Let's use comments to scaffold
out how the game will work.
1:49
The level will run until all
invaders are neutralized or
1:54
an invader reaches the end of the path.
2:01
Got an extra the there.
2:08
First, Each tower has
an opportunity to fire on invaders.
2:11
Then we'll need to count and
move the invaders that are still active.
2:22
When we see the words Run until
that means we'll need a loop.
2:30
Let's make a variable to keep track of
how many invaders there are remaining.
2:34
The loop will run until all
the invaders have been neutralized.
2:44
The things described by these two
comments will happen inside the loop.
2:56
So now we'll loop through each of the
towers and let them fire on the invaders.
3:00
Now we'll count the number
of invaders again.
3:23
We'll reset remaining invaders to zero and
then loop through the invaders.
3:25
We'll increment remaining invaders for
each active invader in the array.
3:42
If the invader is active,
then we should also move it down the path.
3:55
After the invader has moved,
4:02
we should check to see if it's
reached the end of the path.
4:03
If it has, we'll return false
signifying that the user lost.
4:07
So we'll check invader.hasscored.
4:12
Finally if all invaders
are neutralized then the loop will
4:17
exit because remaining
invaders will be zero.
4:20
Then we'll return true
signifying that the user won.
4:23
So this is the end of the loop here.
4:28
So here we return true.
4:30
As we look at this method,
4:34
it isn't entirely obvious on the surface,
what returning true or false means.
4:35
We can provide that information in
a comment here at the top of the method.
4:41
So I'll say Returns: true if
4:46
the player wins, false otherwise.
4:51
We just wrote a ton of code.
4:57
Be sure to read through this code
carefully to make sure that you understand
4:59
what it's doing.
5:02
You can always watch this
video again if that helps.
5:02
Finally, let's compile to make sure that
we haven't introduced any compiler errors.
5:06
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up