1
00:00:08,970 --> 00:00:12,800
We've reached the last lecture in module
11, so as

2
00:00:12,800 --> 00:00:16,689
a final reminder, here are the module 11
learning objectives.

3
00:00:19,890 --> 00:00:23,300
So, last time we finished implementing our
rubber chicken class.

4
00:00:23,300 --> 00:00:26,930
And it's time to actually put that class
into a game.

5
00:00:26,930 --> 00:00:29,590
And so, we're now going to build a
complete game that

6
00:00:29,590 --> 00:00:33,190
includes the rubber chicken as one of the
classes in the game.

7
00:00:33,190 --> 00:00:37,470
I've already written all the code, but
I've also commented all out

8
00:00:37,470 --> 00:00:43,010
again, and captured the sequence that I
followed to develop the code.

9
00:00:43,010 --> 00:00:45,130
So I'm going to bring you through the
process from start

10
00:00:45,130 --> 00:00:48,170
to finish, and I won't actually be typing
code,

11
00:00:48,170 --> 00:00:51,540
I'll just be uncommenting pieces I already
typed in.

12
00:00:51,540 --> 00:00:55,920
And talking about sort of interesting
things as we get to those pieces.

13
00:00:55,920 --> 00:00:59,050
So let's go right to the IDE, I've already
got

14
00:00:59,050 --> 00:01:01,810
it opened up, and zoomed, thank goodness
for both of those.

15
00:01:05,350 --> 00:01:10,250
So the initial stuff, and I'm going to try
not to get dizzy, I'm going to pop back

16
00:01:10,250 --> 00:01:16,610
and forth between the Power Point, and the
code so that we can look at each step.

17
00:01:16,610 --> 00:01:19,510
But before we start doing that, I want to
show you

18
00:01:19,510 --> 00:01:23,100
the core things that I've already included
in the game.

19
00:01:23,100 --> 00:01:27,080
So we already have the standard constants
for window width and window height, I

20
00:01:27,080 --> 00:01:30,750
have some information about chickens, how
many chickens should be in the game, and

21
00:01:30,750 --> 00:01:32,900
how much damage each chicken inflicts.

22
00:01:33,920 --> 00:01:36,120
I'm saving the three sprites that we're
using

23
00:01:36,120 --> 00:01:38,780
in the game for efficiency in this texture

24
00:01:38,780 --> 00:01:40,880
2Ds, because we're going to be creating
bears

25
00:01:40,880 --> 00:01:43,260
and chickens and explosions all over the
place.

26
00:01:43,260 --> 00:01:45,990
So just loading this content once and then
saving it

27
00:01:45,990 --> 00:01:47,750
and using it as we need it is a good idea.

28
00:01:48,850 --> 00:01:50,270
We have some game entities.

29
00:01:50,270 --> 00:01:54,600
We have a list of rubber chickens, a list
of bears, and a list of explosions.

30
00:01:54,600 --> 00:01:56,650
And you will notice that

31
00:01:56,650 --> 00:02:01,750
in this constructor, for the list of
rubber chickens, I've actually provided an

32
00:02:01,750 --> 00:02:07,150
argument, whereas, all the lists we've
created so far I haven't done that.

33
00:02:07,150 --> 00:02:12,004
This just tells the system how large that
list should be.

34
00:02:12,004 --> 00:02:16,316
So it's the capacity of the list, and I
actually know I will always

35
00:02:16,316 --> 00:02:21,937
have exactly num chickens, chickens so if
I provide the capacity first, then I don't

36
00:02:21,937 --> 00:02:24,350
have to grow the list dynamically.

37
00:02:25,650 --> 00:02:28,220
We have teddy bear spawn support, I have
speed

38
00:02:28,220 --> 00:02:31,020
for the teddy bears, they'll spawn every
two seconds.

39
00:02:31,020 --> 00:02:34,010
We've seen this timer stuff before, and I
will need a random number

40
00:02:34,010 --> 00:02:38,970
generator to generate the random x
location for teddy bears that I spawn.

41
00:02:40,620 --> 00:02:42,880
I've set the resolution and made the mouse
visible

