1
00:00:08,360 --> 00:00:11,440
Remember I broke this lecture into two
parts.

2
00:00:11,440 --> 00:00:16,550
So this is the start of part two for the
lecture I started last time.

3
00:00:16,550 --> 00:00:20,670
Okay, but at this point, I didn't really
have anything

4
00:00:20,670 --> 00:00:23,320
to launch my chickens at, and these are
ranged weapons.

5
00:00:23,320 --> 00:00:25,170
I mean, you gotta shoot 'em at something.

6
00:00:25,170 --> 00:00:28,790
And of course, teddy bears are the
something that we should shoot them at.

7
00:00:28,790 --> 00:00:33,700
So the next thing I wanted to do, was to
actually start spawning teddy bears.

8
00:00:33,700 --> 00:00:36,780
To do that, here in the update method,

9
00:00:41,510 --> 00:00:47,550
I implemented the timer stuff that we have
seen numerous times already.

10
00:00:48,730 --> 00:00:52,730
Now a couple of comments about what I was

11
00:00:52,730 --> 00:00:55,640
thinking about and doing as I went through
this code.

12
00:00:55,640 --> 00:00:58,960
First of all, I added a new constructor to
the teddy bear class.

13
00:00:58,960 --> 00:01:04,910
I didn't have one that I could pass in the
sprite, the texture 2D and the velocity.

14
00:01:04,910 --> 00:01:06,630
I had one that I could I pass in a content

15
00:01:06,630 --> 00:01:10,600
manager and a sprite name and a velocity,
but that isn't what I wanted to do.

16
00:01:10,600 --> 00:01:12,640
So I added a new constuctor.

17
00:01:12,640 --> 00:01:17,550
When I did that, I felt horribly guilty,
because if you go look at

18
00:01:17,550 --> 00:01:22,250
that code, there are four constructors
that have lots of duplicated code in them.

19
00:01:22,250 --> 00:01:24,990
And as we learned over the previous

20
00:01:24,990 --> 00:01:28,150
lessons in this module, there's a better
way.

21
00:01:28,150 --> 00:01:31,900
And in fact, it was in the die class that
we discovered

22
00:01:31,900 --> 00:01:34,320
that there's a better way to consolidate

23
00:01:34,320 --> 00:01:37,030
that code, even when we're dealing with
constructors.

24
00:01:37,030 --> 00:01:40,780
We should have the constructors call each
other using this.

25
00:01:40,780 --> 00:01:43,290
I didn't do that, I feel horribly guilty.

26
00:01:43,290 --> 00:01:46,830
You should, you know, feel bad as well, as
you read that code.

27
00:01:46,830 --> 00:01:51,820
Those constructors should be cleaned up,
absolutely, I just didn't do it.

28
00:01:51,820 --> 00:01:55,660
The other thing that I did was, I turned
off teddy bear bouncing.

29
00:01:55,660 --> 00:01:56,910
I didn't want the teddy bears

30
00:01:56,910 --> 00:02:00,936
to be bouncing around in the screen
anymore, I just wanted them to be spawned.

31
00:02:00,936 --> 00:02:08,920
Okay, so because when I did the draw
method I drew all the game entities.

32
00:02:08,920 --> 00:02:13,490
And when I did the update method, I update
all the game entities.

33
00:02:13,490 --> 00:02:16,700
Now if I F5, I actually get teddy bears
spawned.

34
00:02:16,700 --> 00:02:18,840
And I'm very excited and I shoot rubber
chickens at them,

35
00:02:18,840 --> 00:02:21,580
and of course nothing happens because I
haven't coded that yet.

36
00:02:21,580 --> 00:02:22,190
And I'm

37
00:02:22,190 --> 00:02:25,410
sad again because, here are these teddies,
just begging to

38
00:02:25,410 --> 00:02:28,670
be hit with rubber chickens, and I can't
do it.

39
00:02:28,670 --> 00:02:29,270
Very sad.

