Transcript from the "Game Loop" Lesson
>> This works right but there's a few problems I can hit the red stop sign to stop my game, And I can still move around, right? Also if I wanted to have multiple movements of like, head down and to the right, I couldn't do that right now, because each one of these is kind of independent.
[00:00:20] Also, there's a bunch of other things, like normally a game starts, it runs, something happens and it stops, right? And the way that we tend to do this mechanic is what we call a game loop, which is just simply, we, when we start the game, do whatever setup you need to do and then jump in to the loop.
[00:00:56] So what we can do there to create a game loop is we just put a forever loop. That's not really forever. It's not like a wild true that's gonna lock up the main thread. It's like these do run on the event loop, right, where it'll say like, run through this and then put the next tick or the next frame onto the event loop.
[00:01:16] It's s kind of like, do it request animation frame to the next one, or set timeout zero, what have you. And so we can run things over, and over, and over again. And so we'll have that kind of in place. And now, what we could do, for instance, if I just want to have them run forever, right, we could take these out.
[00:01:35] And we'll delete this one here. So now I hit the green block, and he's running right. Maybe I make it wait a second or something along those lines, but it's fine. We're gonna say wait one second in between cuz that's horrifying. And this will keep running as long as the game runs.
[00:01:56] As you can see, it's a little weird though because hitting one of those arrows throw something else into a loop, that event loop, which means that those will take precedence before we take again. So we're probably gonna need to get these into the loop as well. We'll do that momentarily.
[00:02:10] To stop the game, you hit the stop sign. Awesome, and we can have a bunch of stuff in place as well. But let's get the movement into the game loop as well. And so the common way that you will see this is instead of waiting for one of these buttons to be pressed, a lot of times what we'll do is keep looping through.
[00:02:35] Because we can tell what buttons are currently being held down with the sensing blocks. So we'll go ahead and we'll go to say, if I'm going to do this over here for a reason you'll see in a second. We could say, if a given key is pressed in this case, we'll say if the right arrow is pressed, Point in direction 90 and move ten steps and then I'm very lazy.
[00:03:08] So I'll duplicate that. I'll say if the left arrow is pressed, Then point in direction -90. Same logic we had before. And now in my loop, I'm gonna zoom out just slightly because it's getting a little crowded in here. I can say as we go through the loop.
[00:03:33] Check to see if either one of these buttons are pressed, do the thing. Let's actually get rid of that. We don't need to wait a second anymore because we go back to just change the costume when he moves. Okay, so when the game starts We're gonna set everything up the way it's supposed to be, put Arnold wherever we want him to be.
[00:03:56] And then once we've got everything set up the way we want it, we'll start the game loop, which is if the right arrow is pressed, we will point the direction 90 and move him ten steps. And if the left arrow is pressed, we'll point in the direction -90, move him ten steps.
[00:04:11] Either way, we change the costume, right? And there we go. He's running around and we've got both in place. Now there are a bunch of, like this isn't the only way to do movement. Y'all have play Pac-Man. Right, so Pac-Man, you hit a direction. Pacman just keeps going.
[00:04:38] Pac-Man you don't have to hold the joystick in that direction. You hit up on the joystick. Pacman's gonna go until Pacman hits the wall, right? You hit right. Pacman's gonna go until Pacman hits wall or a ghost or something along those lines. And that's where the what mechanic do you want?
[00:04:53] Affects what kind of code you might write. So for instance, if we wanted that kind of mechanic in this place, we could just simply say, hey, when they press the key point, I'm gonna give him direction, right? But then no matter what, move them. So in this case, I can take my hand off the keyboard, and he'll keep going in that given direction.
[00:05:13] No one of those is more right than the other, it's about what mechanic do you want. Do you want the mechanic where they have to press and hold the key? Or do you want the mechanic where they press it once to change the direction, but the character keeps going?
[00:05:25] Both are equally right depending on what kind of effect and what kind of game you're trying to create. In our case though, I'm going to go back to the way things were. And I'll put things in here, but both are okay. Both are correct depending on what we're trying to do.