Module 13 is all about doing text IO in XNA games. So here are the module learning objectives. And just as a reminder, this is the final module in the course. So we're going to look at Text IO. IO stands for input and output. And the text input that we're going to do, we'll cover today. So in this lecture we're going to use keyboard input to control our character in the game, and then next time we'll do text output to display important game information, like score, which is a hugely important piece of game information. That's for next time. This time, we're going to cover the keyboard input part of the equation, if you will. So, you should start off with an in lecture quiz about keyboard input. And now, we'll go to the code that I all ready have opened up. And we'll look at how we can use keyboard input to control a fish in the game. So the end result is this. I drive a fish around, I'm still eating bears, I guess that's a thing for me. In this particular game, I can eat bears with the tail of my fish and all kinds of stuff, so, not nearly as sophisticated as that previous game that we saw, but. I can still drive around, I'm using the arrow keys to drive around with the keyboard so that you know that I'm not, you know, using a controller or something. So the first thing we do, remember when we got mouse input and processed it. We called the mouse.getstate method. When we used the game pad for input. We used the game pad.get state method, so it shouldn't surprise you at all that here when we're going to use the keyboard, we call the keyboard.get state method and it returns to us a keyboard state object. So go do another in lecture quiz. And I'll open up the Help. Keyboard state, if we search on that and go look at the Keyboard State members, we see that there are a number of methods that we're probably going to find fairly useful. We can get the pressed keys. So we can get an array of all the keys that are currently pressed, we can check if a particular key is down, and we can check if a particular key is up. Those are the three methods that you're most likely to use a lot. As you're dealing with this keyboard state object that gets returned from the keyboard get state method. So back to our code. We can see that I'm getting the state here. And in fact, right here, to make it so I can just exit from this game by pressing the Esc key. I've used keyboard, which I just got up here, I'm calling the IsKeyDown method, which returns true or false. And I'm passing in a value from the enumeration keys. So keys has, enumerates, basically, every, every conceivable key on the keyboard, that's probably not precisely accurate given our international student body here, but, lots of keys in the keys enumeration. And so, you did something similar already in the project increment, right where you called keyboard.getstate.IsKeyDownkeys.enter to move from the opening screen on to the actual game play. Now, now you get to see how that actually works, if you hadn't figured that out already, so that gets us the keyboard state, how do we actually use it to move our fish around. If we come down here to the update method and go to Definition by right clicking, and go to Definition, we see that here's what we do. We check if the key is down and it's the right key, now I don't mean the correct key, I mean the right arrow key, then we change the x property here. We'll look at that in a moment. By the fish move amount. So driving with the keyboard is like moving this around with the game pad. It's not following the mouse. It's applying fish for us to, if you will, to move the fish around. And there's a little bit of extra processing here where the fish. Art asset is actually the fish [LAUGH] the fish facing both ways. That's a tongue twister. The fish facing both ways, and so I pick which way it should be pointing, to the right or to the left, based on, based on its direction. So, let's go take a look at the X property And the fish move amount. We'll do it in exactly the opposite order from what I just said. So the fish move amount just gives us the speed of the fish. Basically how many pixels it would move. So this is not physics-based movement in any way. This is five pixels per update if in fact the appropriate key is pressed. The other thing that we should look at is the x property. Because this is the x location in the center of the fish. And, here's the interesting part for us, right? We're changing the drawrectangle, and that's fine, because we're changing the drawrectangle x based on the idea that the x property is centered. But we also are making sure that we're keeping the fish clamped within the window, so we can't drive the fish outside the window, even though we're using the keyboard. So back to the update method, we can see that we have four different if statements, one for right, one for left. One for up, and one for down. And only left and right change the source rectangles because I only have two images. We don't make the face, the fish face up or down. So, you might be wondering, because I know I would be, what happens if you press, press both left and right at the same time? Well, it turns around. You should actually walk through the code and see why if Im holding both left and right down, it's turning around. And the other thing that you can do is you can of course move diagonally. Now, I don't know if you can actually tell from this. But if I'm moving left and right, I'm moving it five pixels per update and if I'm moving diagonally, I'm moving at five times the square root of two pixels per update because of the way this is set up that I can move by fish amount. Horizontally and move by fish amount vertically in a single update. There's actually a very famous bug in Doom. So, it's called the Doom strafe 40 bug. And it essentially happens for exactly this reason. Because, the, okay. Maybe not exactly. But, the same idea. That, moving diagonally, you can move faster than if you're moving horizontally or vertically. So, it, it's, not rocket science, but it's not worth spending the time it would take in this lecture to fix this particular bug, but you should know that there is, in fact, a bug here, a very famous bug that you could fix if you worked at it a little bit. just as a hint, if you decide to do that, figuring out what the move amount should be, based on the keys that are currently pressed. So, for example, if they're going to move diagonally, if you change the move amount to be 5 divided by square root of 2 And then move it that amount horizontally and that amount vertically, you'll have solved this bug. The, the speed of movement will always be the same no matter in which of the eight possible directions they're moving. Ok, the only other, so really that's it for keyboard input, but let's actually add the capability cause you know some gamers like to use these keys that have w's, and a's, and s's and d's on them. So we can add wasd support, a well. Fairly quickly. of course, W is not to the right, so that would be a really wacky way to do it. So let's add WASD support, right, left. Now I finally get to use W. And down. And so if I F5 again, you can tell that I'm actually driving with w, s a, and d. And of course, both if I want to go super fast on the diagonal. So, you should now go do an in lecture quiz. About getting the state and how that works. Then you should do another in lecture quiz about how we process the keys that we have extracted from the keyboard state or. More precisely, how do we process the keyboard state in useful ways? And, really, that's all there is to doing keyboard input. You can see significant parallels between using a mouse for input and a game pad for input and a keyboard for input. We get the state of the input device And then we examine the state of the input device and process that state in appropriate ways. That's it for keyboard input. Next time we will see how to use text output in our XNA games so we can display important textual information.