Introduction
The introduction of Python by Gandi aims to assist in Scratch creation rather than replace Scratch. Therefore, Gandi Python itself does not have the ability to run independently. All Python functions or data calls can only be accessed through Scratch blocks. Python's capabilities in data processing far exceed those of Scratch. Introducing Python can effectively enhance users' efficiency in data processing during the process of creating Scratch projects.
For example, a comparison between implementing bubble sort in Scratch and implementing bubble sort in Python:
Basic Usage Part 1
In this section, we will introduce the functionality of using a Python program to read JSON files and then accessing JSON data through Scratch blocks. Steps:
- Define a JSON file and fill it with data.
data.json
file here can be read in Python by read('data.json').read()
. Generally, in large and complex game projects, there will be some configuration information (such as level configuration, combat data, dialogue scripts, etc), based on this file system, it is convenient to write these configurations to JSON files, and then read them through Python and pass the results to Scratch. This is more intuitive and convenient than the previous common practice in Scratch (writing to Data or using a large variable).- Load the JSON file and process the data through a Python program.
data
is a global variable, and each data set through kv_set
is saved in the global data
, which can be accessed by kv_get
in different characters. With the runtime state, combined with Python's rich data types and standard library, it is easy to manage some runtime data in the game (player data, round battle data, cache data, etc.) and perform some complex processing (calculation, sorting, statistics, etc). During editing testing, you can manually clear the runtime context by using the "Compile and Reset Variables" button in the Python plugin.- Generate Scratch blocks to call Python functions.
Demonstration Video
Basic Usage Part 2
Implement a bubble sort and use Scratch blocks to sort an unordered list. Steps:
- Add your own custom function bubble_sort in the Python editor, and add the function name to the
__
all__
variable.
Here are the conversion rules:
Python types | Scratch types |
str | String |
int/float | Number |
bool | Boolean |
list/set/tuple | List |
Based on the supported data mapping relationships above, it is required that the parameter and return value types declared in Python can only be the types supported above. If a type that is not supported or has not been declared is encountered, it will be treated uniformly as (str/String).
__all__
is a special variable used to define the symbols (variables, functions, classes, etc.) exported in a module. When a module is imported, the all variable is used to specify which symbols will be exported, defining the module's public interface. Only functions exported through all can be called by external Scratch.- Click on the "Refresh blocks" button in the Python blocks category to load newly defined Python functions and convert them into blocks.
- Define an unordered list and sort it by calling the bubble sort block.
Demonstration Video
Other Features
- Integrated AI capability for automatic interpretation and code generation.
- Debugging and Console Output.