42
00:02:42,880 --> 00:02:45,160
because we click on rubber chickens to
launch them.

43
00:02:45,160 --> 00:02:47,170
And we want to make sure the player

44
00:02:47,170 --> 00:02:52,920
can see where the mouse is to do that.
In the load content I haven't done

45
00:02:52,920 --> 00:02:58,080
too much exciting here, I've loaded these
three sprites, so you've seen that before.

46
00:02:58,080 --> 00:03:03,120
But I'm also calling this spawn initial
rubber chickens method.

47
00:03:03,120 --> 00:03:07,620
That doesn't come free in the game one
class, I wrote this, so let's

48
00:03:07,620 --> 00:03:12,550
go to the definition, I right-clicked and
about halfway down, so this is just

49
00:03:12,550 --> 00:03:18,310
writing a method like the methods we've
already written to roll the die.

50
00:03:18,310 --> 00:03:20,660
To tell the rubber chicken to draw and
update

51
00:03:20,660 --> 00:03:25,500
themselves, this is just another method
that I've written.

52
00:03:25,500 --> 00:03:26,890
Now, it turns out that this one

53
00:03:26,890 --> 00:03:30,180
is slightly different, because I've made
it private.

54
00:03:30,180 --> 00:03:33,720
Nobody outside this class should ever be
able to spawn

55
00:03:33,720 --> 00:03:38,520
the initial rubber chickens and so I made
that private.

56
00:03:38,520 --> 00:03:41,868
And it can only be used from within the
game one

57
00:03:41,868 --> 00:03:47,040
class, and specifically, it's only used
from the load content method.

58
00:03:47,040 --> 00:03:50,320
So I'm actually going to do some math
here,

59
00:03:50,320 --> 00:03:53,590
where I figure out what the spacing
between the

60
00:03:53,590 --> 00:03:56,160
rubber chickens should be based on the
window width

61
00:03:56,160 --> 00:03:59,050
and how many rubber chickens I'm going to
have.

62
00:03:59,050 --> 00:04:03,290
And I then set the current x,

63
00:04:03,290 --> 00:04:07,960
so this is the x for the rubber chicken I
am about to create to x spacing over 2.

64
00:04:07,960 --> 00:04:10,510
So this is going to be way over near the
left side of

65
00:04:10,510 --> 00:04:13,400
the window though not all the way up
against left side of the window.

66
00:04:14,590 --> 00:04:16,560
I then spawn the chickens.

67
00:04:16,560 --> 00:04:20,180
I'll talk about this get chicken start y
method right now.

68
00:04:20,180 --> 00:04:21,900
Again, another private method.

69
00:04:23,070 --> 00:04:25,210
And really, it just returns window height,

70
00:04:25,210 --> 00:04:28,100
so the very bottom, minus chicken.height
over two.

71
00:04:28,100 --> 00:04:29,690
So this is going to put the chicken

72
00:04:29,690 --> 00:04:33,230
standing on the bottom of the window,
basically.

73
00:04:33,230 --> 00:04:37,520
I wrote this method because I discovered
that numerous

74
00:04:37,520 --> 00:04:41,000
times I needed to do this exact same
calculation.

75
00:04:41,000 --> 00:04:44,280
So instead of doing the calculation at
three different places

76
00:04:44,280 --> 00:04:47,650
in my code, I did it in this one place
instead.

77
00:04:47,650 --> 00:04:51,600
So, I get the chicken start Y, and now I
do a FOR loop.

78
00:04:51,600 --> 00:04:53,170
This is much like the FOR loop when

79
00:04:53,170 --> 00:04:58,060
we had the initial num teddies a long time
ago when we were learning about FOR loops.

80
00:04:58,060 --> 00:05:00,610
And so I go through the FOR loop, I add a
new

81
00:05:00,610 --> 00:05:07,340
rubber chicken, with a sprite, a location
and how much damage it inflicts.

82
00:05:07,340 --> 00:05:12,980
And then, I change the current x by adding
x spacing to it, so that the next

