1 00:00:00,420 --> 00:00:04,000 One place we can easily see object oriented programming in action 2 00:00:04,000 --> 00:00:05,450 is in video games. 3 00:00:05,450 --> 00:00:08,967 This is because many video games portray simulations of things 4 00:00:08,967 --> 00:00:10,966 that might exist in the real world. 5 00:00:10,966 --> 00:00:13,866 Take a tower defense game for example. 6 00:00:13,866 --> 00:00:16,466 There are lots of tower defense games out there. 7 00:00:16,466 --> 00:00:18,706 They all have a few things in common though. 8 00:00:18,706 --> 00:00:22,667 The player places towers on a map, invaders move down a path and 9 00:00:22,667 --> 00:00:25,230 the towers shoot at them as they pass by. 10 00:00:25,230 --> 00:00:29,447 The player wins if the towers destroy the invaders before they 11 00:00:29,447 --> 00:00:31,486 can reach the end of the path. 12 00:00:31,486 --> 00:00:35,233 In order to write this game, one of the first things we need to do is 13 00:00:35,233 --> 00:00:38,386 decide what types of objects the game should contain. 14 00:00:38,386 --> 00:00:42,566 In other words, we need to determine what classes we need to code up. 15 00:00:42,566 --> 00:00:47,162 For this, it's often a good idea to pay attention to the nouns that are used to 16 00:00:47,162 --> 00:00:48,586 describe the program. 17 00:00:48,586 --> 00:00:51,646 Let's take a look at the description of the game. 18 00:00:51,646 --> 00:00:56,353 The player places towers on a map, invaders move down a path, and 19 00:00:56,353 --> 00:00:59,276 the towers shoot at them as they pass by. 20 00:00:59,276 --> 00:01:03,282 The player wins if the towers destroy the invaders before they 21 00:01:03,282 --> 00:01:05,216 can reach the end of the path. 22 00:01:05,216 --> 00:01:06,396 Let's see here. 23 00:01:06,396 --> 00:01:11,173 We have the nouns, player, tower, 24 00:01:11,173 --> 00:01:14,416 map, invader and path. 25 00:01:14,416 --> 00:01:18,116 We can model each of these types of objects in code. 26 00:01:18,116 --> 00:01:20,096 Let's open work spaces and do it. 27 00:01:20,096 --> 00:01:24,996 To open work spaces, just click on the work spaces button on your screen. 28 00:01:24,996 --> 00:01:28,056 Let's start by creating a class for our tower. 29 00:01:28,056 --> 00:01:30,716 The first thing we need to do is create a file. 30 00:01:30,716 --> 00:01:33,856 We'll call it tower.cs. 31 00:01:33,856 --> 00:01:37,976 The code for this game will go in the treehouse defense namespace. 32 00:01:37,976 --> 00:01:41,409 If you're unsure about why I'm using a namespace here, 33 00:01:41,409 --> 00:01:46,170 I suggest checking out the teachers notes for a video about namespaces in C#. 34 00:01:46,170 --> 00:01:48,386 Now, we can declare a class called tower. 35 00:01:51,626 --> 00:01:53,546 Now, we have a tower class. 36 00:01:53,546 --> 00:01:56,186 Right now it's just an empty class though. 37 00:01:56,186 --> 00:02:00,817 Other than the name, there's nothing about this class that describes what a tower is, 38 00:02:00,817 --> 00:02:02,146 or how it should behave. 39 00:02:02,146 --> 00:02:05,086 We still need to add code to the class to do this. 40 00:02:05,086 --> 00:02:08,946 It's important to remember that the tower class is not a tower itself. 41 00:02:08,946 --> 00:02:11,986 It's just a template for creating tower objects. 42 00:02:11,986 --> 00:02:14,270 To create an actual tower object, 43 00:02:14,270 --> 00:02:18,993 we need to use this class to create an object of type tower just like we use 44 00:02:18,993 --> 00:02:22,966 the heart cookie cutter to create a heart-shaped cookie. 45 00:02:22,966 --> 00:02:24,826 Let's see how to do that now. 46 00:02:24,826 --> 00:02:30,706 You can see here, that I've already created a file called game.cs. 47 00:02:30,706 --> 00:02:33,886 It contains the definition of the game class. 48 00:02:33,886 --> 00:02:36,426 It also contain our main method. 49 00:02:36,426 --> 00:02:40,760 Remember the main method is a specially named method that will be run first when 50 00:02:40,760 --> 00:02:42,660 our program starts. 51 00:02:42,660 --> 00:02:45,976 This is where we'll create our first tower object. 52 00:02:45,976 --> 00:02:49,656 We can do this by first saying what type of an object it is. 53 00:02:49,656 --> 00:02:53,776 So, Tower then give the object a name. 54 00:02:53,776 --> 00:02:57,437 We'll use tower with the lowercase t and 55 00:02:57,437 --> 00:03:03,650 then equals new Tower followed by open and closing parentheses. 56 00:03:03,650 --> 00:03:07,590 Remember, we end all statements in C# with a semicolon. 57 00:03:07,590 --> 00:03:12,798 This creates a new variable called tower with a lowercase t that 58 00:03:12,798 --> 00:03:19,392 is of type Tower with an uppercase T and assigns it a newly created Tower object. 59 00:03:19,392 --> 00:03:25,230 In C# the convention is to name classes starting with a capital letter. 60 00:03:25,230 --> 00:03:28,360 This is handy when creating objects because then we 61 00:03:28,360 --> 00:03:33,490 can just name them the same as their types except the first letter is in lowercase. 62 00:03:33,490 --> 00:03:35,440 We could have named this anything. 63 00:03:35,440 --> 00:03:39,202 We could have named it Bob except that that wouldn't make a lot of sense. 64 00:03:39,202 --> 00:03:41,822 But we wanna give our variables meaningful names. 65 00:03:41,822 --> 00:03:46,062 For now, tower with a lower case t will do. 66 00:03:46,062 --> 00:03:48,202 Let’s review what we just did. 67 00:03:48,202 --> 00:03:53,506 We just used the tower cookie cutter to stamp out a new tower cookie, 68 00:03:53,506 --> 00:03:56,496 and that's how you create an object. 69 00:03:56,496 --> 00:04:01,656 Now you'll hear me and others use the terms object and instance interchangeably. 70 00:04:01,656 --> 00:04:07,716 By creating a tower object, we've just created an instance of the tower class. 71 00:04:07,716 --> 00:04:11,376 We could also call this a tower instance. 72 00:04:11,376 --> 00:04:17,876 In fact, the term for creating an object from a class is called instantiation. 73 00:04:17,876 --> 00:04:21,290 We just instantiated an object from the tower class. 74 00:04:22,580 --> 00:04:25,150 Sometimes you might think that all of this 75 00:04:25,150 --> 00:04:29,750 redundant terminology is just there to confuse you and maybe it is. 76 00:04:29,750 --> 00:04:31,130 But even so, 77 00:04:31,130 --> 00:04:35,970 it's good to be able to recognize these terms when you hear or read them. 78 00:04:35,970 --> 00:04:39,483 Just remember that an object is an instance of a class and 79 00:04:39,483 --> 00:04:43,316 you can use the terms object and instance interchangeably. 80 00:04:43,316 --> 00:04:46,536 Let's go ahead and create the rest of our classes. 81 00:04:46,536 --> 00:04:48,536 We'll need to have one for Invader. 82 00:04:48,536 --> 00:04:52,276 We'll create a new file called Invader.cs. 83 00:04:52,276 --> 00:04:56,016 To save on typing, I'll just copy the code from Tower.cs. 84 00:04:59,956 --> 00:05:03,216 Paste it here and then change the name of the class to Invader. 85 00:05:07,856 --> 00:05:10,956 Let's make another one for map in map.cs 86 00:05:21,356 --> 00:05:23,156 We'll also make one for path. 87 00:05:34,096 --> 00:05:36,544 As you can see, we've created classes for 88 00:05:36,544 --> 00:05:39,676 all of the nouns in our description except for player. 89 00:05:39,676 --> 00:05:44,376 For now, let's hold off on creating a class for the player. 90 00:05:44,376 --> 00:05:49,416 We now have five files in our project, one for each of the classes we've created. 91 00:05:49,416 --> 00:05:53,056 You don't have to create a separate file for each class. 92 00:05:53,056 --> 00:05:56,436 You also don't need to name them the same as the class. 93 00:05:56,436 --> 00:05:59,476 This is just the convention that most people use. 94 00:05:59,476 --> 00:06:02,983 You'll notice that a lot of the way that we name things and 95 00:06:02,983 --> 00:06:06,576 organize things in a C# project are based on convention. 96 00:06:06,576 --> 00:06:09,456 Conventions are not hard and set rules. 97 00:06:09,456 --> 00:06:14,153 They're just things that developers have come to a general consensus to do in 98 00:06:14,153 --> 00:06:17,056 order to make the code easier to read and to use. 99 00:06:17,056 --> 00:06:21,596 Using conventions helps make the code look more professionally done. 100 00:06:21,596 --> 00:06:25,236 What we've just done is create a scaffold for a game project. 101 00:06:25,236 --> 00:06:27,820 Now we just need to fill out the class definitions.