Why use Arkos Code Snippets?
When programming with Scratch, we often encounter some common and generalizable functional needs, such as:
- Calculate the distance between two points;
- Calculate the direction from one point to another;
- When making a large map game, get rid of Scratch's limitations on the size and the position of characters;
- Sort lists conveniently;
However, as vanilla Scratch does not provide relevant blocks, we have to write our own custom blocks, which is not only a waste of time but also a hassle.
In the hope of addressing some common needs, some users in Cocrea like Arkos and _30 wrote this extension after summarizing some code snippets that might be commonly used.
Arkos Code Snippets contains 5 modules:
🚶♂️ Coordinates & Directions
Coordinate and direction related blocks, such as distance calculation.
🔠 String Processing
String processing related blocks, such as finding the substring within a string.
A collection of practical blocks, such as a case sensitive block.
📄 Information Acquisition
Information not provided in vanilla Scratch, such as effect values, whether sprites are hidden, etc.
📊 Sorted Table
Tables that automatically sort according to the sorting value, which can be used for layer sorting and other scenarios.
Functions and Definition
🚶♂️ Coordinates & Directions
Direction from point 1 to point 2
Function: Return the direction from point 1 to point 2
(It returns the direction in Scratch coordinates, where 0 degrees is directly above)
For example, for (0,0) to (1,1), it will return 45°; for (0,0) to (1,0), it will return 90°.
Direction 2 minus direction 1
Function: Return the minimum angle difference from direction 1 to direction 2
(The result is signed, clockwise as positive numbers, counterclockwise as negative numbers)
- From 45 to 90, returns 45;
- From 45 to 10, returns -35;
- From -179 to 179, returns -2.
Distance between two points
Function: Return the distance between two points
Turn a certain number of degrees toward a certain direction
Function: Turn a certain number of degrees toward a certain direction.
🔠 String Processing
Find the position of the substring within the string
Function: Find the position of a string within another string, starting at the specified position.
For example, finding “na” in “banana” starting at the fourth position will return 5.
Insert a substring at a certain position in a string
Function: Insert a certain string before the NTH position in a string
For example, inserting “b” before position 2 of “ac” yields “abc”.
Replace part of a string with another string
Function: Replace the characters from position n to position m in the string with a certain string.
For example, replacing the third to fourth characters in “ABCDEF” with “XX” will yield “ABXXEF”.
Equals sign, but case sensitive
Function: Same as the original "=", the only difference is that it is case sensitive.
“< （）>” block
Function: Convert a reporter block to a boolean block.
(Returns false if the value is 0, false, NaN, or null; otherwise returns true.)
Whereas the code is elegant and easy to understand with the blocks below.
Example 2: We often code as below, with ones and zeros representing yes or no.
Now we can directly use the following code, elegant!
Comparisons on either side
Function: Determines whether a value belongs to a range. The optional operators are <, ≤, and ≠
A simpler way of writing the code:
You will find this block especially useful when the variable to be compared is long.
Comparisons on either side Plus
Function: Similar to the block above, it can determine whether a variable is outside a range. Optional comparison operators: <, ≤, >, ≥, =, ≠;
Optional logical operators: or, and;
Get the code for a color
Function: Get the hexadecimal RGB code of a color. For example, the code for white is # FFFFFF.
One option is to fill in the color code directly, which is very inconvenient.
Another option is to fill in the name of the color, but this only covers a few colors and is troublesome to code.
The most elegant solution:
Force the size to be…
Function: Break Scratch's restriction on the size of a sprite and set it to any size you want.
(Used when making a large map game with an oversize background sprite)
Forcibly move to XY…
Function: Break Scratch’s restriction on the position of a sprite and move the sprite to any position. (Used when making a large map game to move the sprites to the off-screen area)
📄 Information Acquisition
Get the value of an effect
Function: Get the current value of an effect. The effect can be color, fisheye, ghost and so on.
Is a sprite hidden?
Function: Determine if a sprite is hidden.
Used when we want to know whether a sprite is hidden or not.
Current rotation style
Function: Get the current rotation style of a sprite, including all around, left-right and don’t rotate.
Get width or height of the current costume.
Function: Get width or height of the current costume.
Get the edge coordinates of the current sprite
Function: Get the edge coordinates of the current sprite, including top y, bottom y, left x, right x.
(The edge coordinates will change along with the movement of the sprite)
It can be used for more accurate calculation of the rectangular collision box of the sprite.
Is it out of stage?
Function: Determine whether the sprite is off-screen.
It can be used in large map games to determine whether monsters are in the player's view, or to determine whether clones are not on screen and delete the off-screen clones.
📊 Sorted Table
A sorted table is a table like a list, but the contents of the table are sorted by ranking value.
It can be used in scenarios such as layer ordering in Scratch.
Clear the sorted table
Function: Clear all contents of the selected sorted table.
Set the sort mode of the sorted table
Function: Set the sorted table to ascending/descending order.
Ascending: sort from smallest to largest. For example: 1, 3, 6, 13, 256;
Descending: sort from largest to smallest. For example: 100, 97, 82, 60, 0.
Add the content to the sorted table
Function: add XX to the sorted table, along with ranking value and additional data. (where XX is the name of the item, the ranking value is the basis for the sort, and the additional data is some extra information attached to the item)
For example, if you add Xiao Ming with ranking value 95 to a table that already has Xiao Ming with ranking value 100, the latter will overwrite the former.
Get the content from the sorted table
Function: Get the information of the nth item in the sorted table
Function: Get the information of the item with the specified name in the sorted table
Menu options are as follows:
The name of the item.
The position of the item in the table.
The ranking value of the item.
Any additional data attached to the item.
Get the number of rows in the sorted table
Function: Get the number of rows in the sorted table
For example, if the table has:
Student ID: 248
Student ID: 297
Then the block will return 2.
Delete XX in the sorted table
Function: Delete the content in the table.
How to create new sorted tables:
The extension will provide two initial sorted tables: list1 and list2.
You can also create new sorted tables using the following method:
Running the above block will create a sorted table if it does not exist.
The created sorted table will appear in the drop-down menu.
⚠Note: The newly created sorted table will disappear when the project is reopened.
(Luckily, when running a block containing the table that doesn’t exist, the table will be automatically created.)
Scenario A: Point toward a coordinate
Use the script below to make the sprite point toward a coordinate.
Scenario B: Point toward a clone
Usually, we use the following block to make the sprite point towards another sprite.
However, if you want to point towards a clone of a sprite, the block above won’t work as it can only point to the original sprite.
Nevertheless, we can orient a clone by orienting its coordinates:
This technique is often used to make AI creatures face their enemies or turrets face monsters in tower defense games, etc.
Scenario C: Rotate towards a certain direction
Sometimes we want the turrets to rotate towards the enemy slowly in a tower defense game, or make a tank turret rotate towards the mouse rather than point to the mouse instantly. How to achieve that?
- Rotate towards a direction at a constant speed:
- Rotate nonlinearly towards a direction:
For example, rotate nonlinearly towards 90 degrees.
Use the following script to rotate nonlinearly towards the mouse:
Scenario D: Layer ordering
For example, using the following two pieces of code, we implemented a simple layer ordering:
(Note: “clone ID” is a private variable)
The final effect is as follows (sorting the layers according to the Y-coordinate of the ball):
Roadmap and Change Logs
Log / What’s New
Added: - add 🛠 Utilities module - add 📄 Information Acquisition - add 📊 Sorted Table
Init release - 🚶♂️ Coordinates & Directions - 🔠 String Processing