Lesson 2 of 4
Gravity and Jumping
Add one number for up and down speed. The rectangle now has weight. It jumps in a curve and lands.
What you will learn
- Velocity
- Gravity
- Ground collision
- Stopping the double jump
Before you start
The Game Loop. You use the same two files. update() now does more work.
What this stage does
Click the game and press the up arrow or W to jump. The player rises, slows down, falls, and lands on the ground. You cannot jump again until you land.
How gravity works
Jumping is not "move the player up". It is one number that changes on every frame. velocityY is how fast the player is moving up or down right now.
player.velocityY -= gravity; // gravity pulls down, so it takes speed away
player.y += player.velocityY; // move by the current speedA jump sets velocityY to a large positive number one time. Gravity does the rest of the work. The player rises, slows down, stops, and falls. You never have to write the curve yourself.
Landing, and not jumping twice
isJumping is a true-or-false value. It stops you from jumping again while you are in the air. The code sets it to true when you leave the ground. The code sets it back to false when you land.
// Jump only when we are on the ground
if ((keys["ArrowUp"] || keys["KeyW"]) && !player.isJumping) {
player.velocityY = player.jumpPower;
player.isJumping = true;
}
// ...after gravity has moved the player:
if (player.y <= groundTop) {
player.y = groundTop; // put the feet exactly on the ground
player.velocityY = 0; // stop falling
player.isJumping = false; // you may jump again
}Build it step by step
Add the new numbers
Add
jumpPower,velocityY, andisJumpingto theplayerobject. Addgravityas a constant near the top of the file.groundTopis already there from the last stage.Add gravity only
Add the two gravity lines to
update(). Nothing else. Reload. The player falls straight through the floor and disappears. That is correct for now.Add the floor
Add the ground check. The player now falls and lands. There is still no jump.
Add the jump
Add the jump key check. Leave out the
!player.isJumpingpart at first. Hold the up arrow and the player flies. Now add that part and try again. You get one jump. You must land before you can jump again.Tune the feel
Change
gravityto 0.2, then to 1.5. ChangejumpPowerto 8, then to 25. Two numbers control how the whole game feels. Spend real time here.
Tune the feel
Two numbers decide how the whole game feels. Try low gravity with a small jump. Then try high gravity with a big jump.
How much speed gravity adds each frame. A low number makes the player float down slowly.
This is the speed the player leaves the ground at.
Try these changes
Make these changes in the files beside this guide. Each one only uses what this lesson has already covered. Tick each one when it works.
Check yourself
Three quick questions on this lesson. Get them all right to finish the lesson and earn a pack.
Loading your answers
Teacher's versionHow to run this lesson, what to check, and any answers. Students do not need this.
How to teach it
- Run step 2 on its own. Let the player fall out of the world. The missing floor is easy to see. The student will name the problem without your help.
- Let the student fly in step 4 before they add
isJumping. Students enjoy the flying player. It also shows them thatisJumpingis needed, not a random rule. - Draw the arc on the board as a list of
velocityYvalues. Write 15, 14.4, 13.8, and keep going down through 0 into negative numbers. Gravity is one subtraction, repeated every frame. - Step 5 is not filler. Give it ten minutes. Tuning two numbers is the first time most students feel like designers instead of typists.