From Test-Scratch-Wiki
This article explains methods of improving 程式 for compaction and efficiency.
Reasons to Keep Scripts Compact and Efficient
Besides improving your scripts being a professional practice, some reasons why one should compact and further work on scripts include:
- To reduce 专案 slowness due to cluttered or inefficient scripts
- To make them easier to handle, so you can easily change something without having to change many scripts, and so people can make 改编es easier, by simply changing the pattern.
For example, using the methods stated later in this tutorial, this long, cluttered script:
當 @greenflag 被點擊 變數 [upgrade v] 設為 [0] 變數 [cost v] 設為 [5] 重複無限次 等待直到 <<滑鼠鍵被按下?> 且 <碰到 [滑鼠游標 v] ?>> 如果 <(upgrade) = [0]> 那麼 變數 [attack v] 改變 (10) 變數 [cost v] 設為 [8] 變數 [upgrade v] 改變 (1) 造型換成 [造型2 v] end 如果 <(upgrade) = [1]> 那麼 變數 [attack v] 改變 (20) 變數 [cost v] 設為 [12] 變數 [upgrade v] 改變 (1) 造型換成 [造型3 v] end // and so on until upgrade = 10 等待直到 <<滑鼠鍵被按下?> 不成立> end
...can be transformed into this:
當 @greenflag 被點擊 變數 [upgrade v] 設為 [0] 變數 [cost v] 設為 [5] 重複無限次 如果 <<滑鼠鍵被按下?> 且 <碰到 [滑鼠游標 v] ?>> 那麼 如果 <(upgrade) < [11]> 那麼 變數 [attack v] 改變 (((upgrades) + (1)) * (10)) 變數 [cost v] 設為 (四捨五入數值 ((cost) * (1.1))) 變數 [upgrade v] 改變 (1) 造型換成下一個 說出 [Sorry, this upgrade is maxed out.] end end end
Though this is a very simple example, this is often all you need, whereas the previous script would have 10 if statements in it.
As you can see, handling the second one would be much easier.
How to do it
There are two main things to look for in inefficient scripts:
- Scripts that can be combined
- Patterns with numbers that can be made into loops
Combining Scripts
You should see if scripts can be ran together.
Here's a good example of some scripts that can be combined:
當 @greenflag 被點擊 顯示 變數 [HP v] 設為 (100) 當 @greenflag 被點擊 廣播訊息 [gameStart v]
As you probably can see, they can easily be combined. There are much more complex instances of such, but this should make the point clear.
The two scripts do not need to be on different 半圆形积木 as they can easily be combined.
Here is a more complex example:
當 @greenflag 被點擊 造型換成 [cat v] 等待 (1) 秒 造型換成 [dog v] 等待 (1) 秒 造型換成 [cat v] 當 @greenflag 被點擊 說出 [I am a cat!] (1) 秒 等待 (1) 秒 說出 [Now I am a dog!] (1) 秒 等待 (1) 秒 說出 [And now I am a cat again!]
The pauses within the scripts is constant and lines up. Therefore, the following combination is possible:
當 @greenflag 被點擊 造型換成 [cat v] 說出 [I am a cat!] (1) 秒 等待 (1) 秒 造型換成 [dog v] 說出 [Now I am a dog!] (2) 秒 等待 (1) 秒 造型換成 [cat v] 說出 [And now I am a cat again!]
However, some scripts with the same hat block cannot be combined, like these:
當 @greenflag 被點擊 廣播訊息 [start game v] 並等待 廣播訊息 [calculate score v] 並等待 當 @greenflag 被點擊 重複無限次 播放音效 [background music v] 到底 end
The pauses that the blocks make do not line up, and one is in a loop while the other is not.
Finding Patterns
This is a bit more tricky. See the scripts for this project's shop buttons for a more complex example.
For a simpler example, we will use a script shown previously:
當 @greenflag 被點擊 變數 [upgrade v] 設為 [0] 變數 [cost v] 設為 [5] 重複無限次 等待直到 <<滑鼠鍵被按下?> 且 <碰到 [滑鼠游標 v] ?>> 如果 <(upgrade) = [0]> 那麼 變數 [attack v] 改變 (10) 變數 [cost v] 設為 [8] 變數 [upgrade v] 改變 (1) 造型換成 [造型2 v] end 如果 <(upgrade) = [1]> 那麼 變數 [attack v] 改變 (20) 變數 [cost v] 設為 [12] 變數 [upgrade v] 改變 (1) 造型換成 [造型3 v] end // and so on until upgrade = 10 等待直到 <<滑鼠鍵被按下?> 不成立> end
Where's the pattern? Well, we know a few things that can give us clues on what script to make:
- There are ten upgrades, and therefore, ten costumes (assuming each upgrade changes the button's appearance).
- The attack so far is being upgraded by ten more each time. Therefore, you can assume that the attack will be upgraded in the same way up until upgrade equals 10, unless you prefer different results.
- The costumes are organized in order, and therefore, the upgrade number can correspond to them. Even better, the 造型换成下一个 block can be used.
- The cost is changed by the rounded number of the previous number times 1.5. (1.5×5≈8; 1.5×8≈12)
From these clues, this script can be made:
當 @greenflag 被點擊 變數 [upgrade v] 設為 [0] 變數 [cost v] 設為 [5] 重複無限次 等待直到 <<滑鼠鍵被按下?> 且 <碰到 [滑鼠游標 v] ?>> 如果 <(upgrade) < [11]> 那麼 變數 [attack v] 改變 (((upgrades) + (1)) * (10)) 變數 [cost v] 設為 (四捨五入數值 ((cost) * (1.5))) 變數 [upgrade v] 改變 (1) 造型換成下一個 說出 [Sorry, this upgrade is maxed out.] end 等待直到 <<滑鼠鍵被按下?> 不成立> end
Tip
Improving your scripts is something that should be done, but it is much easier to improve and find patterns in scripts after you have made them. Make your scripts so that they do what you want them to do, then improve them, unless you already have efficient scripts.