83
00:05:12,980 --> 00:05:18,310
time around the loop, when I'm on chicken
number one, our second chicken, it will

84
00:05:18,310 --> 00:05:21,530
be at x spacing plus x spacing over 2.

85
00:05:21,530 --> 00:05:25,583
So I'm, you know, putting them in a row
across the bottom of the screen.

86
00:05:25,583 --> 00:05:31,694
And I do that with that for loop and now
if we go back to the Power Point slide,

87
00:05:31,694 --> 00:05:37,518
we see that I have done this step, I've
spawned the initial chickens.

88
00:05:37,518 --> 00:05:40,554
You can't see and by the way, this is
exactly the order

89
00:05:40,554 --> 00:05:43,402
in which I did the steps as I was creating
this game.

90
00:05:43,402 --> 00:05:46,314
I got to you know, step three and said
hey, I should keep track

91
00:05:46,314 --> 00:05:49,520
of this so I can go in the same order when
I do the lecture.

92
00:05:49,520 --> 00:05:52,810
So we spawn those chickens but you can't
see 'em.

93
00:05:52,810 --> 00:05:55,850
So, we should draw all the game entities.

94
00:05:55,850 --> 00:06:00,800
So, let's go up to the draw.
And I'll just uncomment this code.

95
00:06:02,460 --> 00:06:05,530
And you might think reasonably, that I
should just be

96
00:06:05,530 --> 00:06:08,420
drawing the rubber chickens at this point,
why am I drawing

97
00:06:08,420 --> 00:06:12,920
teddy bears and explosions, because those
lists are empty right now and I know it.

98
00:06:12,920 --> 00:06:15,428
To be honest, when I came here I said, I
might

99
00:06:15,428 --> 00:06:18,992
as well draw everything right now while
I'm thinking about it,

100
00:06:18,992 --> 00:06:22,424
rather than waiting until later and
realizing I forgot to draw

101
00:06:22,424 --> 00:06:26,966
the explosion and then coming down and,
you know, spending debugging time.

102
00:06:26,966 --> 00:06:30,405
So I did it when I thought of it.

103
00:06:30,405 --> 00:06:33,680
And now, now that I've uncommented this I
can run it.

104
00:06:35,090 --> 00:06:37,530
And we see a row of rubber, beautiful,

105
00:06:37,530 --> 00:06:39,850
beautiful rubber chickens along the bottom
of the screen.

106
00:06:41,440 --> 00:06:42,090
Okay.

107
00:06:42,090 --> 00:06:47,010
So now, I have drawn all the game
entities.

108
00:06:49,830 --> 00:06:53,910
So the next thing says, comments for all
the updates that need to happen.

109
00:06:53,910 --> 00:06:57,960
So at this point, I started thinking about
how the game would really work and

110
00:06:57,960 --> 00:07:03,260
I went up to the update method and I put
up a set of comments.

111
00:07:03,260 --> 00:07:05,680
I said, you know, make sure you update all
the game

112
00:07:05,680 --> 00:07:08,080
entities, check for collisions between
chickens

113
00:07:08,080 --> 00:07:10,920
and bears, clear out dead teddies.

114
00:07:10,920 --> 00:07:15,270
The sequence of steps that I knew would
need to happen on every

115
00:07:15,270 --> 00:07:17,750
update once the full game was running.

116
00:07:17,750 --> 00:07:19,820
We'll just look at those comments as we go
along because

117
00:07:19,820 --> 00:07:23,760
it's hard to see them with all the code
interspersed between them.

118
00:07:23,760 --> 00:07:30,340
But this was stopping and thinking and not
writing any code, just sort of, capturing

119
00:07:30,340 --> 00:07:34,590
my ideas about the sequence of events that
had to happen in that update method.

120
00:07:35,690 --> 00:07:40,280
So I did that and now we should update all
the game entities.

121
00:07:40,280 --> 00:07:40,420
So

122
00:07:40,420 --> 00:07:44,670
I'll scroll up to the update method, as
you can see, there's lots of code in here,

123
00:07:46,070 --> 00:07:52,569
but here's the upfate the game entities,
and so I'll uncomment all of this.

