1
00:00:08,830 --> 00:00:14,790
Module 13 is all about doing text IO in
XNA games.

2
00:00:14,790 --> 00:00:16,919
So here are the module learning
objectives.

3
00:00:21,960 --> 00:00:26,290
And just as a reminder, this is the final
module in the course.

4
00:00:26,290 --> 00:00:28,940
So we're going to look at Text IO.

5
00:00:28,940 --> 00:00:31,570
IO stands for input and output.

6
00:00:31,570 --> 00:00:35,400
And the text input that we're going to do,
we'll cover today.

7
00:00:35,400 --> 00:00:40,640
So in this lecture we're going to use
keyboard input to control our character in

8
00:00:40,640 --> 00:00:43,250
the game, and then next time we'll

9
00:00:43,250 --> 00:00:47,640
do text output to display important game
information,

10
00:00:47,640 --> 00:00:51,580
like score, which is a hugely important
piece of game information.

11
00:00:51,580 --> 00:00:53,100
That's for next time.

12
00:00:53,100 --> 00:00:56,150
This time, we're going to cover the
keyboard

13
00:00:56,150 --> 00:00:59,840
input part of the equation, if you will.

14
00:00:59,840 --> 00:01:03,720
So, you should start off with an in
lecture quiz about keyboard input.

15
00:01:08,900 --> 00:01:14,040
And now, we'll go to the code that I all
ready have opened up.

16
00:01:14,040 --> 00:01:19,370
And we'll look at how we can use keyboard
input to control a fish in the game.

17
00:01:19,370 --> 00:01:22,860
So the end result is this.

18
00:01:22,860 --> 00:01:26,980
I drive a fish around, I'm still eating
bears, I guess that's a thing for me.

19
00:01:26,980 --> 00:01:29,990
In this particular game, I can eat bears
with the tail of my

20
00:01:29,990 --> 00:01:33,960
fish and all kinds of stuff, so, not
nearly as sophisticated as that

21
00:01:33,960 --> 00:01:36,800
previous game that we saw, but.

22
00:01:36,800 --> 00:01:39,700
I can still drive around, I'm using the
arrow keys to drive around with the

23
00:01:39,700 --> 00:01:43,440
keyboard so that you know that I'm not,
you know, using a controller or something.

24
00:01:44,500 --> 00:01:49,770
So the first thing we do, remember when we
got mouse input and processed it.

25
00:01:49,770 --> 00:01:56,123
We called the mouse.getstate method.
When we used the game pad for input.

26
00:01:56,123 --> 00:01:59,280
We used the game pad.get state method,

27
00:01:59,280 --> 00:02:04,820
so it shouldn't surprise you at all that
here when we're going to use the keyboard,

28
00:02:04,820 --> 00:02:11,050
we call the keyboard.get state method and
it returns to us a keyboard

29
00:02:11,050 --> 00:02:17,760
state object.
So go do another in lecture quiz.

30
00:02:17,760 --> 00:02:18,900
And I'll open up the Help.

31
00:02:23,880 --> 00:02:30,950
Keyboard state, if we search on that and
go look at the Keyboard State members,

32
00:02:33,010 --> 00:02:36,650
we see that there are a number of

33
00:02:36,650 --> 00:02:39,890
methods that we're probably going to find
fairly useful.

34
00:02:39,890 --> 00:02:42,430
We can get the pressed keys.

35
00:02:42,430 --> 00:02:47,640
So we can get an array of all the keys
that are currently pressed, we can check

36
00:02:47,640 --> 00:02:54,870
if a particular key is down, and we can
check if a particular key is up.

37
00:02:54,870 --> 00:02:59,060
Those are the three methods that you're
most likely to use a lot.

38
00:02:59,060 --> 00:03:03,890
As you're dealing with this keyboard state
object that gets returned from the

39
00:03:03,890 --> 00:03:10,030
keyboard get state method.
So back to our code.

40
00:03:10,030 --> 00:03:13,000
We can see that I'm getting the state
here.

41
00:03:13,000 --> 00:03:15,650
And in fact, right here, to make it so I

42
00:03:15,650 --> 00:03:19,820
can just exit from this game by pressing
the Esc key.

43
00:03:19,820 --> 00:03:23,840
I've used keyboard, which I just got up
here,

44
00:03:25,160 --> 00:03:29,780
I'm calling the IsKeyDown method, which
returns true or false.

45
00:03:31,100 --> 00:03:37,560
And I'm passing in a value from the
enumeration keys.

46
00:03:37,560 --> 00:03:44,310
So keys has, enumerates, basically, every,
every conceivable key on the keyboard,

47
00:03:44,310 --> 00:03:50,340
that's probably not precisely accurate
given our international student body

48
00:03:50,340 --> 00:03:55,100
here, but, lots of keys in the keys
enumeration.

