Making a character jump in a Scratch program is an essential part of learning Scratch. There are two ways of making the character jump in scratch – with and without gravity. Learn both the methods of making a character jump in the below section in this post.
How to make the character jump with gravity?
Link for the code for jumping with gravity.
https://scratch.mit.edu/projects/984277930
Step 1: Make the background and a sprite as shown below
Add a value of -7 for x and -110 for y so that the sprite stands on the ground as shown above.
Step 2: Make the character fall because of gravity when the game starts
We would create a variable called gravity with a value of -10, its negative value as it pulls the sprite down, which means decreasing the value of y. In a forever loop, the y value of the sprite will change by the amount of the variable gravity , which is -10, resulting in pulling the sprite down all the time as the game starts.
Step 3: Stop the sprite at its original position while pulled by gravity
Add the code below to the above code inside the forever loop.
It checks whether the sprite y position is less than -111, while coming down, which means the sprite has gone down from its original position, then the value of gravity is set to 0 as the sprite is on the ground.
Step 4: Make the character move up when space key is pressed
Add the code below which checks whether the space key is pressed and the sprite is on the ground ( y position is -112), change the value of y by 10 in a repeat loop.
Step 5: Set the gravity to -10 when the space key is pressed
As we changed the value of gravity to 0 when the sprite lands on the ground, the value of the gravity should be set to -10 when the space key is pressed so that gravity is acting upon the sprite.
How to make the character jump without gravity?
Link for the code for jumping without gravity.
https://scratch.mit.edu/projects/984403161
Step 1: Make the background and a sprite as shown below
Add a value of -7 for x and -110 for y so that the sprite stands on the ground as shown above.
Step 2: Making the character move up when the space key is pressed.
Add the below code to the space key pressed event,
When the space key is pressed, change the y position which makes the sprite go up 10 times.
Step 3: Making the character move down after sometime
Add the below code highlighted in red , which makes the sprite come down after sometime by decreasing the value of y in a repeat loop.
Step 4: Make the sprite come to the original position when the game starts
Hope this is useful to build your own stuff in Scratch Programming, thank you.
You may like to read: Is Math Tuition Necessary?, 10 Games to Code in Scratch, & Coding Flappy Bird Game in Scratch