1
00:00:08,806 --> 00:00:09,968
In this module,

2
00:00:09,968 --> 00:00:14,703
we're going to start talking about
how we use XNA to develop games.

3
00:00:14,703 --> 00:00:19,530
The module learning objective through
this module are provided here.

4
00:00:19,530 --> 00:00:23,830
So first we're going to understand the
different methods that are contained in

5
00:00:23,830 --> 00:00:26,210
the game1 class and that's what
we're going to talk about today.

6
00:00:26,210 --> 00:00:30,480
And then we're going to over
the course of the next couple lectures

7
00:00:30,480 --> 00:00:34,300
we'll learn how to draw sprites,
load sprites,

8
00:00:34,300 --> 00:00:39,210
develop games using existing classes
designed for use in XNA and so on.

9
00:00:41,420 --> 00:00:46,020
So in the previous module, we learned
a lot of stuff about classes and objects.

10
00:00:46,020 --> 00:00:50,130
And I made the claim that
object oriented analysis and

11
00:00:50,130 --> 00:00:54,010
design and implementation is
very useful in the game domain.

12
00:00:54,010 --> 00:00:58,890
And we will see that here as
we start dealing with XNA.

13
00:00:58,890 --> 00:01:02,520
As we're talking about XNA,
you should understand though,

14
00:01:02,520 --> 00:01:06,860
that even though we'll talk about
some XNAs specifics, there's lots of

15
00:01:06,860 --> 00:01:11,500
stuff that we're going to talk about that
is about game development in general.

16
00:01:11,500 --> 00:01:16,810
It will be applicable to any game
development framework you use,

17
00:01:16,810 --> 00:01:18,500
not just XNA.

18
00:01:18,500 --> 00:01:22,206
With that said, you should start
off with an in lecture quiz.

19
00:01:28,383 --> 00:01:30,420
Okay, so ignoring XNA,

20
00:01:30,420 --> 00:01:36,820
there are a number of general things
that we do in any game that we build.

21
00:01:36,820 --> 00:01:39,460
The first thing we do is
we initialize the game.

22
00:01:39,460 --> 00:01:42,820
That could include setting score to zero.

23
00:01:42,820 --> 00:01:47,190
That could include sort
of initializing some

24
00:01:47,190 --> 00:01:50,020
information that we're going to
need throughout the game.

25
00:01:50,020 --> 00:01:54,570
But we do something to get the game
into its starting or initial state.

26
00:01:55,600 --> 00:01:57,120
We also load content.

27
00:01:58,160 --> 00:01:59,990
So when we talk about content,

28
00:01:59,990 --> 00:02:04,170
we mean things like the graphical assets
that you're going to use in a game.

29
00:02:04,170 --> 00:02:08,790
If you're using a 2D game, those graphical
assets are typically called sprites.

30
00:02:08,790 --> 00:02:12,570
If you're building a 3D game
you might have models and

31
00:02:12,570 --> 00:02:16,720
textures that you're importing,
so and audio of course.

32
00:02:16,720 --> 00:02:17,990
So if you have music and

33
00:02:17,990 --> 00:02:21,820
sound effects in your games,
those are also considered content.

34
00:02:21,820 --> 00:02:26,670
So usually, not always, but
usually we think of content as

35
00:02:26,670 --> 00:02:31,605
the thing that comes from artists and
designers, not programmers.

36
00:02:31,605 --> 00:02:35,810
Programmer-developed art
is always interesting, but

37
00:02:35,810 --> 00:02:39,320
not necessarily what you want
in your high quality game.

38
00:02:40,440 --> 00:02:46,010
So one other form of content is
things like level specifications.

39
00:02:46,010 --> 00:02:47,590
If you have a level building tool and

40
00:02:47,590 --> 00:02:51,900
you have level designers building levels,
you might load that content,

41
00:02:51,900 --> 00:02:55,740
you have to load that content,
before you actually run your game.

42
00:02:55,740 --> 00:03:00,880
Now, content loading generally,
we do some up front, early on and

43
00:03:00,880 --> 00:03:05,980
regularly we will find ourselves loading
more content as we go through a game.

44
00:03:05,980 --> 00:03:10,130
So for example, we might load the initial
level, and then once the player has

45
00:03:10,130 --> 00:03:13,390
completed that level, we're going
to need to load the next level.