49
00:03:55,100 --> 00:04:01,868
And so, you did something similar already
in the project increment, right where you

50
00:04:01,868 --> 00:04:06,380
called
keyboard.getstate.IsKeyDownkeys.enter to

51
00:04:06,380 --> 00:04:08,072
move from the opening

52
00:04:08,072 --> 00:04:10,831
screen on to the actual game play.

53
00:04:10,831 --> 00:04:12,963
Now, now you get to see how that actually
works,

54
00:04:12,963 --> 00:04:15,407
if you hadn't figured that out already, so
that gets us

55
00:04:15,407 --> 00:04:18,497
the keyboard state, how do we actually use
it to move our fish around.

56
00:04:18,497 --> 00:04:24,425
If we come down here to the update method
and go to Definition by right

57
00:04:24,425 --> 00:04:30,170
clicking, and go to Definition, we see
that here's what we do.

58
00:04:31,270 --> 00:04:35,930
We check if the key is down and it's the
right key, now I don't

59
00:04:35,930 --> 00:04:40,600
mean the correct key, I mean the right
arrow key, then we change the x

60
00:04:40,600 --> 00:04:43,690
property here.
We'll look at that in a moment.

61
00:04:43,690 --> 00:04:45,180
By the fish move amount.

62
00:04:45,180 --> 00:04:49,710
So driving with the keyboard is like
moving this around with the game pad.

63
00:04:49,710 --> 00:04:51,730
It's not following the mouse.

64
00:04:51,730 --> 00:04:56,150
It's applying fish for us to, if you will,
to move the fish around.

65
00:04:56,150 --> 00:05:03,380
And there's a little bit of extra
processing here where the fish.

66
00:05:03,380 --> 00:05:05,675
Art asset is actually the fish

67
00:05:05,675 --> 00:05:06,350
[LAUGH]

68
00:05:06,350 --> 00:05:10,460
the fish facing both ways.
That's a tongue twister.

69
00:05:10,460 --> 00:05:14,930
The fish facing both ways, and so I pick
which way it should be

70
00:05:14,930 --> 00:05:19,750
pointing, to the right or to the left,
based on, based on its direction.

71
00:05:19,750 --> 00:05:23,720
So, let's go take a look at the X property
And the fish move amount.

72
00:05:23,720 --> 00:05:27,180
We'll do it in exactly the opposite order
from what I just said.

73
00:05:27,180 --> 00:05:31,000
So the fish move amount just gives us the
speed of the fish.

74
00:05:31,000 --> 00:05:34,190
Basically how many pixels it would move.

75
00:05:34,190 --> 00:05:37,178
So this is not physics-based movement in
any way.

76
00:05:37,178 --> 00:05:43,130
This is five pixels per update if in fact
the appropriate key is pressed.

77
00:05:44,180 --> 00:05:47,880
The other thing that we should look at is
the x property.

78
00:05:48,940 --> 00:05:52,570
Because this is the x location in the
center of the fish.

79
00:05:52,570 --> 00:05:55,380
And, here's the interesting part for us,
right?

80
00:05:55,380 --> 00:05:56,050
We're changing the

81
00:05:56,050 --> 00:06:00,270
drawrectangle, and that's fine, because
we're changing the drawrectangle x

82
00:06:00,270 --> 00:06:03,900
based on the idea that the x property is
centered.

83
00:06:03,900 --> 00:06:06,230
But we also are making sure that

84
00:06:06,230 --> 00:06:08,810
we're keeping the fish clamped within the
window,

85
00:06:08,810 --> 00:06:10,580
so we can't drive the fish outside

86
00:06:10,580 --> 00:06:12,800
the window, even though we're using the
keyboard.

87
00:06:14,130 --> 00:06:16,790
So back to the update method,

88
00:06:18,890 --> 00:06:25,630
we can see that we have four different if
statements, one for right, one for left.

89
00:06:25,630 --> 00:06:27,500
One for up, and one for down.

90
00:06:27,500 --> 00:06:31,610
And only left and right change the source
rectangles because I only have two images.

91
00:06:31,610 --> 00:06:34,810
We don't make the face, the fish face up
or down.

92
00:06:36,050 --> 00:06:40,650
So, you might be wondering, because I know
I would be, what

93
00:06:40,650 --> 00:06:44,850
happens if you press, press both left and
right at the same time?

94
00:06:44,850 --> 00:06:46,670
Well, it turns around.

95
00:06:46,670 --> 00:06:49,580
You should actually walk through the code
and see why

96
00:06:49,580 --> 00:06:54,150
if Im holding both left and right down,
it's turning around.

97
00:06:54,150 --> 00:06:58,770
And the other thing that you can do is you
can of course move diagonally.

98
00:06:58,770 --> 00:07:02,790
Now, I don't know if you can actually tell
from this.

