Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
      You have completed C# Objects!
      
    
You have completed C# Objects!
Preview
    
      
  The game is finished. Let's make a level and play it.
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 let's make a level and play the game.
                      0:00
                    
                    
                      This is where we'll go
back to the main method.
                      0:02
                    
                    
                      In most real life games,data about
each level would be stored in a file.
                      0:04
                    
                    
                      When the player chooses to play a level,
it would read from the file, and
                      0:10
                    
                    
                      the map, path, invader, and level objects
would be created using that data.
                      0:13
                    
                    
                      For the time, being instead of
reading this information from a file,
                      0:19
                    
                    
                      we'll create the level
object right here in main.
                      0:22
                    
                    
                      We already have a map and
a path created so let's keep those.
                      0:25
                    
                    
                      We'll need to create an array of invaders.
                      0:29
                    
                    
                      We'll have four in this level.
                      0:32
                    
                    
                      So I'll say invader,
array, call it invader,
                      0:34
                    
                    
                      call invaders, then create
a new Invader pass of the path,
                      0:38
                    
                    
                      and then I'll copy this three more times.
                      0:44
                    
                    
                      We'll construct a level called Level1 and
pass in the invaders.
                      0:50
                    
                    
                      Again, in real life players would pick
where to place towers by clicking
                      1:02
                    
                    
                      on the map.
                      1:06
                    
                    
                      Our game doesn't have a graphical
element to it yet, so we can't do that.
                      1:08
                    
                    
                      Instead, we could use the console to let
players select where to place towers.
                      1:12
                    
                    
                      If you like,
you can code this up yourself.
                      1:17
                    
                    
                      If you've taken the prerequisites for
this course,
                      1:20
                    
                    
                      then you've already learned everything
you need to know, in order to do that.
                      1:22
                    
                    
                      For now, we'll create and assign the
towers their locations directly in code,
                      1:26
                    
                    
                      and then pass them to the level object.
                      1:30
                    
                    
                      I encourage you to make improvements to
the tree house defense game on your own,
                      1:33
                    
                    
                      as you see fit.
                      1:36
                    
                    
                      It's very good practice, and
you'll understand even more about why
                      1:38
                    
                    
                      object-oriented programming makes
changing and extending existing code so
                      1:41
                    
                    
                      straightforward.
                      1:45
                    
                    
                      Let's create an array of towers here.
                      1:47
                    
                    
                      So, let me create a new tower in
the array and give it a MapLocation.
                      1:55
                    
                    
                      Right now,
our towers only have a range of one, so
                      2:00
                    
                    
                      we'll need to place them
right beside the path.
                      2:04
                    
                    
                      The path is on row two of the map so
let's place them on row three.
                      2:07
                    
                    
                      So I'll out the first one at 1, 3.
                      2:11
                    
                    
                      Then I'll just copy this, paste it
a couple times, and change the x value.
                      2:17
                    
                    
                      I'll put the 2nd one on x equals 3,
and the 3rd one at x equals 5.
                      2:23
                    
                    
                      Now we can set the tower
property on the level.
                      2:30
                    
                    
                      We can set this property like so.
                      2:34
                    
                    
                      Another way you'll see properties being
set on the objects is in a property
                      2:39
                    
                    
                      initialization list.
                      2:43
                    
                    
                      This is handy, when you want
to set a property on an object
                      2:45
                    
                    
                      immediately after the object is created.
                      2:48
                    
                    
                      To do, that you just add some
curly braces right here.
                      2:51
                    
                    
                      Now I can just say towers = towers.
                      2:55
                    
                    
                      And, don't need this line.
                      2:59
                    
                    
                      You can only do this at the time
the object is being constructed.
                      3:02
                    
                    
                      This only works with properties.
                      3:06
                    
                    
                      If we had other properties, we could list
them here, and separate them by commas.
                      3:08
                    
                    
                      Also, there's no need for
a semi-colon here.
                      3:13
                    
                    
                      Now we can call play.
                      3:17
                    
                    
                      Play returns true, if the player won.
                      3:19
                    
                    
                      So, we'll store that in a boolean
variable called playerWon.
                      3:21
                    
                    
                      Finally, let's print the game's
outcome to the screen.
                      3:30
                    
                    
                      We could write an if else statement,
                      3:34
                    
                    
                      to print two different messages to the
screen, depending on if the player won.
                      3:36
                    
                    
                      Or, by using the ternary if operator, we
can do it all in the single line like so.
                      3:40
                    
                    
                      So Console.WriteLine first
will say player space
                      3:45
                    
                    
                      then will concatenate whether
the player won or lost.
                      3:51
                    
                    
                      So use the ternary if operator here.
                      3:57
                    
                    
                      We'll put it all in some parentheses.
                      3:59
                    
                    
                      So say if playerWon,
                      4:02
                    
                    
                      then we'll have it say won,
                      4:05
                    
                    
                      make that a lowercase w,
else will have it say lost.
                      4:10
                    
                    
                      When the program runs
                      4:16
                    
                    
                      the level will be created in
the play method will be executed.
                      4:20
                    
                    
                      The invaders will start
marching down the path, and
                      4:24
                    
                    
                      as they get close to the towers,
the towers will shoot at them.
                      4:27
                    
                    
                      When the play method returns, we'll know
if we placed enough towers at the right
                      4:31
                    
                    
                      places on the map, to stop the invaders
from getting to the end of the path.
                      4:35
                    
                    
                      Let's try it out.
                      4:39
                    
                    
                      First we'll need to compile and
run the game.
                      4:41
                    
                    
                      So at the end of our compile statement,
                      4:47
                    
                    
                      I'll type && mono TreehouseDefense.exe.
                      4:51
                    
                    
                      All right we won!
                      4:55
                    
              
        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