124
00:07:53,900 --> 00:07:58,520
And really I wanted to update the chicken
because, you know, once I drew

125
00:07:58,520 --> 00:08:02,190
chickens I wanted to click on them and I
clicked on them and nothing happened.

126
00:08:02,190 --> 00:08:05,500
And so I was upset, and so

127
00:08:05,500 --> 00:08:08,000
I went and implemented this.

128
00:08:08,000 --> 00:08:11,530
And because I was updating the chickens, I
knew everything would need to be updated.

129
00:08:11,530 --> 00:08:17,720
So I just added updating all of them now I
made a comment here, by the way,

130
00:08:17,720 --> 00:08:22,850
that I changed the rubber chicken speed
for finer granularity.

131
00:08:22,850 --> 00:08:24,270
So let's go to the rubber chicken.

132
00:08:25,720 --> 00:08:31,910
So in this field that is rubber chicken
speed, I made it a float, and

133
00:08:31,910 --> 00:08:33,140
I made it 0.8 f.

134
00:08:34,280 --> 00:08:36,490
the f is required because it's a float;

135
00:08:36,490 --> 00:08:39,450
otherwise, the compiler will think 0.8 is
a double.

136
00:08:40,520 --> 00:08:44,200
So I've made that a float, and I realized,
when

137
00:08:44,200 --> 00:08:47,330
I was doing that, that I regularly make
the speed a

138
00:08:47,330 --> 00:08:51,870
float because we usually want to have
finer control over

139
00:08:51,870 --> 00:08:57,280
the speed of our game entities than
integers provides to us.

140
00:08:57,280 --> 00:09:03,170
So now if I F5 it, I can click on a rubber
chicken and he flies up, and I can, you

141
00:09:03,170 --> 00:09:05,620
know, click on all the ru-, well not all,
I'll get

142
00:09:05,620 --> 00:09:08,510
bored, but I can click on rubber chickens
to launch them.

143
00:09:09,600 --> 00:09:13,390
And so, now, we've noticed that we are,
well, we've

144
00:09:13,390 --> 00:09:16,880
noticed, because I just said we're
updating all the game entities.

145
00:09:19,510 --> 00:09:22,880
And the next thing is, when a chicken
flies out

146
00:09:22,880 --> 00:09:26,260
of the top of the window, we should do
something.

147
00:09:26,260 --> 00:09:31,770
We should either remove that chicken from
the game, make it inactive, so

148
00:09:31,770 --> 00:09:37,030
that you know, we clean out the dead
rubber chickens later, and it's reclaimed.

149
00:09:37,030 --> 00:09:39,041
And, and that's a perfectly reasonable
thing to do.

150
00:09:39,041 --> 00:09:41,777
And in fact in the optional project, you
will

151
00:09:41,777 --> 00:09:44,657
be, if you choose to do it, you'll be
removing

152
00:09:44,657 --> 00:09:50,010
inactive projectiles, from the game.
Now for chickens, the way

153
00:09:50,010 --> 00:09:55,290
I wanted this to work is, I pretty much
always wanted the bottom

154
00:09:55,290 --> 00:10:01,090
row of chickens to be full.
Let's see, that's not exactly correct.

155
00:10:01,090 --> 00:10:04,570
For each, think of it as 20 columns.

156
00:10:04,570 --> 00:10:07,270
Here, why don't I run it and then we can
look at it.

157
00:10:07,270 --> 00:10:10,100
So, I wanted

158
00:10:10,100 --> 00:10:13,910
there always to be a chicken in this
zeroth column.

159
00:10:13,910 --> 00:10:17,480
So either he's sitting down here, or he's
flying his way up.

160
00:10:17,480 --> 00:10:22,530
But once he's gotten to the top and
outside the window, I wanted a new chicken

161
00:10:22,530 --> 00:10:27,570
to be put here, so that you always had
each column have a chicken in it.

162
00:10:27,570 --> 00:10:35,070
So, the problem, or one of the
alternatives was to destroy the chicken,

163
00:10:35,070 --> 00:10:36,940
and spawn a new one.

