1
00:00:05,760 --> 00:00:07,760
ok so as well as adding extra behavior

2
00:00:07,760 --> 00:00:09,860
to our subclasses we can also change

3
00:00:09,860 --> 00:00:12,020
the behavior of methods that exists in

4
00:00:12,020 --> 00:00:14,630
the superclass so to see how to do that

5
00:00:14,630 --> 00:00:17,210
we need to create a new vampire enemy

6
00:00:17,210 --> 00:00:18,800
but i think that makes for a good

7
00:00:18,800 --> 00:00:22,250
challenge

8
00:00:22,250 --> 00:00:27,850
ok so the challenge for you is to create

9
00:00:27,850 --> 00:00:30,370
that new vampire class that's a subclass

10
00:00:30,370 --> 00:00:33,280
of enemy now vampires have three lives

11
00:00:33,280 --> 00:00:36,190
and take 12 hit points of damage you can

12
00:00:36,190 --> 00:00:37,790
create a new python file for the

13
00:00:37,790 --> 00:00:39,620
vampire if you like but I've actually

14
00:00:39,620 --> 00:00:41,950
suggest adding it to the existing enemy

15
00:00:41,950 --> 00:00:46,160
.py file

16
00:00:46,160 --> 00:00:48,710
you want to test your class by creating

17
00:00:48,710 --> 00:00:51,020
one or two vampire instances and

18
00:00:51,020 --> 00:00:53,840
displaying their details also inflict

19
00:00:53,840 --> 00:00:55,700
some damage to make sure that the

20
00:00:55,700 --> 00:00:58,220
take_damaged method is working ok

21
00:00:58,220 --> 00:01:01,010
and also make sure that the trolls can

22
00:01:01,010 --> 00:01:02,990
also take damage because we haven't

23
00:01:02,990 --> 00:01:04,580
tested that so far

24
00:01:04,580 --> 00:01:06,020
alright so that's a challenge see how

25
00:01:06,020 --> 00:01:08,170
you go with it pause the video and I'll

26
00:01:08,170 --> 00:01:09,620
see you when you get back

27
00:01:09,620 --> 00:01:15,740
ok so let's go through my solution so

28
00:01:15,740 --> 00:01:17,420
what I'm going to do is add the vampire

29
00:01:17,420 --> 00:01:19,760
class to the enemy.py

30
00:01:19,760 --> 00:01:24,620
so....

31
00:01:24,620 --> 00:01:30,200
...

32
00:01:30,200 --> 00:02:08,020
...

33
00:02:08,020 --> 00:02:09,770
now we're going to be coming back

34
00:02:09,770 --> 00:02:12,100
to add more features to our class that

35
00:02:12,100 --> 00:02:14,210
vampire class but for now that's all we

36
00:02:14,210 --> 00:02:15,590
need for the challenge

37
00:02:15,590 --> 00:02:17,630
let's test this I'm going to go

38
00:02:17,630 --> 00:02:20,300
back to the main .py file the

39
00:02:20,300 --> 00:02:21,850
first thing we need to do is import the

40
00:02:21,850 --> 00:02:24,740
vampire class into this file so I'm

41
00:02:24,740 --> 00:02:27,950
going to add a comma at the end...

42
00:02:27,950 --> 00:02:30,020
again the warning because it's

43
00:02:30,020 --> 00:02:31,490
unused at the moment unused import

44
00:02:31,490 --> 00:02:33,850
statement going to delete the monster

45
00:02:33,850 --> 00:02:35,510
references out but let's actually add a

46
00:02:35,510 --> 00:02:39,130
vampire....

47
00:02:39,130 --> 00:02:44,990
...

48
00:02:44,990 --> 00:02:51,620
...

49
00:02:51,620 --> 00:02:53,510
....so that's

50
00:02:53,510 --> 00:02:57,070
clearly working ok ok now because our

51
00:02:57,070 --> 00:02:58,940
trolls and vampires all inherit from

52
00:02:58,940 --> 00:03:01,100
enemy they have all the methods that the

53
00:03:01,100 --> 00:03:03,910
enemy class defines so we can inflict

54
00:03:03,910 --> 00:03:06,620
damage on both trolls and vampires by

55
00:03:06,620 --> 00:03:09,800
calling the take_damaged method so

56
00:03:09,800 --> 00:03:12,380
let's go add some damage to the

57
00:03:12,380 --> 00:03:17,060
second troll...

58
00:03:17,060 --> 00:03:23,060
...

59
00:03:23,060 --> 00:03:34,570
...

60
00:03:34,570 --> 00:03:38,320
...so let's just confirm this

61
00:03:38,320 --> 00:03:42,820
will run it scrolling up we can see I

62
00:03:42,820 --> 00:03:45,050
took 18 points of damage have five left

63
00:03:45,050 --> 00:03:48,250
the message that was for ugh and ugh

64
00:03:48,250 --> 00:03:51,470
has now no more live one-hit points5 and

