Single Frame: различия между версиями

Материал из Поле цифровой дидактики
м
 
 
(не показаны 2 промежуточные версии этого же участника)
Строка 1: Строка 1:
'''Single frame''' is a way to make a script run faster than normal.
'''Single frame''' is a way to make a script run faster than normal.


Scratch loop [[blocks]] are programmed with a slight delay between repetitions. If this delay did not exist, simple motion scripts could cause a sprite to fly or even disappear off the screen instantly. The [[Control Blocks|control blocks]] and [[Motion Blocks|motion blocks]] have this delay. During this delay, an additional screen refresh is done: Scratch redraws the entire Stage. This is also a big source of lag.{{citation needed|date=May 2017}}
Scratch loop [[blocks]] are programmed with a slight delay between repetitions. If this delay did not exist, simple motion scripts could cause a sprite to fly or even disappear off the screen instantly. The [[Control Blocks|control blocks]] and [[Motion Blocks|motion blocks]] have this delay. During this delay, an additional screen refresh is done: Scratch redraws the entire Stage. This is also a big source of lag.


The Single Frame technique can be used to bypass this slight delay, so that computationally-heavy tasks (for example, [[Raycasting]] or [[Raytracing]], or complex string manipulation and parsing) run quickly. It is like the [[Turbo Mode|turbo mode]] function. The easiest way is to remove all of the blocks mentioned above so that the screen is refreshed only once at the end of a script: all drawing happens in a "single frame".
The Single Frame technique can be used to bypass this slight delay, so that computationally-heavy tasks (for example, [[Raycasting]] or [[Raytracing]], or complex string manipulation and parsing) run quickly. It is like the [[Turbo Mode|turbo mode]] function. The easiest way is to remove all of the blocks mentioned above so that the screen is refreshed only once at the end of a script: all drawing happens in a "single frame".
Строка 43: Строка 43:
[[Procedures|Custom blocks]] can be made to run atomically. In [[Scratch 2.0]] and later versions, checking the "Run Without Screen Refresh" box in a custom block's edit menu sets the custom block to run atomically. The [[All at Once (block)|All at Once]] block, which is no longer in Scratch because of the option to run a custom block without screen refresh, evaluated the argument atomically, however, but still had the built-in lag.
[[Procedures|Custom blocks]] can be made to run atomically. In [[Scratch 2.0]] and later versions, checking the "Run Without Screen Refresh" box in a custom block's edit menu sets the custom block to run atomically. The [[All at Once (block)|All at Once]] block, which is no longer in Scratch because of the option to run a custom block without screen refresh, evaluated the argument atomically, however, but still had the built-in lag.


{{Block Frame|blocks=<scratchblocks>
<scratchblocks>
pen down
pen down
all at once {
all at once {
Строка 51: Строка 51:
     end
     end
} :: control
} :: control
</scratchblocks>|caption=The [[All at Once (block)|All at Once block]] in action.}}
</scratchblocks>


== Problems ==
== Problems ==
Строка 71: Строка 71:


== BYOB Single Frame ==
== BYOB Single Frame ==
{{snap!imgs}}
 
[[File:Warp.png|right|frame|The warp block]]
The [[Scratch Modification]] [[Build Your Own Blocks (Scratch Modification)|BYOB]] allows atomic procedures. To make a procedure (custom block) atomic in BYOB 3.1 and below, check the "atomic?" checkbox in the block editor dialog. In [[Snap!]], use the warp block to make the enclosed script atomic when run.
The [[Scratch Modification]] [[Build Your Own Blocks (Scratch Modification)|BYOB]] allows atomic procedures. To make a procedure (custom block) atomic in BYOB 3.1 and below, check the "atomic?" checkbox in the block editor dialog. In BYOB4.0/Snap!, use the warp block to make the enclosed script atomic when run.


In both versions, recursion is atomic. So an atomic repetition block can be written, which takes a lambda input and recursively evaluates the lambda and then evaluates itself, decrementing the number of repetitions:
In both versions, recursion is atomic. So an atomic repetition block can be written, which takes a lambda input and recursively evaluates the lambda and then evaluates itself, decrementing the number of repetitions:


[[File:Recursive-warp.png]]
 
[[Category:Scripting Tutorials]]
[[Category:Scripting Tutorials]]
[[Category:Scratch Program]]
[[Category:Computer Science]]
[[Category:Project Items]]

Текущая версия на 13:53, 31 марта 2023