99
00:07:02,790 --> 00:07:05,900
But if I'm moving left and right, I'm
moving

100
00:07:05,900 --> 00:07:10,490
it five pixels per update and if I'm
moving diagonally,

101
00:07:10,490 --> 00:07:15,980
I'm moving at five times the square root
of two pixels per update because

102
00:07:15,980 --> 00:07:20,070
of the way this is set up that I can move
by fish amount.

103
00:07:20,070 --> 00:07:25,540
Horizontally and move by fish amount
vertically in a single update.

104
00:07:25,540 --> 00:07:28,960
There's actually a very famous bug in
Doom.

105
00:07:28,960 --> 00:07:35,830
So, it's called the Doom strafe 40 bug.
And it essentially happens for exactly

106
00:07:35,830 --> 00:07:38,010
this reason.
Because, the, okay.

107
00:07:38,010 --> 00:07:38,870
Maybe not exactly.

108
00:07:38,870 --> 00:07:40,670
But, the same idea.

109
00:07:40,670 --> 00:07:43,350
That, moving diagonally, you can move
faster

110
00:07:43,350 --> 00:07:46,280
than if you're moving horizontally or
vertically.

111
00:07:46,280 --> 00:07:51,580
So, it, it's, not rocket science, but it's
not worth spending

112
00:07:51,580 --> 00:07:53,840
the time it would take in this lecture to
fix this

113
00:07:53,840 --> 00:07:56,820
particular bug, but you should know that
there is, in fact,

114
00:07:56,820 --> 00:08:00,940
a bug here, a very famous bug that you
could fix if

115
00:08:00,940 --> 00:08:05,840
you worked at it a little bit.
just as a hint, if you decide to do

116
00:08:05,840 --> 00:08:11,090
that, figuring out what the move amount
should be, based

117
00:08:11,090 --> 00:08:16,640
on the keys that are currently pressed.

118
00:08:16,640 --> 00:08:17,930
So, for example, if they're going to move

119
00:08:17,930 --> 00:08:20,710
diagonally, if you change the move amount
to

120
00:08:20,710 --> 00:08:26,470
be 5 divided by square root of 2 And then
move it that amount horizontally and

121
00:08:26,470 --> 00:08:29,870
that amount vertically, you'll have solved
this bug.

122
00:08:29,870 --> 00:08:32,960
The, the speed of movement will always be
the same

123
00:08:32,960 --> 00:08:36,760
no matter in which of the eight possible
directions they're moving.

124
00:08:38,770 --> 00:08:42,350
Ok, the only other, so really that's it
for

125
00:08:42,350 --> 00:08:46,910
keyboard input, but let's actually add the
capability cause you

126
00:08:46,910 --> 00:08:50,020
know some gamers like to use these keys
that

127
00:08:50,020 --> 00:08:53,100
have w's, and a's, and s's and d's on
them.

128
00:08:53,100 --> 00:08:55,920
So we can add wasd support, a well.

129
00:08:57,030 --> 00:08:57,850
Fairly quickly.

130
00:09:00,100 --> 00:09:06,370
of course, W is not to the right, so that
would be a really wacky way to do it.

131
00:09:06,370 --> 00:09:12,489
So let's add WASD support,

132
00:09:12,489 --> 00:09:18,159
right, left.
Now I finally get to use W.

133
00:09:22,290 --> 00:09:23,880
And down.

134
00:09:23,880 --> 00:09:31,530
And so if I F5 again, you can tell that
I'm actually driving with w, s a, and d.

135
00:09:31,530 --> 00:09:34,720
And of course, both if I want to go super
fast on the diagonal.

136
00:09:35,740 --> 00:09:39,440
So, you should now go do an in lecture
quiz.

137
00:09:40,710 --> 00:09:44,600
About getting the state and how that
works.

138
00:09:49,490 --> 00:09:53,720
Then you should do another in lecture quiz
about how we

139
00:09:53,720 --> 00:09:59,330
process the keys that we have extracted
from the keyboard state or.

140
00:09:59,330 --> 00:10:04,670
More precisely, how do we process the
keyboard state in useful ways?

141
00:10:07,760 --> 00:10:11,140
And, really, that's all there is to doing
keyboard input.

142
00:10:11,140 --> 00:10:15,770
You can see significant parallels between
using a mouse for input

143
00:10:15,770 --> 00:10:18,870
and a game pad for input and a keyboard
for input.

144
00:10:18,870 --> 00:10:22,260
We get the state of the input device And
then we examine

145
00:10:22,260 --> 00:10:29,100
the state of the input device and process
that state in appropriate ways.

146
00:10:29,100 --> 00:10:33,600
That's it for keyboard input.
Next time we will see how to use

147
00:10:33,600 --> 00:10:40,270
text output in our XNA games so we can
display important textual information.