40
00:02:29,270 --> 00:02:33,320
But we have completed the spawning teddy
bears step.

41
00:02:34,570 --> 00:02:38,320
We also need to, because teddy bears don't
bounce anymore, we

42
00:02:38,320 --> 00:02:41,990
need to deactivate the teddy bears when
they exit the window.

43
00:02:41,990 --> 00:02:44,900
So check for teddy bear leaving window.

44
00:02:44,900 --> 00:02:45,660
All in common,

45
00:02:48,300 --> 00:02:52,700
so now, if the collision rectangle for the
top of the teddy bear

46
00:02:52,700 --> 00:02:57,450
is greater than window height, that means
that teddy bear's below the window.

47
00:02:57,450 --> 00:03:01,020
And so I just de-activate it, and we'll
clean out that teddy's later.

48
00:03:01,020 --> 00:03:06,260
So, unlike the chicken, where we wanted to
just keep that chicken

49
00:03:06,260 --> 00:03:11,550
object around to save memory, this teddy
bear, there's no reason to save it around.

50
00:03:11,550 --> 00:03:13,600
We'll just deactivate it and, and

51
00:03:13,600 --> 00:03:14,160
clean it up.

52
00:03:15,450 --> 00:03:18,590
So, we've done the next step where I
deactivated

53
00:03:18,590 --> 00:03:22,620
teddy bears outside the window, and
cleaning out dead teddies

54
00:03:24,890 --> 00:03:29,730
is exactly the way we cleaned out dead
teddies before.

55
00:03:30,770 --> 00:03:35,680
So, you can in fact, the way you can know
that a

56
00:03:35,680 --> 00:03:39,340
teddy bear has actually been cleaned out,
is we will let one.

57
00:03:39,340 --> 00:03:44,430
This will be testing both the deactivate
the teddy, and removing

58
00:03:44,430 --> 00:03:50,070
inactive teddies.
So there we have removed that teddy bear.

59
00:03:52,470 --> 00:03:54,440
I have to believe, right, so, the teddy

60
00:03:54,440 --> 00:03:57,720
bears are actually dying because of their
timer.

61
00:03:57,720 --> 00:04:01,060
So that doesn't actually test going out
the bottom of the window.

62
00:04:01,060 --> 00:04:06,360
So we should go change how long a teddy
bear lives, so

63
00:04:06,360 --> 00:04:08,920
that it can get all the way to the bottom
of the window.

64
00:04:10,040 --> 00:04:12,670
So let's make teddy bears live you know,
15 seconds.

65
00:04:12,670 --> 00:04:15,900
It's very unlikely they'll actually
survive

66
00:04:15,900 --> 00:04:17,560
that long, especially once I can shoot

67
00:04:17,560 --> 00:04:18,254
rubber chickens.

68
00:04:18,254 --> 00:04:22,070
And let me, you can actually add break
points while

69
00:04:22,070 --> 00:04:23,930
the game is running if you're quick enough
about it.

70
00:04:25,030 --> 00:04:26,900
And so I've added it so that this teddy

71
00:04:26,900 --> 00:04:29,900
bear here, once he's left the window he
gets removed.

72
00:04:29,900 --> 00:04:34,040
So, we've tested that.
We've made sure that both the teddy bear

73
00:04:34,040 --> 00:04:40,980
going outside of the window deactivates
it, and that dead teddies are cleaned out.

74
00:04:40,980 --> 00:04:42,690
So let's change that teddy bear life back

75
00:04:42,690 --> 00:04:43,290
to five.

76
00:04:43,290 --> 00:04:47,130
Again, we regularly change our code as
we're

77
00:04:47,130 --> 00:04:50,990
testing, just to temporarily test out
certain things.

78
00:04:50,990 --> 00:04:52,410
So we've cleaned out the dead teddies.

79
00:04:54,260 --> 00:04:57,190
And you'll notice I've been doing this in
small steps.