46
00:03:13,390 --> 00:03:16,890
We don't usually load all the levels all
at once, because that takes too long,

47
00:03:16,890 --> 00:03:20,050
and players get bored as they wait for
that to happen.

48
00:03:20,050 --> 00:03:22,490
So we load content
throughout the game too.

49
00:03:22,490 --> 00:03:25,060
But, we're going to find that usually,

50
00:03:25,060 --> 00:03:28,070
there's some content we
need to load upfront.

51
00:03:28,070 --> 00:03:29,870
So we do this initialization stuff and

52
00:03:29,870 --> 00:03:33,070
then we load content that we're going to
need at least initially in our game.

53
00:03:33,070 --> 00:03:37,470
And then we enter what's
typically called the game loop.

54
00:03:37,470 --> 00:03:42,170
And this game loop basically runs
until the player quits the game.

55
00:03:42,170 --> 00:03:44,790
So the game loop is [COUGH] excuse me,

56
00:03:44,790 --> 00:03:49,330
the game loop is, update the game,
change the game world.

57
00:03:49,330 --> 00:03:53,420
So that means moving a rubber
chicken through the game world.

58
00:03:53,420 --> 00:03:57,800
If the rubber chicken happens to have
velocity, it means having non-player

59
00:03:57,800 --> 00:04:02,150
characters do planning for
how they're going to come get you.

60
00:04:02,150 --> 00:04:06,180
It means all kinds of stuff
where we update the game world.

61
00:04:07,300 --> 00:04:11,450
And then we draw it because updating
the game world without actually showing

62
00:04:11,450 --> 00:04:16,670
the player those updates to the game world
leads to a fairly boring game, right?

63
00:04:16,670 --> 00:04:18,060
It's all invisible.

64
00:04:18,060 --> 00:04:20,110
So we update the game
world then we draw it.

65
00:04:20,110 --> 00:04:21,963
You will sometimes hear
that called rendering.

66
00:04:21,963 --> 00:04:26,107
And then we update the game world and
then we draw it and we update and

67
00:04:26,107 --> 00:04:30,030
draw until the player quits the game,
essentially.

68
00:04:30,030 --> 00:04:34,190
Now sometimes we move between playing
the game and dealing with the menus and

69
00:04:34,190 --> 00:04:35,350
those kinds of things.

70
00:04:35,350 --> 00:04:38,350
But we're still updating and drawing,
updating and drawing, updating and

71
00:04:38,350 --> 00:04:39,990
drawing as we go along.

72
00:04:39,990 --> 00:04:42,700
So those are standard ideas that happen no

73
00:04:42,700 --> 00:04:45,279
matter what game development
framework you're using.

74
00:04:46,370 --> 00:04:52,440
However, let's fire up XNA and
look at how XNA supports those things.

75
00:04:54,660 --> 00:04:59,450
So I open up the environment, and I
select New Project over here on the left.

76
00:04:59,450 --> 00:05:02,790
And then I get a set of possibilities for
the new project.

77
00:05:02,790 --> 00:05:04,920
None of these are mono game games.

78
00:05:04,920 --> 00:05:08,140
But if you look under
the visual C# templates here,

79
00:05:08,140 --> 00:05:10,420
there is a Mono Game section.

80
00:05:10,420 --> 00:05:15,030
So I'll select that and
I'll select Mono Game Windows project.

81
00:05:16,370 --> 00:05:19,620
I will rename the project IntroToXna and

82
00:05:19,620 --> 00:05:25,810
then I'm going to browse to a reasonable
location to save it and click OK.

83
00:05:25,810 --> 00:05:29,230
And then we'll come back and look and
see what template gets generated for us.

84
00:05:30,260 --> 00:05:33,180
You may find when it
generates the new project for

85
00:05:33,180 --> 00:05:37,540
you that there's no code actually shown
over here in the blue pane on the left.

86
00:05:37,540 --> 00:05:40,094
So, we could just click on game1.cs.

87
00:05:40,094 --> 00:05:45,360
And it will show us the game1 class
that we actually want to look at.

88
00:05:46,400 --> 00:05:46,900
Okay.

89
00:05:48,250 --> 00:05:53,668
Before we actually look at these details,
I would like you to go do an in-lecture

