Jumping
Описание | Как научить агента прыгать? |
---|---|
Область знаний | Информатика |
Область использования (ISTE) | |
Возрастная категория | 7
|
Поясняющее видео | |
Близкие рецепту понятия | |
Среды и средства для приготовления рецепта: |
There are many ways to make a sprite appear to jump, depending on how you want a sprite to and how realistic it looks. This article contains a number of them, categorized from least-realistic to most realistic. The key press up key is usually used for jumping in games, but sometimes key press Space or even the mouse is used for jumping.
Simple jumping
The following is commonly used in animations and results in a sprite effectively teleporting upwards, and then downwards again.
The below script results in the sprite moving up if the up arrow is pressed.
It was used in the Scrolling Demo project by SampleProjectsTeam.
Another jumping method which works very effectively and realistic is this.
This is very fast but you can adjust the speed by adding a "wait block" before the "change y" block.
Falling
The following script "teleports" the sprite upwards, and then has it fall back down at a constant rate, until it lands on a platform.
The next script makes the sprite rise up at a constant rate and fall back down at a constant rate.
Limited Jumps
In most video games, you cannot jump more than a certain number of times, and you cannot jump in midair. This can be implemented in Scratch.
The pink blocks labeled "jump" represent a compatible jump script.
To allow you to only jump on the ground, use the following script.
Double jumping is a common element in video games. Although not possible in real life, these scripts can make you jump up to two times.
To make the sprite jump up to n times, change the 2 to an n.
Realistic Jumping
To make a simple jumping game (not to be confused with platformer), follow this script:
Then enter this snippet of code:
If one of the scripts do not work, it can be changed.
Physically Accurate Jumping
With Ground Detection
A more realistic effect for jumping commonly used in games is the following, using a variable to control the vertical speed of a sprite (simulating gravity) is this:
Without Ground Detection
If one does not need to sense the ground and simply want a realistic, gravitational jump, that will end at the starting point, the following script can replicate the jump: