1 00:00:00,270 --> 00:00:04,480 Let's take another look at a graphical representation of a map. 2 00:00:04,480 --> 00:00:10,010 We need to be able to specify where on the map a tower or an invader is located. 3 00:00:10,010 --> 00:00:13,517 We can think of our map as being divided up into a two-dimensional grid. 4 00:00:14,590 --> 00:00:18,700 Each grid square is a location where an object can be placed on the map. 5 00:00:19,730 --> 00:00:24,470 This allows us to use Cartesian coordinates to identify grid squares. 6 00:00:24,470 --> 00:00:28,467 Cartesian coordinates are used to identify a point on a two-dimensional grid. 7 00:00:29,600 --> 00:00:34,180 Traditionally variables named X and Y are used to specify a point. 8 00:00:35,410 --> 00:00:39,080 X is the distance from the left-most grid square. 9 00:00:39,080 --> 00:00:42,821 Y is the distance from the bottom-most grid square. 10 00:00:42,821 --> 00:00:48,561 A tower located at the bottom left corner of the map would be located at x=0, y=0. 11 00:00:48,561 --> 00:00:53,356 And this tower is placed at point x=3 and 12 00:00:53,356 --> 00:00:57,201 y=1 or point 3, 1 for short. 13 00:00:57,201 --> 00:00:59,741 Where is this tower located at? 14 00:00:59,741 --> 00:01:00,741 That's right. 15 00:01:00,741 --> 00:01:03,301 It's at point 5,4. 16 00:01:03,301 --> 00:01:07,480 So a point on the map has both an X and a Y coordinate. 17 00:01:07,480 --> 00:01:10,150 Let's model a point using a class. 18 00:01:10,150 --> 00:01:13,224 We'll need to create a new file called Point.cs. 19 00:01:15,304 --> 00:01:19,284 We'll define this class inside the Treehousedefense namespace. 20 00:01:24,764 --> 00:01:28,904 So our Point class is going to look very similar to our map class. 21 00:01:28,904 --> 00:01:33,480 It will have two fields, one called X and one called Y. 22 00:01:33,480 --> 00:01:35,800 Just like our Map class needs both a width and 23 00:01:35,800 --> 00:01:40,710 a height, the Point class will need both an X and a Y coordinate. 24 00:01:40,710 --> 00:01:44,680 You know everything you need to know to write the Point class. 25 00:01:44,680 --> 00:01:47,716 I suggest you pause the video here, and go ahead and 26 00:01:47,716 --> 00:01:49,701 do that on your own for practice. 27 00:01:49,701 --> 00:01:52,461 Then when you come back, we'll work through it together. 28 00:01:55,961 --> 00:01:58,401 All right, how do you think you did? 29 00:01:58,401 --> 00:01:59,881 Let's take a look. 30 00:01:59,881 --> 00:02:02,041 We'll make a class and call it Point. 31 00:02:04,801 --> 00:02:09,762 Then we'll have two public fields in our class, one for X, and one for Y. 32 00:02:09,762 --> 00:02:13,848 We'll make them both integers, and they'll both be public because we'll need 33 00:02:13,848 --> 00:02:16,560 to be able to read them from other classes. 34 00:02:16,560 --> 00:02:20,472 Points don't move, so we need to make both X and Y readonly. 35 00:02:25,132 --> 00:02:28,850 We'll need to add a constructor to initialize X and Y. 36 00:02:28,850 --> 00:02:32,890 It will need to be public and take an x and y parameter. 37 00:02:32,890 --> 00:02:36,882 We'll use these parameters to set the value of the X and Y fields. 38 00:02:45,243 --> 00:02:46,503 There we have it. 39 00:02:46,503 --> 00:02:52,620 As I said, the point class looks almost identical to the Map class. 40 00:02:52,620 --> 00:02:53,940 Only the names are different. 41 00:02:55,210 --> 00:02:56,960 What it means to be a point and 42 00:02:56,960 --> 00:03:00,280 what it means to be a map are still two very different things though. 43 00:03:01,520 --> 00:03:04,350 Remember to compile your code to make sure that you're not getting 44 00:03:04,350 --> 00:03:05,930 any compiler errors. 45 00:03:05,930 --> 00:03:09,380 If you do get compiler errors, check to make sure that your code looks like 46 00:03:09,380 --> 00:03:12,680 the code written in these videos and that all of your files are saved. 47 00:03:14,200 --> 00:03:15,270 Way to go. 48 00:03:15,270 --> 00:03:18,460 You're writing classes and you're making objects. 49 00:03:18,460 --> 00:03:22,422 Objects are a fun way to think about designing software, don't you think? 50 00:03:22,422 --> 00:03:27,355 As we learn more about the capabilities of C# to do object-oriented programming, 51 00:03:27,355 --> 00:03:30,282 it's going to get even more interesting and fun. 52 00:03:30,282 --> 00:03:32,880 We are off to a great start, but this is just the beginning. 53 00:03:34,170 --> 00:03:36,420 We've learned about classes and fields. 54 00:03:36,420 --> 00:03:39,530 Next we'll learn about methods and their role in objects.