65
00:03:51,470 --> 00:03:53,270
scrolling down that took 5 points

66
00:03:53,270 --> 00:03:55,720
damages got seven left and you can see

67
00:03:55,720 --> 00:03:59,780
the confirmation of that live 3.7 but we

68
00:03:59,780 --> 00:04:01,910
should really test the other path

69
00:04:01,910 --> 00:04:03,440
through the take damage

70
00:04:03,440 --> 00:04:05,630
let's give ugh a hefty thrak with the

71
00:04:05,630 --> 00:04:08,150
sword glandarin and inflict 30

72
00:04:08,150 --> 00:04:10,060
points of damage and go back and do that

73
00:04:10,060 --> 00:04:14,730
will separate this will do...

74
00:04:14,730 --> 00:04:18,120
...

75
00:04:18,120 --> 00:04:27,660
...

76
00:04:27,660 --> 00:04:28,980
...i'm going to

77
00:04:28,980 --> 00:04:31,650
switch this to floating mode so that it

78
00:04:31,650 --> 00:04:32,970
doesn't affect the subtitles at the

79
00:04:32,970 --> 00:04:34,920
bottom of the screen then click on that

80
00:04:34,920 --> 00:04:36,600
that's going to put the run window up a

81
00:04:36,600 --> 00:04:38,580
little bit higher still be down a little

82
00:04:38,580 --> 00:04:40,410
bit more as well but you can also

83
00:04:40,410 --> 00:04:44,430
hide it when running it actually

84
00:04:44,430 --> 00:04:48,060
going to close it for now and run again we

85
00:04:48,060 --> 00:04:52,590
can see that ugh is now live 0 hit points5 so

86
00:04:52,590 --> 00:04:54,000
I'm still got a problem with hit points

87
00:04:54,000 --> 00:04:55,680
but you can see that ugh has lost a life

88
00:04:55,680 --> 00:04:57,630
here and we haven't got the message that

89
00:04:57,630 --> 00:04:59,760
appears when he's got zero or more hit

90
00:04:59,760 --> 00:05:01,590
points left so clearly this is something

91
00:05:01,590 --> 00:05:03,870
we need to fix close that down for now

92
00:05:03,870 --> 00:05:06,900
and go back to enemy.py

93
00:05:06,900 --> 00:05:08,340
really when a monsters got no lives

94
00:05:08,340 --> 00:05:10,800
left there dead at the moment you saw

95
00:05:10,800 --> 00:05:12,390
that we can keep fighting ugh

96
00:05:12,390 --> 00:05:15,270
had written a attack method it fought back

97
00:05:15,270 --> 00:05:18,060
that's not good but what is good is that

98
00:05:18,060 --> 00:05:19,560
we don't have to make changes to all

99
00:05:19,560 --> 00:05:20,840
our monsters

100
00:05:20,840 --> 00:05:22,680
okay we only got 2 at the moment but you

101
00:05:22,680 --> 00:05:24,390
could create 8 or 90 monsters in

102
00:05:24,390 --> 00:05:25,020
the game

103
00:05:25,020 --> 00:05:27,450
adding code to each class to track when the

104
00:05:27,450 --> 00:05:29,490
monsters dead would be a lot of work

105
00:05:29,490 --> 00:05:31,470
with that many enemies we also have to

106
00:05:31,470 --> 00:05:33,240
test the code for each monster to make

107
00:05:33,240 --> 00:05:36,180
sure we hadn't introduced to bug but

108
00:05:36,180 --> 00:05:38,880
getting back to our enemy class because

109
00:05:38,880 --> 00:05:40,280
all our monsters all subclasses the

110
00:05:40,280 --> 00:05:41,970
enemy class we can just add the

111
00:05:41,970 --> 00:05:43,920
necessary code to the enemy superclass

112
00:05:43,920 --> 00:05:45,720
and will be then automatically available

113
00:05:45,720 --> 00:05:49,080
in all subclasses so let's go ahead and

114
00:05:49,080 --> 00:05:51,000
make a change to start tracking when

115
00:05:51,000 --> 00:05:52,470
they reach their hit points where there

116
00:05:52,470 --> 00:05:54,330
should be dead and to do that want to

117
00:05:54,330 --> 00:05:57,630
start on here on line 8 by adding

118
00:05:57,630 --> 00:05:58,890
another attribute is going to be...

119
00:05:58,890 --> 00:06:04,340
....and what we

120
00:06:04,340 --> 00:06:05,880
want to do is have a look at this code

121
00:06:05,880 --> 00:06:09,300
in the take damage method and it's under

122
00:06:09,300 --> 00:06:11,640
this else where the life's self.lives

123
00:06:11,640 --> 00:06:14,030
is minus equal to 1 we need a

124
00:06:14,030 --> 00:06:16,770
test their the test is going to be...

125
00:06:16,770 --> 00:06:21,840
....

126
00:06:21,840 --> 00:06:57,060
....

127
00:06:57,060 --> 00:06:59,730
ok confirming that we've got a new

