 Okay, hi everybody. This is a Hopefully a short relatively short tutorial on how to make a character that can jump up and down I want to talk about that. It's a little bit physics based and there's a couple really kind of key gotchas You got to be careful of so let me show you what I've got so far I'm using the turtle module. This is Python 3 and what I've done is I've created a line and I've called this line ground level and I've got my character here It's just a little green square and this green square is going to jump when I press the space bar But of course it's doing nothing right now So I'm gonna go through the basics of the code and I'm assuming that if you're watching this tutorial You've seen some of my other tutorials and you kind of know the basics of what I'm doing here So I'm using the turtle module and then I've set up my screen. I've given it a title I've set a background color in this case. I set the dimensions of the screen So 320 pixels high by 800 pixels wide And this wind tracer this actually shuts off screen updates and Then down here the counterpart is when update and it updates it when I choose So I've created a variable called ground level notice that it is capitalized And what this means usually in coding is that this is a constant It means during the course of the game this number will not change. So for this particular program That's the way it's gonna be so it's at the minus 40 Y-corded Then I've created a turtle and I've called it pen I'm gonna use that to draw the ground and so I started at negative 400 because remember it's 800 pixels wide So I started at negative 400 and then ground level Put the pen down drawn all the way over to positive 400 ground level and I put the pen up done and Then I have here a jumper. That's what I've called my character jumper is also a turtle Speed zero does not mean the speed that it moves. This is the animation speed It's just something you need to have for the turtle module as I said before it's a green square and I use pen up because I don't want it to draw a trail and this is some of the stuff that I've added to it Okay, so I said it's got a width of 20 a height of 20 And the reason is that is the default size of a turtle in python if I change this to 230 it's not going to change the size It's just it's just there for mathematical purposes And then these two values are very very important dy and dx dy is your speed up and down So positive value is up a negative value is down dx is your speed left and your speed right even though I won't really be using it here So a positive dx means it would move right a negative dx is it would move left? And what I've added here is something called a state So I'm basically saying that the jumper is ready and that'll come in handy a little bit later And then I want the jumper to go to negative 200 and then ground level plus half of its height So if I don't have this this is something You might find weird, but because it's the centered so I don't want the jumper to be below the line I want it to be on the line. So I have to add half of the height. That's why I put that Height equals 20 in there. So that is the starter code I will put a link to this starter code So if you want to follow along you can just download that and then start following along Otherwise you do a lot of pausing and reproducing this and then I'll also put a link to the final code down below as well So anyway, let us get started. So the things we need to think about If a character is gonna move Let's go ahead and make our character move a little bit. So I'm gonna make a new function I'm gonna call it jump go figure And in this method, I'm gonna say jumper dot dy equals oh Let's try five see what happens. Okay, and Then in here what I need to do is I need to say something say, you know move the jumper So I'm gonna do this y equals Jumper dot y core Okay, notice there's parentheses here. So this gets me the current y coordinate of the jumper Where is it? Okay, right? It when we start it should be at the ground level And then I'm gonna say y plus equals jumper Dot dy Okay, so jumper dot dy so it's gonna add The jumpers dy to the y coordinates and then what I have to do is jumper dot Set y not set x To the new y value now. I could combine all three of these into one Line I have them three lines. So it's simple and clearer for beginners to understand what's going on So I'm gonna run this and nothing's gonna happen and I'll explain why in a second Okay, so I'm gonna go ahead and press the spacebar and realize I didn't connect the space key to my function So what I do is a win dot listen again If you've seen my other tutorials, you know this win dot on key press now if you're using Python 2 It's on key Python 3 on key press jump Comma notice no parentheses here even though there's parentheses here We don't put them here and then the key that I'm using is space I think on Python 2 it's a capital S But in Python 3 it is a small s. I want to run it And watch what happens when I push the spacebar It flies up and off the screen, which is a good start, but not quite where we want to end up So in the real world, we have to ask ourselves what keeps us from flying into the sky And hopefully the answer will come to you and it is gravity So we need to set gravity for our game So I'm gonna go ahead. I'm gonna put a New value here gravity notice. I'm capitalizing it because gravity is not going to change Gravity is pulling us down. So I'm gonna make it minus. I'm gonna say zero point I don't know. We'll just 0.3. We'll pick 0.3 for now And then I actually have to add that into here Now here's the interesting thing gravity doesn't work on why It works on dy So I'm gonna say You know gravity just put gravity here so we know what this section does and I'm gonna say jumper dot dy plus equals Gravity So what that's gonna do is every time through our main game loop Gravity is going to reduce our dy So let's run that and see what happens Okay, you see how it fell right down through the floor Because we have gravity. Let me try that again. I'm gonna try to hit the space bar real quick Okay, so you can see using the space bar. I can keep myself afloat Now if this was a flappy bird game, that would be all we need to do to make the game work And we can we can make a flappy bird thing with that, but I'm thinking more like the crone dinosaur game for this so What we need to add now Is a way for it to stop when it hits the The ground level and so I'm gonna put that down here Now don't put it first because it ends up not working the order the code here is really kind of important and So yeah, put it here Deal with that. So what I'm gonna do is say if jumper dot y core again parentheses is less than Ground level Okay, and just so you know, I'm using the idea. I'm using is genie or text editor We want to call it and it is free and open source. It's very cool. Check it out It has some nice autocomplete features if you enable them plus Jumper dot height again like I talked about earlier divided by two So if I'm below the ground I'm gonna move myself back up to the ground. So I'm gonna say jumper dot set y Ground level and then I'm going to set My dy to zero. Okay, so let's go ahead and run that See what happens. Okay. Okay. We got a couple problems, which I expected. That's okay Okay, so it's the ground level. This should be ground level plus jumper dot height divided by two Let's try that again. Okay, and now we have a Jumping character. There's one little flaw with this. Now watch what happens here. I Can keep flying up and off the screen, which is not really what we want to happen. Okay, it's kind of cool But it's not what we want to happen in this particular game so what we need to do here is we need to keep track of Whether the jumper is jumping Okay, so that's why I used this jump state Now there's other ways we could do this, but this I like to do it this way. So in here what I want to say I would say if Jumper dot state Equals ready then it can jump. So let's run that So I'm gonna push the spacebar jumps. Okay, so you see didn't really do anything So what we got to do down here Or here actually I got to change the state jumper dot state equals called jumping So our jumper is gonna have two states ready and jumping. So if he's ready jump Change the dy and then do the jump. I hit f function f5 again run it Okay jumped now. It's not jumping because the state is still Jumping so then down here when I hit the ground. I got to do jumper dot state equals Ready so this tells me that I can go ahead and jump again. Let's go ahead and run that Jump jump jump jump around That's basically it now the thing with this program is that everybody's computer is gonna run just a bit Different at a different speed. So you're gonna see some really high frame rates I'm gonna see some lower frame rates. So I kind of played around with the numbers and I found that 12 and negative 0.8 So for gravity negative 0.8 and dy 12 for the jumper worked really really well For what I want to do at least so you can see boom jumps up jumps up and comes down So you will have to play with the numbers so that it gets to about you know that point You can be a little higher to be a little lower or whatever you want to do But you can see here. We've got a nice little jumping character and that's really all there is to it Okay, so just to kind of do a quick review. I had to pick the ground level and Then you know, I made a little turtle jumper turtle character. I gave it a width and a height A dx and a dy even though I didn't really use dx or or the width in this particular case It will come in handy if you decide to expand this program. I Set it to the location plus half of a tight Set gravity to a constant again my computer negative 0.8 works really well You might want something a little bit different. You might want to make your jumper Yeah, give it give your jumper a little bit stronger legs You can even be like a power-up type thing depends or a level based on the level of the game You can kind of play around with that We'll go back to 12. So then I gave it a state So if it's ready, then it can jump and I switched the state And of course I talked about the keyboard binding here So in my loop, I need to adjust for gravity I need to actually move the jumper and Then I need to check if the jumper has hit the ground if so I need to reset the dy I need to reset the state and I need to set it on the ground level So let's go ahead just one more time And there we go that is it so yeah, that's that's all I want to show you today again I get this request a lot It's it's it's harder than it looks actually if this code is out of order Or if you don't quite have this set up correctly with the whole state thing It just doesn't work as well as you would think and your thing gets stuck and gets very frustrating trust me I know so anyway. Thanks for watching subscribe for updates and keep on coding. Have a great day. Have a great weekend. Cheers