Single frame is a way to make a script run faster than normal.

Scratch loop blocks are programmed with a slight delay between repetitions. If this delay did not exist, simple motion scripts could cause a sprite to fly or even disappear off the screen instantly. The control blocks and motion blocks have this delay. During this delay, an additional screen refresh is done: Scratch redraws the entire Stage. This is also a big source of lag.

The Single Frame technique can be used to bypass this slight delay, so that computationally-heavy tasks (for example, Raycasting or Raytracing, or complex string manipulation and parsing) run quickly. It is like the turbo mode function. The easiest way is to remove all of the blocks mentioned above so that the screen is refreshed only once at the end of a script: all drawing happens in a "single frame".

Example

For example, the following scripts perform the same task in essence, but if run, in the second script the sprite will flicker while in the first it will remain still:

when green flag clicked
go to x: (100) y: (0)
forever // single frame
set x to ((x position) * (-1))
set x to ((x position) * (-1))
set x to ((x position) * (-1))
set x to ((x position) * (-1))
set x to ((x position) * (-1))
set x to ((x position) * (-1))

when green flag clicked
go to x: (100) y: (0)
forever
repeat (6) // not single frame
    set x to ((x position) * (-1))
end

By performing the action 6 times directly, rather than using the repeat block, the script works slightly faster and thus the flickering is gone.

Single Frame works in the Scratch 3.0 Online Editor and Offline Editor. It may not work on older versions of Scratch.

Atomicity

Single-frame is related to the concept of atomicity. Atomic scripts run completely through and then show the end result. Most programming languages provide atomic looping only. For learners, though, non-atomic looping is easier to understand. For example, the following script reacts differently in atomic and non-atomic interpreters:

when gf clicked
repeat (180)
turn cw (1) degrees
end

In an atomic interpreter, the script would pause for a second, then the sprite would suddenly turn upside-down. In a non-atomic interpreter like Scratch, the sprite would rotate slowly until it is upside-down.

Atomic Procedures

Custom blocks can be made to run atomically. In Scratch 2.0 and later versions, checking the "Run Without Screen Refresh" box in a custom block's edit menu sets the custom block to run atomically. The All at Once block, which is no longer in Scratch because of the option to run a custom block without screen refresh, evaluated the argument atomically, however, but still had the built-in lag.

pen down
all at once {
    repeat (300)
        turn right (15) degrees
        move (10) steps
    end
} :: control

Problems

Using the single-frame technique can/will slow down scripts in other loops, as Scratch tries to run through every loop at the same rate (fast loops generally will not begin their second repetition until the slowest loops are done). As a result, scripts that may have been faster before the implementation of the single-frame technique is applied to one of the loops will usually run slower.

Another problem is the readability of scripts. Complex single-frame projects often have large scripts which are hard to understand. Also, Scratch sometimes cannot draw large scripts properly, and cuts off the bottom few blocks of the script, and/or causes massive lag.

Larger scripts are harder to edit as the Squeak environment slows down. A lag of many seconds can develop in enormous scripts, making edits tough. Mainly adding blocks, dragging out blocks, changing input values, and adding comments gets delayed.

It is impossible to make a Forever block single-frame. Placing a forever loop in an atomic custom block will cause your project to lag severely. Шаблон:Cn

Tips

Some tips to make the process of making single-frame projects easy are:

  • Use the duplicate function. If you need to single-frame a repeating script, duplicate the script that needs to be repeated the appropriate number of times by right-clicking the top block of the script and selecting "duplicate".
  • Try to avoid manually changing values in blocks as it is time-consuming. For example, if a value needs to be continually incremented each repetition, it is faster to replace it by a variable and add a "change variable by" block at the end of each repetition.

Effects of Turbo Mode

Turbo Mode runs a project without the slight delays inherent in loop blocks, and redraws the stage at regular intervals. Using this method and then running a project in turbo mode decreases the effectiveness of turbo mode, when compared with the same project running at normal speed. The single-frame technique can often be used to allow complex projects to run without the aid of turbo mode.

BYOB Single Frame

The Scratch Modification BYOB allows atomic procedures. To make a procedure (custom block) atomic in BYOB 3.1 and below, check the "atomic?" checkbox in the block editor dialog. In Snap!, use the warp block to make the enclosed script atomic when run.

In both versions, recursion is atomic. So an atomic repetition block can be written, which takes a lambda input and recursively evaluates the lambda and then evaluates itself, decrementing the number of repetitions: