Logo
  • Features
  • Educators
  • What's New
  • Blog
  • Showcase
  • About us
  • Contact
Try it now!
Arkos Code Snippets
Arkos Code Snippets

Arkos Code Snippets

Why use Arkos Code Snippets?

When programming with Scratch, we often encounter some common and generalizable functional needs, such as:

  1. Calculate the distance between two points;
  2. Calculate the direction from one point to another;
  3. When making a large map game, get rid of Scratch's limitations on the size and the position of characters;
  4. Sort lists conveniently;
  5. ……

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.

Modules

Arkos Code Snippets contains 5 modules:

Module
Description
🚶‍♂️ 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.
🛠 Utilities
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

image

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

image

Function: Return the minimum angle difference from direction 1 to direction 2

(The result is signed, clockwise as positive numbers, counterclockwise as negative numbers)

Examples:

  1. From 45 to 90, returns 45;
  2. From 45 to 10, returns -35;
  3. From -179 to 179, returns -2.

Distance between two points

image

Function: Return the distance between two points

Turn a certain number of degrees toward a certain direction

image

Function: Turn a certain number of degrees toward a certain direction.

🔠 String Processing

Find the position of the substring within the string

image

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

image

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

image

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”.

🛠 Utilities

Equals sign, but case sensitive

image

Function: Same as the original "=", the only difference is that it is case sensitive.

image
image

“< ()>” block

image

Function: Convert a reporter block to a boolean block.

(Returns false if the value is 0, false, NaN, or null; otherwise returns true.)

💡
Example 1: The reporter block below cannot fit in the if-then block, so it's a little bit troublesome to use.
image

Whereas the code is elegant and easy to understand with the blocks below.

image

Example 2: We often code as below, with ones and zeros representing yes or no.

image
image

Now we can directly use the following code, elegant!

image
image

Comparisons on either side

image

Function: Determines whether a value belongs to a range. The optional operators are <, ≤, and ≠

💡
Example: The code is traditionally written as follows:
image

A simpler way of writing the code:

image

You will find this block especially useful when the variable to be compared is long.

Comparisons on either side Plus

image

Function: Similar to the block above, it can determine whether a variable is outside a range. Optional comparison operators: <, ≤, >, ≥, =, ≠;

Optional logical operators: or, and;

💡
Example:
image

Get the code for a color

image

Function: Get the hexadecimal RGB code of a color. For example, the code for white is # FFFFFF.

💡
Example: Sometimes we write custom blocks for drawing, but struggle with how to fill in the parameters for color:
image

One option is to fill in the color code directly, which is very inconvenient.

image
image

Another option is to fill in the name of the color, but this only covers a few colors and is troublesome to code.

image
image

The most elegant solution:

image

Force the size to be…

image

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…

image

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

image

Function: Get the current value of an effect. The effect can be color, fisheye, ghost and so on.

Is a sprite hidden?

image

Function: Determine if a sprite is hidden.

Used when we want to know whether a sprite is hidden or not.

Current rotation style

image

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.

image

Function: Get width or height of the current costume.

Get the edge coordinates of the current sprite

image

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?

image

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

image

Function: Clear all contents of the selected sorted table.

Set the sort mode of the sorted table

image

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

image

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)

⚠️
Note: If there is already an item that has the same name as the content to be added in the table, the later addition will overwrite what already exists.

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

image

Function: Get the information of the nth item in the sorted table

image

Function: Get the information of the item with the specified name in the sorted table

Menu options are as follows:

Property
Description
Name
The name of the item.
Rank
The position of the item in the table.
Ranking value
The ranking value of the item.
Extra
Any additional data attached to the item.

Get the number of rows in the sorted table

image

Function: Get the number of rows in the sorted table

For example, if the table has:

Name
Ranking value
Additional data
Xiao Ming
95
Student ID: 248
Little Red
90
Student ID: 297

Then the block will return 2.

Delete XX in the sorted table

image

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:

💡
Use the following script to create a new table:
image

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.

image

⚠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.)

Best Practices

Scenario A: Point toward a coordinate

Use the script below to make the sprite point toward a coordinate.

image

Scenario B: Point toward a clone

Usually, we use the following block to make the sprite point towards another sprite.

image

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:

image

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?

  1. Rotate towards a direction at a constant speed:
image
image
  1. Rotate nonlinearly towards a direction:
  2. For example, rotate nonlinearly towards 90 degrees.

    image

Use the following script to rotate nonlinearly towards the mouse:

image
image

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)

image
image

The final effect is as follows (sorting the layers according to the Y-coordinate of the ball):

image

Roadmap and Change Logs

Version
Log / What’s New
1.1
Added: - add 🛠 Utilities module - add 📄 Information Acquisition - add 📊 Sorted Table
1.0
Init release - 🚶‍♂️ Coordinates & Directions - 🔠 String Processing
Logo

Designed by 2ndR with love

Discord