Static Members4:39 with Jeremy McLain
Static members are shared by all instances of the class.
Since we know how many invaders there are and 0:00 how much health each of them has, the outcome of the game is fairly predictable. 0:02 There's lots of things we can do to make the game more interesting. 0:08 For instance, we could break the game up into multiple rounds or 0:12 limit how many towers the player can place. 0:16 Or require the player to pay for 0:18 towers using resources that they acquire while playing the game. 0:20 These are all common elements in tower defense games. 0:24 One thing that nearly all games have, 0:28 including tower defense games, is uncertainty. 0:30 Uncertainty means that even if you were to play the game the exact same way 0:33 every time, you might get different outcomes. 0:38 One way we can add uncertainty to treehouse defense 0:41 is by making it possible for the towers to miss their target. 0:44 Right now, if a tower shoots an invader, it's always successful. 0:48 This makes the outcome of the game rather predictable. 0:53 We can make it possible for the towers to occasionally miss. 0:56 The easiest way to create uncertainty is to use a random number generator. 1:01 A simple random number generator produces a randomly created number 1:06 every time it's asked for one. 1:10 The .NET framework contains a class named random 1:13 that provides a random number generator. 1:16 Let's use the random class to make our game less predictable. 1:19 To get an idea of how the random class provided by the .NET framework works, 1:23 let's open the C# repo. 1:28 First, we need to construct an object of type random. 1:30 Random is in the system namespace, so we'll type System.Random, 1:33 give it a name equals new, 1:38 System.Random. 1:42 Now that we have a random number generator object, we can ask it for random numbers. 1:46 We can do this by calling the next method. 1:51 This gives a new random integer. 1:55 Another handy method is NextDouble. 1:58 NextDouble returns a random decimal number that's between zero and one. 2:00 Notice that every time these methods are called, they return a different number. 2:07 The random number generator provided by the .NET framework 2:13 is actually a pseudo random number generator. 2:16 If you'd like to know more about what that means, 2:19 then check out the teacher's notes for a link to the random classes documentation. 2:21 For what we want, the random class is more than sufficient. 2:26 Using random, we're going to make it possible for 2:31 a tower to occasionally miss its target. 2:33 Each tower object will need to have access to an instance of the random class. 2:36 We could instantiate the random class for each tower object. 2:41 This is unnecessary though. 2:45 The towers actually just need to share a single random object. 2:47 They'll all call the random's next method, and each time they'll get a random number. 2:52 There's a way to allow all objects of a class to share the same objects. 2:57 We can make the random object a static field of the tower class. 3:03 We've used static members before. 3:08 We used the static square root and power methods from the math class. 3:10 We also used the static WriteLine method in the tower class. 3:15 Remember, to use a static method, we don't have to have an instance of the class. 3:18 We call them on the class itself. 3:23 We can also have static fields and properties. 3:27 Let's add a static field to the tower class for the random number generator and 3:30 see what that looks like. 3:35 Static members of a class can have any accessibility level. 3:37 Math.squareroot is a public method so that any other class can use it. 3:40 Only the tower class needs access to this, so we'll make it private. 3:45 Next, and this is what makes the field static. 3:50 We'll type the static keyword. 3:52 We'll also need to make this field read only since we don't want 3:55 this field to ever be overridden. 3:58 Now, we'll declare this field just like any other field and 4:00 initialize it at the same time. 4:03 Because we made this field static, 4:10 there can only be one of these no matter how many towers are created. 4:12 In contrast, location is not static and 4:17 every tower object has its own location on the map. 4:20 Which can be different for every tower object. 4:24 So, random is initialized once and only once. 4:27 And it's shared by all the other tower objects. 4:30 In the next video, 4:33 we'll see how to use random to add some uncertainty to our game. 4:34
You need to sign up for Treehouse in order to download course files.Sign up