90
00:05:53,668 --> 00:05:58,625
quiz just to make sure that you
internalized those ideas of the game loop.

91
00:06:02,549 --> 00:06:10,270
Okay, so let's talk about the first
few things that get supported in XNA.

92
00:06:10,270 --> 00:06:12,230
I am going to zoom in, of course.

93
00:06:13,390 --> 00:06:17,400
And you saw this template before,
though I didn't zoom in before

94
00:06:17,400 --> 00:06:22,009
because this is what XNA gives us when
we say we want to do a Windows game.

95
00:06:23,770 --> 00:06:26,920
Right here is the constructor.

96
00:06:26,920 --> 00:06:28,260
We know about constructors.

97
00:06:28,260 --> 00:06:31,190
They're used to instantiate objects.

98
00:06:31,190 --> 00:06:34,700
So if there's stuff that we want to do

99
00:06:36,440 --> 00:06:41,910
when this game object gets created,
then we do it here in the constructor.

100
00:06:42,980 --> 00:06:47,580
However, that's a trade off because
we can also do those things

101
00:06:47,580 --> 00:06:50,550
right here in the initialize method.

102
00:06:50,550 --> 00:06:55,550
So we talked about initialization, setting
the score to zero, those kinds of things.

103
00:06:55,550 --> 00:07:01,290
That's what you would put here in the
initialize method because what happens is,

104
00:07:01,290 --> 00:07:02,890
the sequence of events for

105
00:07:02,890 --> 00:07:08,990
XNA games is the constructor gets
called to create a game object.

106
00:07:08,990 --> 00:07:13,150
Then initialize gets called
to initialize the game world.

107
00:07:14,200 --> 00:07:19,440
Then LoadContent gets called to load
that content we were talking about,

108
00:07:19,440 --> 00:07:22,490
graphics, sound effects,
those kinds of things.

109
00:07:22,490 --> 00:07:24,405
And then XNA enters the game loop.

110
00:07:25,460 --> 00:07:29,819
So these first three things Constructor,

111
00:07:29,819 --> 00:07:36,830
Initialize method and
LoadContent support those

112
00:07:36,830 --> 00:07:43,250
pre game loop activities that we do
in pretty much every game we build.

113
00:07:43,250 --> 00:07:48,238
So this is a good opportunity for
you to go do another in lecture quiz to

114
00:07:48,238 --> 00:07:52,367
make sure you understand what
LoadContent is all about.

115
00:07:56,128 --> 00:08:01,166
The next thing we'll do is we
will go to the update method.

116
00:08:01,166 --> 00:08:03,820
We're going to skip the UnloadContent.

117
00:08:03,820 --> 00:08:06,950
For the games you do in this course,
you'll never have to use that.

118
00:08:06,950 --> 00:08:11,620
For larger game, of course you would
unload content so that you didn't have

119
00:08:11,620 --> 00:08:16,680
everything in memory as you went along for
the 23 hour play session or

120
00:08:16,680 --> 00:08:20,980
anything like that, because that will
slow people's computers or consoles down.

121
00:08:20,980 --> 00:08:25,570
So there are certainly periods in
which we unload content in games.

122
00:08:25,570 --> 00:08:28,680
We're not going to need to
ever do that in this course.

123
00:08:28,680 --> 00:08:33,510
So we move on to the update method and
the update method gets called

124
00:08:34,750 --> 00:08:39,600
every frame in the game no matter what.

125
00:08:39,600 --> 00:08:43,010
So even if the game really
starts to slow down, what

126
00:08:44,070 --> 00:08:48,840
will happen is the update method
always gets called and the draw method

127
00:08:48,840 --> 00:08:53,650
which we'll look at soon, the draw method
gets called as it's possible to call it.

128
00:08:53,650 --> 00:08:55,780
As long as it's not going too slow.

129
00:08:55,780 --> 00:09:01,280
Again, unless you do something really
nuts in this class, you're never going

130
00:09:01,280 --> 00:09:07,330
to end up with a slow running game where
you see frame drops and stuff like that.

131
00:09:07,330 --> 00:09:11,960
Okay, so here in the update method
there's some prebuilt stuff.

132
00:09:11,960 --> 00:09:14,900
Notice it, well it may not be clear yet,

133
00:09:14,900 --> 00:09:19,440
because we haven't even talked about how
we use the Xbox controller, but this

