WEBVTT 1 00:00:01.760 --> 00:00:04.640 ok so as well as adding extra behavior 2 00:00:04.640 --> 00:00:06.740 to our subclasses we can also change ok so as well as adding extra behavior 3 00:00:06.740 --> 00:00:08.900 the behavior of methods that exists in to our subclasses we can also change 4 00:00:08.900 --> 00:00:11.510 the superclass so to see how to do that the behavior of methods that exists in 5 00:00:11.510 --> 00:00:14.090 we need to create a new vampire enemy the superclass so to see how to do that 6 00:00:14.090 --> 00:00:15.680 but i think that makes for a good we need to create a new vampire enemy 7 00:00:15.680 --> 00:00:19.130 challenge but i think that makes for a good 8 00:00:19.130 --> 00:00:24.739 ok so the challenge for you is to create challenge 9 00:00:24.739 --> 00:00:27.259 that new vampire class that's a subclass ok so the challenge for you is to create 10 00:00:27.259 --> 00:00:30.169 of enemy now vampires have three lives that new vampire class that's a subclass 11 00:00:30.169 --> 00:00:33.079 and take 12 hit points of damage you can of enemy now vampires have three lives 12 00:00:33.079 --> 00:00:34.670 create a new python file for the and take 12 hit points of damage you can 13 00:00:34.670 --> 00:00:36.500 vampire if you like but I've actually create a new python file for the 14 00:00:36.500 --> 00:00:38.839 suggest adding it to the existing enemy vampire if you like but I've actually 15 00:00:38.839 --> 00:00:43.040 .py file suggest adding it to the existing enemy 16 00:00:43.040 --> 00:00:45.590 you want to test your class by creating .py file 17 00:00:45.590 --> 00:00:47.900 one or two vampire instances and you want to test your class by creating 18 00:00:47.900 --> 00:00:50.720 displaying their details also inflict one or two vampire instances and 19 00:00:50.720 --> 00:00:52.580 some damage to make sure that the displaying their details also inflict 20 00:00:52.580 --> 00:00:55.100 take_damaged method is working ok some damage to make sure that the 21 00:00:55.100 --> 00:00:57.890 and also make sure that the trolls can take_damaged method is working ok 22 00:00:57.890 --> 00:00:59.870 also take damage because we haven't and also make sure that the trolls can 23 00:00:59.870 --> 00:01:01.460 tested that so far also take damage because we haven't 24 00:01:01.460 --> 00:01:02.900 alright so that's a challenge see how tested that so far 25 00:01:02.900 --> 00:01:05.059 you go with it pause the video and I'll alright so that's a challenge see how 26 00:01:05.059 --> 00:01:06.500 see you when you get back you go with it pause the video and I'll 27 00:01:06.500 --> 00:01:12.620 ok so let's go through my solution so see you when you get back 28 00:01:12.620 --> 00:01:14.300 what I'm going to do is add the vampire ok so let's go through my solution so 29 00:01:14.300 --> 00:01:16.640 class to the enemy.py what I'm going to do is add the vampire 30 00:01:16.640 --> 00:01:21.500 so.... class to the enemy.py 31 00:01:21.500 --> 00:01:27.080 ... so.... 32 00:01:27.080 --> 00:02:04.909 ... ... 33 00:02:04.909 --> 00:02:06.650 now we're going to be coming back ... 34 00:02:06.650 --> 00:02:08.989 to add more features to our class that now we're going to be coming back 35 00:02:08.989 --> 00:02:11.090 vampire class but for now that's all we to add more features to our class that 36 00:02:11.090 --> 00:02:12.470 need for the challenge vampire class but for now that's all we 37 00:02:12.470 --> 00:02:14.510 let's test this I'm going to go need for the challenge 38 00:02:14.510 --> 00:02:17.180 back to the main .py file the let's test this I'm going to go 39 00:02:17.180 --> 00:02:18.739 first thing we need to do is import the back to the main .py file the 40 00:02:18.739 --> 00:02:21.620 vampire class into this file so I'm first thing we need to do is import the 41 00:02:21.620 --> 00:02:24.830 going to add a comma at the end... vampire class into this file so I'm 42 00:02:24.830 --> 00:02:26.900 again the warning because it's going to add a comma at the end... 43 00:02:26.900 --> 00:02:28.370 unused at the moment unused import again the warning because it's 44 00:02:28.370 --> 00:02:30.739 statement going to delete the monster unused at the moment unused import 45 00:02:30.739 --> 00:02:32.390 references out but let's actually add a statement going to delete the monster 46 00:02:32.390 --> 00:02:36.019 vampire.... references out but let's actually add a 47 00:02:36.019 --> 00:02:41.870 ... vampire.... 48 00:02:41.870 --> 00:02:48.500 ... ... 49 00:02:48.500 --> 00:02:50.390 ....so that's ... 50 00:02:50.390 --> 00:02:53.959 clearly working ok ok now because our ....so that's 51 00:02:53.959 --> 00:02:55.820 trolls and vampires all inherit from clearly working ok ok now because our 52 00:02:55.820 --> 00:02:57.980 enemy they have all the methods that the trolls and vampires all inherit from 53 00:02:57.980 --> 00:03:00.799 enemy class defines so we can inflict enemy they have all the methods that the 54 00:03:00.799 --> 00:03:03.500 damage on both trolls and vampires by enemy class defines so we can inflict 55 00:03:03.500 --> 00:03:06.680 calling the take_damaged method so damage on both trolls and vampires by 56 00:03:06.680 --> 00:03:09.260 let's go add some damage to the calling the take_damaged method so 57 00:03:09.260 --> 00:03:13.940 second troll... let's go add some damage to the 58 00:03:13.940 --> 00:03:19.940 ... second troll... 59 00:03:19.940 --> 00:03:31.459 ... ... 60 00:03:31.459 --> 00:03:35.209 ...so let's just confirm this ... 61 00:03:35.209 --> 00:03:39.709 will run it scrolling up we can see I ...so let's just confirm this 62 00:03:39.709 --> 00:03:41.930 took 18 points of damage have five left will run it scrolling up we can see I 63 00:03:41.930 --> 00:03:45.139 the message that was for ugh and ugh took 18 points of damage have five left 64 00:03:45.139 --> 00:03:48.350 has now no more live one-hit points5 and the message that was for ugh and ugh 65 00:03:48.350 --> 00:03:50.150 scrolling down that took 5 points has now no more live one-hit points5 and 66 00:03:50.150 --> 00:03:52.609 damages got seven left and you can see scrolling down that took 5 points 67 00:03:52.609 --> 00:03:56.660 the confirmation of that live 3.7 but we damages got seven left and you can see 68 00:03:56.660 --> 00:03:58.790 should really test the other path the confirmation of that live 3.7 but we 69 00:03:58.790 --> 00:04:00.320 through the take damage should really test the other path 70 00:04:00.320 --> 00:04:02.510 let's give ugh a hefty thrak with the through the take damage 71 00:04:02.510 --> 00:04:05.030 sword glandarin and inflict 30 let's give ugh a hefty thrak with the 72 00:04:05.030 --> 00:04:06.949 points of damage and go back and do that sword glandarin and inflict 30 73 00:04:06.949 --> 00:04:11.610 will separate this will do... points of damage and go back and do that 74 00:04:11.610 --> 00:04:15.000 ... will separate this will do... 75 00:04:15.000 --> 00:04:24.540 ... ... 76 00:04:24.540 --> 00:04:25.860 ...i'm going to ... 77 00:04:25.860 --> 00:04:28.530 switch this to floating mode so that it ...i'm going to 78 00:04:28.530 --> 00:04:29.850 doesn't affect the subtitles at the switch this to floating mode so that it 79 00:04:29.850 --> 00:04:31.800 bottom of the screen then click on that doesn't affect the subtitles at the 80 00:04:31.800 --> 00:04:33.480 that's going to put the run window up a bottom of the screen then click on that 81 00:04:33.480 --> 00:04:35.460 little bit higher still be down a little that's going to put the run window up a 82 00:04:35.460 --> 00:04:37.290 bit more as well but you can also little bit higher still be down a little 83 00:04:37.290 --> 00:04:41.310 hide it when running it actually bit more as well but you can also 84 00:04:41.310 --> 00:04:44.940 going to close it for now and run again we hide it when running it actually 85 00:04:44.940 --> 00:04:49.470 can see that ugh is now live 0 hit points5 so going to close it for now and run again we 86 00:04:49.470 --> 00:04:50.880 I'm still got a problem with hit points can see that ugh is now live 0 hit points5 so 87 00:04:50.880 --> 00:04:52.560 but you can see that ugh has lost a life I'm still got a problem with hit points 88 00:04:52.560 --> 00:04:54.510 here and we haven't got the message that but you can see that ugh has lost a life 89 00:04:54.510 --> 00:04:56.640 appears when he's got zero or more hit here and we haven't got the message that 90 00:04:56.640 --> 00:04:58.470 points left so clearly this is something appears when he's got zero or more hit 91 00:04:58.470 --> 00:05:00.750 we need to fix close that down for now points left so clearly this is something 92 00:05:00.750 --> 00:05:03.780 and go back to enemy.py we need to fix close that down for now 93 00:05:03.780 --> 00:05:05.220 really when a monsters got no lives and go back to enemy.py 94 00:05:05.220 --> 00:05:07.680 left there dead at the moment you saw really when a monsters got no lives 95 00:05:07.680 --> 00:05:09.270 that we can keep fighting ugh left there dead at the moment you saw 96 00:05:09.270 --> 00:05:12.150 had written a attack method it fought back that we can keep fighting ugh 97 00:05:12.150 --> 00:05:14.940 that's not good but what is good is that had written a attack method it fought back 98 00:05:14.940 --> 00:05:16.440 we don't have to make changes to all that's not good but what is good is that 99 00:05:16.440 --> 00:05:17.729 our monsters we don't have to make changes to all 100 00:05:17.729 --> 00:05:19.560 okay we only got 2 at the moment but you our monsters 101 00:05:19.560 --> 00:05:21.270 could create 8 or 90 monsters in okay we only got 2 at the moment but you 102 00:05:21.270 --> 00:05:21.900 the game could create 8 or 90 monsters in 103 00:05:21.900 --> 00:05:24.330 adding code to each class to track when the the game 104 00:05:24.330 --> 00:05:26.370 monsters dead would be a lot of work adding code to each class to track when the 105 00:05:26.370 --> 00:05:28.350 with that many enemies we also have to monsters dead would be a lot of work 106 00:05:28.350 --> 00:05:30.120 test the code for each monster to make with that many enemies we also have to 107 00:05:30.120 --> 00:05:33.060 sure we hadn't introduced to bug but test the code for each monster to make 108 00:05:33.060 --> 00:05:35.760 getting back to our enemy class because sure we hadn't introduced to bug but 109 00:05:35.760 --> 00:05:37.169 all our monsters all subclasses the getting back to our enemy class because 110 00:05:37.169 --> 00:05:38.850 enemy class we can just add the all our monsters all subclasses the 111 00:05:38.850 --> 00:05:40.800 necessary code to the enemy superclass enemy class we can just add the 112 00:05:40.800 --> 00:05:42.600 and will be then automatically available necessary code to the enemy superclass 113 00:05:42.600 --> 00:05:45.960 in all subclasses so let's go ahead and and will be then automatically available 114 00:05:45.960 --> 00:05:47.880 make a change to start tracking when in all subclasses so let's go ahead and 115 00:05:47.880 --> 00:05:49.350 they reach their hit points where there make a change to start tracking when 116 00:05:49.350 --> 00:05:51.210 should be dead and to do that want to they reach their hit points where there 117 00:05:51.210 --> 00:05:54.510 start on here on line 8 by adding should be dead and to do that want to 118 00:05:54.510 --> 00:05:55.770 another attribute is going to be... start on here on line 8 by adding 119 00:05:55.770 --> 00:06:01.229 ....and what we another attribute is going to be... 120 00:06:01.229 --> 00:06:02.760 want to do is have a look at this code ....and what we 121 00:06:02.760 --> 00:06:06.180 in the take damage method and it's under want to do is have a look at this code 122 00:06:06.180 --> 00:06:08.520 this else where the life's self.lives in the take damage method and it's under 123 00:06:08.520 --> 00:06:10.919 is minus equal to 1 we need a this else where the life's self.lives 124 00:06:10.919 --> 00:06:13.650 test their the test is going to be... is minus equal to 1 we need a 125 00:06:13.650 --> 00:06:18.720 .... test their the test is going to be... 126 00:06:18.720 --> 00:06:53.940 .... .... 127 00:06:53.940 --> 00:06:56.610 ok confirming that we've got a new .... 128 00:06:56.610 --> 00:06:58.530 attribute called alive that's ok confirming that we've got a new 129 00:06:58.530 --> 00:07:00.720 initialized to true init method on line 8 attribute called alive that's 130 00:07:00.720 --> 00:07:03.660 we then change the take_ initialized to true init method on line 8 131 00:07:03.660 --> 00:07:05.730 damage method so after losing a we then change the take_ 132 00:07:05.730 --> 00:07:07.680 life we check to see if the life's left damage method so after losing a 133 00:07:07.680 --> 00:07:10.170 is still greater than 0 if the monster life we check to see if the life's left 134 00:07:10.170 --> 00:07:11.610 still has lives left is still greater than 0 if the monster 135 00:07:11.610 --> 00:07:12.810 we print a message saying they lost a still has lives left 136 00:07:12.810 --> 00:07:15.120 life you can see that here on line 18 we print a message saying they lost a 137 00:07:15.120 --> 00:07:17.880 and otherwise we set self.alive to life you can see that here on line 18 138 00:07:17.880 --> 00:07:19.920 false line 21 and otherwise we set self.alive to 139 00:07:19.920 --> 00:07:21.390 that's because they're no longer alive false line 21 140 00:07:21.390 --> 00:07:25.530 because they reached 0 lives left that's because they're no longer alive 141 00:07:25.530 --> 00:07:26.940 let's confirm that this works because they reached 0 lives left 142 00:07:26.940 --> 00:07:28.950 so we're going to come down here and add let's confirm that this works 143 00:07:28.950 --> 00:07:33.690 some code let's add some code via a so we're going to come down here and add 144 00:07:33.690 --> 00:07:36.420 while loop that tests while the vampire is some code let's add some code via a 145 00:07:36.420 --> 00:07:39.660 alive so... while loop that tests while the vampire is 146 00:07:39.660 --> 00:07:41.220 ... alive so... 147 00:07:41.220 --> 00:07:51.030 ... ... 148 00:07:51.030 --> 00:07:52.980 ...basically going to keep ... 149 00:07:52.980 --> 00:07:55.380 taking one point of damage until all his ...basically going to keep 150 00:07:55.380 --> 00:07:57.540 life is overt in which case at that taking one point of damage until all his 151 00:07:57.540 --> 00:07:59.730 point the alive attribute will change life is overt in which case at that 152 00:07:59.730 --> 00:08:01.890 from true to false the whille will exit point the alive attribute will change 153 00:08:01.890 --> 00:08:03.660 and then the program should exit as well from true to false the whille will exit 154 00:08:03.660 --> 00:08:06.420 let's just run this to see if it works and then the program should exit as well 155 00:08:06.420 --> 00:08:09.870 you can see there that we've got let's just run this to see if it works 156 00:08:09.870 --> 00:08:12.810 vlad take some damage there we got down you can see there that we've got 157 00:08:12.810 --> 00:08:16.560 to zero points left lost a life vlad take some damage there we got down 158 00:08:16.560 --> 00:08:18.060 notice also hear he's lost another life to zero points left lost a life 159 00:08:18.060 --> 00:08:20.040 and another life because hit points notice also hear he's lost another life 160 00:08:20.040 --> 00:08:23.160 didn't get reset that's something we'll and another life because hit points 161 00:08:23.160 --> 00:08:24.930 need to look at to reset the hit points didn't get reset that's something we'll 162 00:08:24.930 --> 00:08:27.570 back to a value when he has lost a life need to look at to reset the hit points 163 00:08:27.570 --> 00:08:29.970 but he's still alive but you can see the back to a value when he has lost a life 164 00:08:29.970 --> 00:08:31.980 principle of continuing taking damage but he's still alive but you can see the 165 00:08:31.980 --> 00:08:35.610 until he has died works the loop exit principle of continuing taking damage 166 00:08:35.610 --> 00:08:37.920 and once you got to 0 lives blood was until he has died works the loop exit 167 00:08:37.920 --> 00:08:38.670 dead and once you got to 0 lives blood was 168 00:08:38.670 --> 00:08:41.850 and the program exited and also up here dead 169 00:08:41.850 --> 00:08:45.330 ugh was dead as well at that point and and the program exited and also up here 170 00:08:45.330 --> 00:08:46.530 again you could argue that we should be ugh was dead as well at that point and 171 00:08:46.530 --> 00:08:47.970 putting the hit points back to their again you could argue that we should be 172 00:08:47.970 --> 00:08:50.010 original values when the monster putting the hit points back to their 173 00:08:50.010 --> 00:08:51.450 losses a life original values when the monster 174 00:08:51.450 --> 00:08:53.250 that's up to as the designer of the class losses a life 175 00:08:53.250 --> 00:08:55.260 and if makes more sense for the that's up to as the designer of the class 176 00:08:55.260 --> 00:08:57.030 monsters to behave like that then of and if makes more sense for the 177 00:08:57.030 --> 00:08:58.800 course you're the Creator you can do monsters to behave like that then of 178 00:08:58.800 --> 00:08:59.310 that course you're the Creator you can do 179 00:08:59.310 --> 00:09:00.690 alright so i'm going to stop the video that 180 00:09:00.690 --> 00:09:04.830 here now we've seen how to give a alright so i'm going to stop the video 181 00:09:04.830 --> 00:09:07.380 subclass additional behavior when we here now we've seen how to give a 182 00:09:07.380 --> 00:09:08.940 added the grunt method to the troll subclass additional behavior when we 183 00:09:08.940 --> 00:09:10.680 class in the next video we're going to added the grunt method to the troll 184 00:09:10.680 --> 00:09:12.240 look at how to override methods of the class in the next video we're going to 185 00:09:12.240 --> 00:09:14.430 superclass to change the way that a look at how to override methods of the 186 00:09:14.430 --> 00:09:16.950 subclass behaves and what I'll do is I'll superclass to change the way that a 187 00:09:16.950 --> 00:09:19.800 just run it one more time I'm just gonna subclass behaves and what I'll do is I'll 188 00:09:19.800 --> 00:09:22.860 take this out of floating mode putting just run it one more time I'm just gonna 189 00:09:22.860 --> 00:09:24.330 that back their on the screen take this out of floating mode putting 190 00:09:24.330 --> 00:09:32.000 okay i'll see you in the next video that back their on the screen