80
00:04:57,190 --> 00:05:01,330
But those small steps are sort of in
larger chunks, if you will.

81
00:05:01,330 --> 00:05:03,699
So the first chunk was getting rubber
chickens working.

82
00:05:04,810 --> 00:05:07,680
And the second chunk was getting teddy
bears working.

83
00:05:07,680 --> 00:05:12,350
And now, the third chunk, this is not a
game development term

84
00:05:12,350 --> 00:05:14,775
by the way, this is just the way I'm
thinking of it.

85
00:05:14,775 --> 00:05:15,060
[LAUGH]

86
00:05:15,060 --> 00:05:20,320
So, the third thing is the interactions
between teddy bears and chickens.

87
00:05:21,680 --> 00:05:23,680
So this is the collision stuff.

88
00:05:23,680 --> 00:05:25,240
So we'll go back to game one.

89
00:05:25,240 --> 00:05:26,760
We'll go back into the update method.

90
00:05:26,760 --> 00:05:31,168
And this is absolutely the biggest piece
of code, as you can see here.

91
00:05:31,168 --> 00:05:36,410
So, this is, first of all, let me
uncomment it.

92
00:05:42,630 --> 00:05:44,590
So these are nested loops.

93
00:05:44,590 --> 00:05:47,700
I made a comment about nested loops,
though you hadn't seen any yet.

94
00:05:48,740 --> 00:05:50,470
And I talked about, you know, processing a

95
00:05:50,470 --> 00:05:53,530
two dimensional game board like
tic-tac-toe or something.

96
00:05:53,530 --> 00:05:55,610
And by the way, the code that was in

97
00:05:55,610 --> 00:05:59,450
the tic-tac-toe programming assignment now
you should absolutely be

98
00:05:59,450 --> 00:06:02,240
able to understand those nested four loops
that I

99
00:06:02,240 --> 00:06:05,510
gave to you, because we hadn't talked
about those yet.

100
00:06:05,510 --> 00:06:08,370
These are, in fact, nested four loops
nested

101
00:06:08,370 --> 00:06:11,584
loops anyway, although the inner one is a
four

102
00:06:11,584 --> 00:06:13,840
h loop and the outer one is a four loop.

103
00:06:13,840 --> 00:06:16,210
I actually had em both as for each loops,

104
00:06:16,210 --> 00:06:19,420
and then we'll see that I create a new
chicken.

105
00:06:19,420 --> 00:06:21,360
And when I added the chicken to my

106
00:06:21,360 --> 00:06:24,510
chickens collection right here, my code
blew up

107
00:06:24,510 --> 00:06:27,200
just as I warned you it would, because

108
00:06:27,200 --> 00:06:30,940
I was changing the collection inside for
each loop.

109
00:06:30,940 --> 00:06:33,490
So I changed this to a four loop, because
I

110
00:06:33,490 --> 00:06:36,890
might add a chicken.
So these are nested loops.

111
00:06:36,890 --> 00:06:39,190
For every chicken, we're going to take the

112
00:06:39,190 --> 00:06:42,280
first chicken and compare it to every
bear.

113
00:06:42,280 --> 00:06:45,960
Then we're going to take the second
chicken, and compare it to every bear.

114
00:06:45,960 --> 00:06:49,000
And then we're going to take the third
chicken, and compare it to every bear.

115
00:06:49,000 --> 00:06:51,390
You can see why this collision detection
thing can get

116
00:06:51,390 --> 00:06:55,780
pretty computationally intense if you have
a lot of game objects.

117
00:06:55,780 --> 00:06:59,060
Okay, this is standard stuff.
Both things are active and

118
00:06:59,060 --> 00:07:05,110
the collision rectangles intersect So we
detected a collision, and I want to apply

119
00:07:05,110 --> 00:07:09,090
damage to the bear, and I want to explode
the bear as appropriate.