134
00:09:19,440 --> 00:09:23,440
basically lets you press the back button
and leave the game if you choose to do so.

135
00:09:23,440 --> 00:09:29,130
And then we're going to add all
our world updating logic here.

136
00:09:29,130 --> 00:09:32,350
And we don't need any
of that right now but

137
00:09:32,350 --> 00:09:34,220
this method will get called every frame so

138
00:09:34,220 --> 00:09:38,880
we know whenever we need to update
the game world, this is where we do it.

139
00:09:38,880 --> 00:09:43,836
So you should go do another in lecture
quiz that asks you an XNA specific

140
00:09:43,836 --> 00:09:45,772
question about updating.

141
00:09:50,357 --> 00:09:55,118
And then finally, because we're inside the
game loop now we update the game world and

142
00:09:55,118 --> 00:09:57,580
then we render or draw it.

143
00:09:57,580 --> 00:10:01,480
And so here's where we draw our game.

144
00:10:02,570 --> 00:10:08,590
So update gets called and then draw,
and this by default clears the screen

145
00:10:08,590 --> 00:10:13,100
to a CornflowerBlue, that's why we get
that CornflowerBlue when we run it here.

146
00:10:13,100 --> 00:10:14,867
Let me run it just to remind you.

147
00:10:17,746 --> 00:10:20,499
So we get a CornflowerBlue screen drawn,
and

148
00:10:20,499 --> 00:10:26,610
then we'll add some drawing code here when
it's time for us to actually draw stuff.

149
00:10:26,610 --> 00:10:33,530
So the draw method will draw all
the visible entities in our game.

150
00:10:34,598 --> 00:10:37,230
So there's one more in lecture quiz

151
00:10:37,230 --> 00:10:42,160
that will ask about your
understanding of pixels in general.

152
00:10:42,160 --> 00:10:43,310
We're going to focus,

153
00:10:43,310 --> 00:10:48,250
in fact I will say we are exclusively
going to do 2D games in this class.

154
00:10:49,390 --> 00:10:53,920
Stepping to 3D is absolutely possible
once you've built a good foundation of

155
00:10:53,920 --> 00:10:54,920
expertise.

156
00:10:54,920 --> 00:10:58,090
But is definitely not
beginning programmers stuff.

157
00:10:59,420 --> 00:11:01,330
So we'll do 2D games here.

158
00:11:01,330 --> 00:11:04,440
And 2D games we regularly think about,
therefore,

159
00:11:04,440 --> 00:11:09,231
2 dimensional sprites that we're going to
display and 2 dimensional sprites have

160
00:11:09,231 --> 00:11:15,190
four components as long as we're going
to support transparency or opacity.

161
00:11:15,190 --> 00:11:19,500
Those four components are a red component,
a green component, a blue component,

162
00:11:19,500 --> 00:11:24,160
that's the RGB stuff you've heard
of before, and an alpha component.

163
00:11:24,160 --> 00:11:27,630
And so you should now go do
the in lecture quiz to tell me

164
00:11:27,630 --> 00:11:28,970
if you understand what alpha means.

165
00:11:31,125 --> 00:11:36,290
Okay, I am not going to save
that particular project.

166
00:11:36,290 --> 00:11:38,870
Don't worry we'll build one next
time that we actually end up saving.

167
00:11:38,870 --> 00:11:41,290
In fact, I've already pre-built it.

168
00:11:41,290 --> 00:11:46,320
So to recap for today though the standard

169
00:11:46,320 --> 00:11:50,836
game stuff that almost all games
do include initialization,

170
00:11:50,836 --> 00:11:55,040
loading content, updating the game world,
and drawing the game world.

171
00:11:55,040 --> 00:11:59,260
And that update and draw is contained
in what's called the game loop, and

172
00:11:59,260 --> 00:12:01,710
it happens until the player ends the game.

173
00:12:01,710 --> 00:12:07,150
We also took a look at how XNA supports
those different activities within

174
00:12:07,150 --> 00:12:12,970
the template that is provided to us
when we In Visual C# 2010 Express,

175
00:12:12,970 --> 00:12:19,560
say we want a new Windows game and so next
time, we'll actually use XNA to build a,

176
00:12:19,560 --> 00:12:25,090
I hate to call it a game so I'll put it in
quotes, to build our first game in XNA.