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
We're going to continue building the Treehouse Defense game started in C# Objects. Let's refresh ourselves on the code so far.
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
Open Workspaces and
you'll see the code for
0:00
the Treehouse Defense game
that's been written so far.
0:02
Let's take a few minutes
to dissect this code so
0:06
we have a good understanding
of it before we move on.
0:08
This is 100% review, so
I'll move through this rather quickly.
0:11
Let's start by looking at Map.cs.
0:16
This is the definition of the Map class.
0:19
A map determines the width and
height boundaries of a level.
0:22
It's constructed by passing it a width and
height value for the map.
0:25
We also have an OnMap method which can be
0:29
used to determine if
a point is on the map.
0:32
If we open Point.cs, we'll see
a definition for the Point class.
0:35
The Point class represents
a coordinate on the grid, so
0:40
it's initialized with the values for
x and y.
0:43
The Point class has two methods,
both named DistanceTo.
0:47
This first one will tell us the distance
between the point object the DistanceTo
0:51
method is being called on and the x and
y coordinates being passed in.
0:55
The second method is an overload of
DistanceTo, meaning there are two
0:59
methods with the same name, but
they accept different parameters.
1:04
This second method calculates the distance
between the two point objects.
1:08
The Point class is a very general purpose
class to represent coordinates on a grid.
1:12
We needed a class that was more specific
to the Treehouse Defense game, so
1:16
we subclassed the Point class and
created the MapLocation class.
1:21
We can see the MapLocation
class in MapLocation.cs.
1:25
Here we can see that MapLocation
is a subclass of Point,
1:29
because it says : Point here.
1:32
Also, remember,
a subclass of another class inherits
1:34
all of the non-private
members of the other class.
1:38
So the MapLocation class also has x and
y fields, and the two DistanceTo methods.
1:41
Inheritance is one of
the principles of object-oriented
1:48
programming that we've
learned about before.
1:51
We'll get a lot more practice using
inheritance in this course as well.
1:53
The MapLocation constructor also
takes x and y parameters and
1:57
passes them up to the Point class
constructor here where it says, : Base.
2:01
It also takes a Map object as a parameter.
2:06
The constructor verifies that
the MapLocation being constructed
2:09
is indeed on the map, because if it isn't,
2:13
it can't rightfully be called
a MapLocation, now, can it?
2:16
The InRangeOf method determines if
this MapLocation object is in range of
2:19
the MapLocation passed in.
2:23
This OutOfBoundsException is
an exception type we made ourselves.
2:26
We can find a definition for
2:30
the OutOfBoundsException
type in Exceptions.cs.
2:32
Notice that the OutOfBoundsException
is a type of TreehouseException,
2:35
which, in turn,
is a type of System.Exception.
2:40
Remember, all exception types in C#
must inherit from System.Exception.
2:44
You'll remember that in Treehouse Defense,
invaders march down the path and
2:49
towers shoot at them.
2:54
If an invader makes it past the end
of the path, then the player loses.
2:56
So we also have a class
to represent the path.
3:01
A path is simply a list
of adjacent MapLocations.
3:05
These MapLocations are stored in an array.
3:08
We've made this array member variable
private because users of this class
3:11
don't need to know that it's an array or
have direct access to it.
3:15
They only need to know
the length of the path and
3:19
what the MapLocation is
at any step in the path.
3:21
So we've provided a public property for
the length,
3:25
which just returns the array's length,
and a public method named GetLocationAt.
3:28
The Path class is a good demonstration
of another principle of object-oriented
3:34
programming, that is, encapsulation.
3:38
By making the array private and
exposing the methods and
3:41
properties that we want
the user of this class to have,
3:44
we've given ourselves the freedom to
change the implementation details
3:47
of this class in the future without
affecting any users of the class.
3:51
The public methods of a class
are part of the class's interface.
3:55
When designing a software application,
3:59
we want to think of classes
in terms of their interface.
4:01
This greatly facilitates
software design and
4:04
development because the software designer,
or sometimes called the architect,
4:07
can focus on making sure that
the interfaces are correct.
4:12
And the implementation details can be
left up to the individual developers
4:15
who write the code for the classes.
4:20
This also helps with maintaining
the class over time and fixing bugs.
4:22
So long as the public interface isn't
affected, we can fix bugs all we want and
4:26
there's less possibility of
breaking the entire program.
4:31
This is why encapsulation is such
an important principle in object-oriented
4:34
programming.
4:39
We'll learn more about how to define
interfaces between classes later in
4:39
this course.
4:43
The main user of the Path
class is the invader.
4:44
We can find the code for
the Invader class in Invader.cs.
4:47
Invaders keep track of their
location on the path by storing it
4:51
in the pathStep field.
4:55
Location is a computed property that
gets the MapLocation of the invader
4:57
based on its pathStep.
5:01
HasScored is another computed property
that returns true if the invader has made
5:04
it past the end of the path.
5:08
Take note that HasScored and
5:10
location are both computed properties, but
they're written with different syntax.
5:13
We could just as easily have written
the HasScored property using the equal
5:19
angle bracket notation or written
the location property using curly braces.
5:23
The result is the same.
5:29
I've done both here to
demonstrate both ways for
5:30
you to use as a reference in the future.
5:33
Health is a property with a public
getter and a private setter.
5:36
It's initialized to 2.
5:39
An invader is neutralized if its
health is less than or equal to 0,
5:41
and it's active if it is neither
neutralized nor has it scored.
5:46
Finally, invaders have two behaviors.
5:51
They can move and
their health can be decreased.
5:54
These are both methods using
two different method syntaxes.
5:57
We can tell that they're methods because
of the opening and closing parentheses.
6:01
The Move method uses the single
line method syntax and
6:06
DecreaseHealth uses the more
traditional syntax with curly braces.
6:10
The result is the same.
6:15
Now that we've seen Invader,
let's take a look at Tower in Tower.cs.
6:16
The primary behavior of a tower
is to shoot at the invaders.
6:22
We initialize the tower
with a location on the map.
6:26
The FireOnInvaders method loops through
the invaders and shoots at them.
6:29
There are private fields in the Tower
class for how far the tower can shoot,
6:34
how much it can decrease the invaders'
health, and its accuracy.
6:38
We've made it possible for
the tower to miss its target
6:43
by using the Random class to give us a
random number each time the tower shoots.
6:46
The IsSuccessfulShot method determines
if the tower hits its target
6:51
by comparing the randomly generated
number with the accuracy of the tower.
6:56
Now that we've taken a look at
all of the pieces of the game,
7:00
let's take a quick look
at how it's played.
7:03
Let's start by looking at
the main method for the program.
7:06
We can find it in Game.cs.
7:10
The main method in a program is
known as the program's entry point.
7:13
This is the first code that will be
executed when the program starts.
7:18
Treehouse Defense can have many
different levels for the player to play.
7:22
Each level could have a different sized
map, a different path through the map, and
7:26
a different set of invaders
that move down the path.
7:30
Typically, a game would
read this information for
7:33
each level played from a file.
7:36
This would require us to write
some code to open and read a file.
7:38
That's beyond the scope of this course.
7:42
We're interested in learning the ins and
outs of object-oriented programming here.
7:45
You can find a link to
a course on reading and
7:49
writing files in the teacher's
notes of this video.
7:52
Instead of reading the level
details from a file,
7:54
we're constructing the level
here in main instead.
7:57
I just want you to know that this
is not typically how this is done.
8:00
It does demonstrate how to
construct various objects and
8:04
arrays of objects programmatically,
though.
8:07
This is where we actually
construct the level object and
8:09
pass it the invaders that
will be in the level.
8:12
Now, if this game had a more sophisticated
user interface such as a graphical
8:15
user interface, we would allow the user
of the game to use their mouse or
8:18
touch display to pick
the location of the towers.
8:23
We're not concerned with how to create
a graphical user interface in this course,
8:26
so we're doing this in code instead.
8:31
There are many ways to create
a graphical user interface for a game.
8:34
You can find a link to courses on how
to do this in the teacher's notes.
8:37
In this course,
we really just want to understand
8:41
how to use object-oriented programming
in C# to write the code for our game.
8:44
We call the Level's Play method to
start the level and print out who won.
8:49
Here we have the various
catch clauses to catch and
8:55
handle the various exceptions
that we expect might happen.
8:57
Finally, let's take a look at
the level class in Level.cs.
9:01
It's fairly simple.
9:06
It contains a list of invaders and towers.
9:07
The meat of this class is the Play method,
9:11
which contains most of the logic for
the game.
9:13
Isn't it fascinating that once we have
the types of object in our game modeled in
9:16
code, the logic for
the game boils down to just these lines?
9:20
Ideally, the logic for
a program can be read in coherent English.
9:24
In our case,
we can read the code like this.
9:29
While there are remaining invaders, each
of the towers will fire on the invaders.
9:32
Each of the invaders that
is still active can move.
9:38
If they score, then the player loses,
otherwise, the player wins.
9:41
That's the entire Treehouse Defense
game up to this point.
9:48
I know we went through
that rather quickly.
9:52
Hopefully it's mostly review to you.
9:54
We'll get to know this code even more as
we alter it to make the entire game more
9:56
extendable.
10:01
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