120
00:07:09,090 --> 00:07:10,200
So I added a

121
00:07:12,670 --> 00:07:17,470
health food, I added a health food.
I added a health field to the bear.

122
00:07:17,470 --> 00:07:24,350
Bear starts at 100 health, and I also
added a take damage method to the bear.

123
00:07:24,350 --> 00:07:28,100
Which reduces the bear's health by the
damage amount, and if the health

124
00:07:28,100 --> 00:07:31,305
is less than or equal to zero, I clamp the
health to 0.

125
00:07:31,305 --> 00:07:33,750
because we don't want negative health,
even for

126
00:07:33,750 --> 00:07:37,120
dead teddy bears, and I deactivated the
bear.

127
00:07:37,120 --> 00:07:38,590
So he'll get

128
00:07:38,590 --> 00:07:42,710
cleaned out of that teddy bears list in
the game one update method.

129
00:07:42,710 --> 00:07:43,860
Let me go back here.

130
00:07:43,860 --> 00:07:50,250
And so what I want though is, I want the
teddy bear to take multiple hits to die.

131
00:07:50,250 --> 00:07:53,980
So I make em take damage and I check to
see if he's inactive.

132
00:07:53,980 --> 00:07:58,260
And if he's inactive, then I'm going to
start playing an explosion.

133
00:07:58,260 --> 00:07:59,730
This time it's going to be great.

134
00:07:59,730 --> 00:08:02,950
both the teddy bears and the projectiles
will blow up.

135
00:08:02,950 --> 00:08:03,900
Rubber chickens

136
00:08:03,900 --> 00:08:07,730
will blow up when we have that collision.
Okay.

137
00:08:07,730 --> 00:08:14,510
So, let me comment this out and show you
this cool bug

138
00:08:14,510 --> 00:08:18,040
that I ran into when I tried testing with
the above in place.

139
00:08:18,040 --> 00:08:22,370
So, I put this stuff in place, but I
hadn't implemented this yet,

140
00:08:22,370 --> 00:08:25,270
because, you know, you write a little code
and then you test it.

141
00:08:25,270 --> 00:08:28,470
And so, it took me a few minutes to figure
out what was going on here,

142
00:08:28,470 --> 00:08:34,010
because even though the chickens only
inflict 50 damage, and the

143
00:08:34,010 --> 00:08:38,830
teddy bears have 100 health, I was killing
these with one shot.

144
00:08:38,830 --> 00:08:41,210
And that is absolutely incorrect.

145
00:08:42,750 --> 00:08:44,780
Here's the issue.

146
00:08:44,780 --> 00:08:53,350
Because I hadn't implemented this code
yet, I wasn't making the chicken inactive.

147
00:08:53,350 --> 00:08:57,850
So on one update, the chicken hits the
teddy bear, the

148
00:08:57,850 --> 00:09:01,290
teddy bear takes damage, so now he's down
to 50 help.

149
00:09:01,290 --> 00:09:04,420
He's still active though, and I loop
around to

150
00:09:04,420 --> 00:09:07,020
the next update, a 60th of a second later.

151
00:09:08,020 --> 00:09:12,490
And because the chicken is still active,
this evaluates to true.

152
00:09:13,610 --> 00:09:16,260
So, I hit the bear, same chicken.

153
00:09:16,260 --> 00:09:19,400
Same chicken damages the bear again
because,

154
00:09:19,400 --> 00:09:20,660
oh, this is horrible.

155
00:09:20,660 --> 00:09:26,260
The rubber chicken is passing through the
teddy bear, ugh, every update.

156
00:09:26,260 --> 00:09:30,420
And so that's why, I was getting one shot

157
00:09:30,420 --> 00:09:33,480
kills of bears even though I had attempted
to implement

158
00:09:35,730 --> 00:09:36,380
two hits per death.

159
00:09:36,380 --> 00:09:36,497
[LAUGH]