128
00:06:59,730 --> 00:07:01,650
attribute called alive that's

129
00:07:01,650 --> 00:07:03,840
initialized to true init method on line 8

130
00:07:03,840 --> 00:07:06,780
we then change the take_

131
00:07:06,780 --> 00:07:08,850
damage method so after losing a

132
00:07:08,850 --> 00:07:10,800
life we check to see if the life's left

133
00:07:10,800 --> 00:07:13,290
is still greater than 0 if the monster

134
00:07:13,290 --> 00:07:14,730
still has lives left

135
00:07:14,730 --> 00:07:15,930
we print a message saying they lost a

136
00:07:15,930 --> 00:07:18,240
life you can see that here on line 18

137
00:07:18,240 --> 00:07:21,000
and otherwise we set self.alive to

138
00:07:21,000 --> 00:07:23,040
false line 21

139
00:07:23,040 --> 00:07:24,510
that's because they're no longer alive

140
00:07:24,510 --> 00:07:28,650
because they reached 0 lives left

141
00:07:28,650 --> 00:07:30,060
let's confirm that this works

142
00:07:30,060 --> 00:07:32,070
so we're going to come down here and add

143
00:07:32,070 --> 00:07:36,810
some code let's add some code via a

144
00:07:36,810 --> 00:07:39,540
while loop that tests while the vampire is

145
00:07:39,540 --> 00:07:42,780
alive so...

146
00:07:42,780 --> 00:07:44,340
...

147
00:07:44,340 --> 00:07:54,150
...

148
00:07:54,150 --> 00:07:56,100
...basically going to keep

149
00:07:56,100 --> 00:07:58,500
taking one point of damage until all his

150
00:07:58,500 --> 00:08:00,660
life is overt in which case at that

151
00:08:00,660 --> 00:08:02,850
point the alive attribute will change

152
00:08:02,850 --> 00:08:05,010
from true to false the whille will exit

153
00:08:05,010 --> 00:08:06,780
and then the program should exit as well

154
00:08:06,780 --> 00:08:09,540
let's just run this to see if it works

155
00:08:09,540 --> 00:08:12,990
you can see there that we've got

156
00:08:12,990 --> 00:08:15,930
vlad take some damage there we got down

157
00:08:15,930 --> 00:08:19,680
to zero points left lost a life

158
00:08:19,680 --> 00:08:21,180
notice also hear he's lost another life

159
00:08:21,180 --> 00:08:23,160
and another life because hit points

160
00:08:23,160 --> 00:08:26,280
didn't get reset that's something we'll

161
00:08:26,280 --> 00:08:28,050
need to look at to reset the hit points

162
00:08:28,050 --> 00:08:30,690
back to a value when he has lost a life

163
00:08:30,690 --> 00:08:33,090
but he's still alive but you can see the

164
00:08:33,090 --> 00:08:35,100
principle of continuing taking damage

165
00:08:35,100 --> 00:08:38,730
until he has died works the loop exit

166
00:08:38,730 --> 00:08:41,039
and once you got to 0 lives blood was

167
00:08:41,039 --> 00:08:41,789
dead

168
00:08:41,789 --> 00:08:44,970
and the program exited and also up here

169
00:08:44,970 --> 00:08:48,450
ugh was dead as well at that point and

170
00:08:48,450 --> 00:08:49,650
again you could argue that we should be

171
00:08:49,650 --> 00:08:51,090
putting the hit points back to their

172
00:08:51,090 --> 00:08:53,130
original values when the monster

173
00:08:53,130 --> 00:08:54,570
losses a life

174
00:08:54,570 --> 00:08:56,370
that's up to as the designer of the class

175
00:08:56,370 --> 00:08:58,380
and if makes more sense for the

176
00:08:58,380 --> 00:09:00,150
monsters to behave like that then of

177
00:09:00,150 --> 00:09:01,920
course you're the Creator you can do

178
00:09:01,920 --> 00:09:02,430
that

179
00:09:02,430 --> 00:09:03,810
alright so i'm going to stop the video

180
00:09:03,810 --> 00:09:07,950
here now we've seen how to give a

181
00:09:07,950 --> 00:09:10,500
subclass additional behavior when we

182
00:09:10,500 --> 00:09:12,060
added the grunt method to the troll

183
00:09:12,060 --> 00:09:13,800
class in the next video we're going to

184
00:09:13,800 --> 00:09:15,360
look at how to override methods of the

185
00:09:15,360 --> 00:09:17,550
superclass to change the way that a

186
00:09:17,550 --> 00:09:20,070
subclass behaves and what I'll do is I'll

187
00:09:20,070 --> 00:09:22,920
just run it one more time I'm just gonna

188
00:09:22,920 --> 00:09:25,980
take this out of floating mode putting

189
00:09:25,980 --> 00:09:27,450
that back their on the screen

190
00:09:27,450 --> 00:09:35,120
okay i'll see you in the next video

