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.

when green flag clicked
forever
  if <key [up arrow v] pressed?> then //typical jumping key
    change y by (50)
    wait (0.5) secs
    change y by (-50) // русский комментарий
  end

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

when green flag clicked
forever
  if <key [up arrow v] pressed?> then //typical jumping key
    change y by (5)
  end

It was used in the Scrolling Demo project by SampleProjectsTeam.

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

when green flag clicked
forever
  if <key [up arrow v] pressed?> then //typical jumping key
    repeat (10)
      change y by (20)
  end
    repeat (10)
      change y by (-20)
  end

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.

when green flag clicked
forever
  if <key [up arrow v] pressed?> then //typical jumping key
    change y by (50)
    repeat until <touching color [#000000]?> //color of the ground
      change y by (-5)
    end
  end
end

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

when gf clicked
forever
if <key [up arrow v] pressed?> then //typical jumping key
repeat (10)
change y by (5)
end
repeat until <touching color [#000000]?> //color of the ground
change y by (-5)
end
end

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.

when gf clicked
forever
if <<key [up arrow v] pressed?> and <touching color [#000000]>> then // typical jumping key, color of the ground
change y by (5)
jump::custom
end

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.

when gf clicked
forever
if <touching color [#000000]?> then // color of the ground
set [jumps v] to [0]
end

when gf clicked
forever
if <<key [up arrow v] pressed?> and <touching color [#000000]>> then // typical jumping key
change y by (5) // this will keep the first script for resetting the jump count
change [jumps v] by (1)
jump::custom
else
if <<key [up arrow v] pressed?> and <(jumps) < [2]>> then // This will keep you from jumping more than twice
change [jumps v] by (1)
jump::custom
end
end

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:

when green flag clicked
go to x: [*input*] y: [*input*]
set [y speed v] to [0] //This is just for setting everything up

Then enter this snippet of code:

when [space v] key pressed
set [y speed v] to [*input*]
repeat (((y speed) * (2)) + (1))
change y by (y speed)
change [y speed v] by [-1]
end

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:

when green flag clicked
set [y speed v] to [0] //How fast the sprite is moving upwards.
forever
  change y by (y speed)

when green flag clicked
forever
  ...//Check if the player is on the ground.
  if <touching color [#000000]?> then // The color of the platforms.
    set [y speed v] to (0)
    ...//Check if the player wants to jump.
    if <key [up arrow v] pressed?> then
      ...//Jump!
      set [y speed v] to (9.9)
    end
  end
  change [y speed v] by (-0.5) // Gravity

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:

when gf clicked
forever
if <key [up arrow v] pressed?> then
set [y vel v] to (10)
repeat (20)
change y by (y vel)
change [y vel v] by (-1) //simulates gravity
end
end

See Also