160
00:09:36,497 --> 00:09:37,690
Two hits per death.
How's that for a slogan?

161
00:09:37,690 --> 00:09:40,210
So once I put this back in.

162
00:09:42,700 --> 00:09:47,050
So now I've made it so the chicken will
become inactive on this collision.

163
00:09:48,330 --> 00:09:51,500
Chickens always die on a collision,
there's no two hit chicken death.

164
00:09:51,500 --> 00:09:52,850
So we explode

165
00:09:54,940 --> 00:09:55,230
it.

166
00:09:55,230 --> 00:09:59,630
And we're going to add a new chicken down
at the bottom of the column.

167
00:09:59,630 --> 00:10:03,550
So, I thought about moving this particular
chicken down to

168
00:10:03,550 --> 00:10:06,420
the bottom again, but it made less sense
to me.

169
00:10:06,420 --> 00:10:10,249
When a chicken leaves the window, it makes
sense to me to drag it back.

170
00:10:11,490 --> 00:10:14,200
Once you explode a chicken, it really
doesn't make

171
00:10:14,200 --> 00:10:17,440
sense to me to move that exploded chicken
around.

172
00:10:17,440 --> 00:10:19,990
You certainly could do it, exactly the way
we did

173
00:10:19,990 --> 00:10:24,030
it when a chicken left the window, but I
added a new one in this case.

174
00:10:24,030 --> 00:10:27,550
And I made sure here that the new one that

175
00:10:27,550 --> 00:10:31,660
I was adding was in the same column, same
x location.

176
00:10:31,660 --> 00:10:34,810
Here's another one of those calls to get
chicken start y.

177
00:10:34,810 --> 00:10:36,050
This is where I added it.

178
00:10:36,050 --> 00:10:37,790
When I got here I said, holy smokes.

179
00:10:37,790 --> 00:10:41,450
I need to write a method for that, and
chicken damage like that.

180
00:10:41,450 --> 00:10:45,527
So now we will see that teddy

181
00:10:45,527 --> 00:10:52,020
bears take two hits to die.
One, two.

182
00:10:52,020 --> 00:10:53,390
Oh my gosh.

183
00:10:53,390 --> 00:10:55,720
We still aren't quite done, as you can
see, because the

184
00:10:55,720 --> 00:11:00,230
chicken, we get explosions, but then the
chicken keeps flying up.

185
00:11:00,230 --> 00:11:03,020
We'll figure out what's going on there
momentarily.

186
00:11:03,020 --> 00:11:05,220
But if we go back to the Powerpoint, we
can

187
00:11:05,220 --> 00:11:09,110
see that this collision stuff had a lot of
special processing.

188
00:11:09,110 --> 00:11:11,130
We had nested loops,

189
00:11:11,130 --> 00:11:13,920
which we hadn't seen before, though we'd
talked about.

190
00:11:13,920 --> 00:11:16,830
We had multiple hits to kill a teddy bear,

191
00:11:16,830 --> 00:11:21,430
which was buggy at first until we
completely implemented that.

192
00:11:21,430 --> 00:11:23,700
We have both teddies and chickens
exploding,

193
00:11:23,700 --> 00:11:27,190
so you got the gratuitous extra
explosions.

194
00:11:27,190 --> 00:11:28,470
There was that cool bug.

195
00:11:28,470 --> 00:11:32,150
And that may seem weird talking about a
cool bug, but they're mysteries right?

196
00:11:32,150 --> 00:11:37,260
You go solve these mysteries and that's
part of the fun of programming.

197
00:11:37,260 --> 00:11:40,370
And then there was some special chicken
processing,

198
00:11:40,370 --> 00:11:45,040
the part where I made the chicken
inactive, but

199
00:11:45,040 --> 00:11:47,050
also added a new chicken to make sure I

200
00:11:47,050 --> 00:11:49,030
always had a chicken in one of those
columns.

201
00:11:50,620 --> 00:11:52,530
Just a couple more things.

