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.

whenclickedforeverifkeyuparrowpressed?thentypicaljumpingkeychangeyby50wait0.5secschangeyby-50русскийкомментарийend

The below script results in the sprite moving up if the up arrow is pressed.

whenclickedforeverifkeyuparrowpressed?thentypicaljumpingkeychangeyby5end

It was used in the Scrolling Demo project by SampleProjectsTeam.

Another jumping method which works very effectively and realistic is this.

whenclickedforeverifkeyuparrowpressed?thentypicaljumpingkeyrepeat10changeyby20endrepeat10changeyby-20end

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.

whenclickedforeverifkeyuparrowpressed?thentypicaljumpingkeychangeyby50repeatuntiltouchingcolor?colorofthegroundchangeyby-5endendend

The next script makes the sprite rise up at a constant rate and fall back down at a constant rate.

whenclickedforeverifkeyuparrowpressed?thentypicaljumpingkeyrepeat10changeyby5endrepeatuntiltouchingcolor?colorofthegroundchangeyby-5endend

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.

whenclickedforeverifkeyuparrowpressed?andtouchingcolorthentypicaljumpingkey,colorofthegroundchangeyby5jumpend

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.

whenclickedforeveriftouchingcolor?thencolorofthegroundsetjumpsto0endwhenclickedforeverifkeyuparrowpressed?andtouchingcolorthentypicaljumpingkeychangeyby5thiswillkeepthefirstscriptforresettingthejumpcountchangejumpsby1jumpelseifkeyuparrowpressed?andjumps<2thenThiswillkeepyoufromjumpingmorethantwicechangejumpsby1jumpendend

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:

whenclickedgotox:*input*y:*input*setyspeedto0Thisisjustforsettingeverythingup

Then enter this snippet of code:

whenspacekeypressedsetyspeedto*input*repeatyspeed*2+1changeybyyspeedchangeyspeedby-1end

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:

whenclickedsetyspeedto0Howfastthespriteismovingupwards.foreverchangeybyyspeedwhenclickedforever...Checkiftheplayerisontheground.iftouchingcolor?thenThecoloroftheplatforms.setyspeedto0...Checkiftheplayerwantstojump.ifkeyuparrowpressed?then...Jump!setyspeedto9.9endendchangeyspeedby-0.5Gravity

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:

whenclickedforeverifkeyuparrowpressed?thensetyvelto10repeat20changeybyyvelchangeyvelby-1simulatesgravityendend

See Also