164
00:10:36,940 --> 00:10:41,620
However, what I decided to do instead was
this.

165
00:10:41,620 --> 00:10:44,476
Let's go to the check for chicken leaving
the window.

166
00:10:44,476 --> 00:10:49,675
Right here.
Instead of

167
00:10:49,675 --> 00:10:56,180
destroying it, I decided to move that
chicken

168
00:10:56,180 --> 00:11:00,240
back down to the bottom of the column.
And so you'll

169
00:11:00,240 --> 00:11:03,400
notice I added a reset method to Rubber
Chicken so

170
00:11:03,400 --> 00:11:07,880
we can go to the definition of that reset
method here.

171
00:11:07,880 --> 00:11:10,160
And so it puts the chicken back at the
bottom of the

172
00:11:10,160 --> 00:11:15,960
window It sets its velocity back to zero,
and it makes moving false.

173
00:11:15,960 --> 00:11:17,365
And just so you know,

174
00:11:17,365 --> 00:11:17,700
[LAUGH]

175
00:11:17,700 --> 00:11:21,710
I originally had this as a set y rather
than a reset.

176
00:11:21,710 --> 00:11:24,910
So, I just moved the chicken back to the
bottom of the window.

177
00:11:24,910 --> 00:11:27,094
And because the velocity was still
positive,

178
00:11:27,094 --> 00:11:28,998
and because it was still moving, it just

179
00:11:28,998 --> 00:11:32,358
kept scrolling up, so it would, you know,
pop down to the bottom and go

180
00:11:32,358 --> 00:11:35,326
up, and pop down to the bottom and go up,
and while that was very

181
00:11:35,326 --> 00:11:39,216
hypnotic, I didn't like it cause it wasn't
how the game was supposed to work.

182
00:11:39,216 --> 00:11:43,480
So I had to change this to reset.
So notice.

183
00:11:43,480 --> 00:11:46,960
Right, that it's okay, we develop, we
design and implement our

184
00:11:46,960 --> 00:11:50,650
classes, and then when we start using
them, we may well

185
00:11:50,650 --> 00:11:54,290
discover that there were things that we
didn't know we needed

186
00:11:54,290 --> 00:11:57,850
and we discover we need em as we start
using the code.

187
00:11:57,850 --> 00:12:01,850
It's a perfectly natural part of
developing games.

188
00:12:01,850 --> 00:12:05,490
And so, I felt perfectly free to add

189
00:12:05,490 --> 00:12:09,190
methods as I needed them throughout the
code.

190
00:12:09,190 --> 00:12:12,770
Okay, so why did I move the chicken down
to the

191
00:12:12,770 --> 00:12:17,190
bottom again instead of just destroying it
and creating a new one?

192
00:12:17,190 --> 00:12:19,160
Because this is better for memory.

193
00:12:19,160 --> 00:12:22,970
There's no need to throw away the chicken
to the garbage collector has to go

194
00:12:22,970 --> 00:12:28,750
pick it up, just be...just so I can create
a new chicken in the same column.

195
00:12:28,750 --> 00:12:31,570
I can just move this chicken right back
down,

196
00:12:31,570 --> 00:12:34,630
and so that saves us a little bit on
memory.

197
00:12:34,630 --> 00:12:37,230
So, we've done the replacing chickens
part.

198
00:12:40,110 --> 00:12:44,725
And at that point, I could shoot chickens,
I could launch chickens up toward the top

199
00:12:44,725 --> 00:12:49,291
of the window, and then when they exited
the window, they came back to the bottom.

200
00:12:49,291 --> 00:12:52,310
So, let me run that to show you.

201
00:12:52,310 --> 00:12:54,930
So, I can send a chicken, and when he's
out, I

202
00:12:54,930 --> 00:12:58,239
get a new chicken, really the same
chicken, back in that column.

203
00:12:59,310 --> 00:13:02,350
This has turned out to be another long
lecture,

204
00:13:02,350 --> 00:13:05,040
so, I'm breaking this one into two parts
as well.

205
00:13:05,040 --> 00:13:07,000
This is the end of part one.