202
00:11:52,530 --> 00:11:57,750
So the chicken kept moving forward, even
after we started the explosion

203
00:11:57,750 --> 00:12:01,970
at the middle of the chicken, because we
aren't cleaning out dead chickens.

204
00:12:03,230 --> 00:12:03,560
So

205
00:12:03,560 --> 00:12:05,630
I have come to this part in the code where
we do

206
00:12:05,630 --> 00:12:09,310
clean out dead chickens, this is just like
cleaning out dead teddy bears.

207
00:12:09,310 --> 00:12:11,290
And so now, we will see that when I shoot
a

208
00:12:11,290 --> 00:12:15,590
teddy bear with a chicken, the chicken
does not keep going up.

209
00:12:18,180 --> 00:12:20,245
I'm sorry, I just got a little distracted
there

210
00:12:20,245 --> 00:12:22,069
because I was shooting rubber chickens at
teddy bears.

211
00:12:23,080 --> 00:12:25,940
So, we've now cleaned out the dead rubber
chickens,

212
00:12:25,940 --> 00:12:28,660
and there's just one more thing for us to
do.

213
00:12:28,660 --> 00:12:31,030
For efficiency, you can't even see this.

214
00:12:31,030 --> 00:12:34,830
But for efficiency, we have those
explosions that we cleaned out

215
00:12:34,830 --> 00:12:38,200
before, when we had explosions, and we
should do that again.

216
00:12:38,200 --> 00:12:42,050
Once an explosion has played, it becomes
inactive.

217
00:12:42,050 --> 00:12:43,320
And so we should get it out

218
00:12:43,320 --> 00:12:44,250
of the list.

219
00:12:44,250 --> 00:12:47,800
I did make a change that I forgot to
mention moments ago.

220
00:12:47,800 --> 00:12:51,230
When I create an explosion, if I can find
it.

221
00:12:52,580 --> 00:12:56,190
When I create an explosion, I changed the
explosion class.

222
00:12:56,190 --> 00:12:58,380
Early on, when we were using explosions,
remember,

223
00:12:58,380 --> 00:13:01,050
we had an explosion, and we'd create it,

224
00:13:01,050 --> 00:13:05,160
and then hold it around, and then we'd
tell it to play at a particular location.

225
00:13:05,160 --> 00:13:08,370
This is kind of a different way of
thinking about explosions,

226
00:13:08,370 --> 00:13:10,890
where you just create the explosion, and
play it

227
00:13:10,890 --> 00:13:13,640
right away, and then when it's done, it's
done.

228
00:13:13,640 --> 00:13:16,230
And, sort of we know enough now for that

229
00:13:16,230 --> 00:13:19,770
to be an intuitive and doable thing for
us.

230
00:13:19,770 --> 00:13:21,971
So, once we've cleaned out the dead

231
00:13:21,971 --> 00:13:24,710
explosions, we are all done with this
game.

232
00:13:25,720 --> 00:13:28,020
So, what did we do today?

233
00:13:28,020 --> 00:13:29,960
We used the rubber chicken class and some

234
00:13:29,960 --> 00:13:32,460
of the other classes we had hanging
around.

235
00:13:32,460 --> 00:13:33,670
And we built a small

236
00:13:33,670 --> 00:13:39,840
game up step by step following the steps I
used to built this small game.

237
00:13:39,840 --> 00:13:43,850
And talking about the concepts we've
learned so far in the course as we did it.

238
00:13:45,140 --> 00:13:47,710
So that's the end of Module 11.

239
00:13:47,710 --> 00:13:51,340
We're all done with, well, we're all done

240
00:13:51,340 --> 00:13:55,630
learning the new ideas behind class design
and implementation.

241
00:13:55,630 --> 00:13:58,700
And so next time, we'll actually start
learning

242
00:13:58,700 --> 00:14:02,540
how to add music and sound effects to our
x and a games.