pydartsnut-scratch 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pydartsnut_scratch-0.1.0/.gitignore +6 -0
- pydartsnut_scratch-0.1.0/PKG-INFO +92 -0
- pydartsnut_scratch-0.1.0/README.md +80 -0
- pydartsnut_scratch-0.1.0/pyproject.toml +27 -0
- pydartsnut_scratch-0.1.0/scripts/make_fixture.py +202 -0
- pydartsnut_scratch-0.1.0/scripts/make_flag_test.py +284 -0
- pydartsnut_scratch-0.1.0/scripts/probe_controls.py +118 -0
- pydartsnut_scratch-0.1.0/scripts/probe_e2e.py +95 -0
- pydartsnut_scratch-0.1.0/src/pydartsnut_scratch/__init__.py +3 -0
- pydartsnut_scratch-0.1.0/src/pydartsnut_scratch/__main__.py +185 -0
- pydartsnut_scratch-0.1.0/src/pydartsnut_scratch/blocks.py +1461 -0
- pydartsnut_scratch-0.1.0/src/pydartsnut_scratch/control.py +28 -0
- pydartsnut_scratch-0.1.0/src/pydartsnut_scratch/costume.py +76 -0
- pydartsnut_scratch-0.1.0/src/pydartsnut_scratch/costume_svg.py +481 -0
- pydartsnut_scratch-0.1.0/src/pydartsnut_scratch/inputs.py +73 -0
- pydartsnut_scratch-0.1.0/src/pydartsnut_scratch/opcodes.py +135 -0
- pydartsnut_scratch-0.1.0/src/pydartsnut_scratch/project.py +239 -0
- pydartsnut_scratch-0.1.0/src/pydartsnut_scratch/renderer.py +415 -0
- pydartsnut_scratch-0.1.0/src/pydartsnut_scratch/runtime.py +516 -0
- pydartsnut_scratch-0.1.0/src/pydartsnut_scratch/values.py +119 -0
- pydartsnut_scratch-0.1.0/tests/expected_gaps.txt +1 -0
- pydartsnut_scratch-0.1.0/tests/fixtures/tracer.sb3 +0 -0
- pydartsnut_scratch-0.1.0/tests/test_control.py +41 -0
- pydartsnut_scratch-0.1.0/tests/test_costume.py +186 -0
- pydartsnut_scratch-0.1.0/tests/test_engine.py +1602 -0
- pydartsnut_scratch-0.1.0/tests/test_flag_project.py +44 -0
- pydartsnut_scratch-0.1.0/tests/test_opcodes.py +82 -0
- pydartsnut_scratch-0.1.0/tests/test_values.py +66 -0
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: pydartsnut-scratch
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Scratch 3 interpreter for the Dartsnut panel
|
|
5
|
+
Requires-Python: >=3.12
|
|
6
|
+
Requires-Dist: numpy
|
|
7
|
+
Requires-Dist: pydartsnut==1.4.0
|
|
8
|
+
Requires-Dist: skia-python
|
|
9
|
+
Provides-Extra: dev
|
|
10
|
+
Requires-Dist: pytest; extra == 'dev'
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
|
|
13
|
+
# pydartsnut-scratch
|
|
14
|
+
|
|
15
|
+
A Scratch 3 interpreter shared by the Dartsnut desktop app and firmware. It
|
|
16
|
+
runs `.sb3` projects at 30 ticks per second and renders a 128×96 stage at the
|
|
17
|
+
top of the 128×160 panel. Unsupported opcodes are logged once and skipped.
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
|
|
21
|
+
Use Python 3.12 or later. From the repository root on macOS or Linux:
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
python3.12 -m venv .venv
|
|
25
|
+
.venv/bin/python -m pip install -e '.[dev]'
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
On Windows, use `py -3.12 -m venv .venv` and run the Python commands with
|
|
29
|
+
`.venv\Scripts\python.exe`. For desktop development, point
|
|
30
|
+
`DARTSNUT_SCRATCH_ENGINE_DEV` at the absolute path to `.venv` before launching
|
|
31
|
+
the app. The engine uses the `pydartsnut` 1.4.0 shared-memory ABI.
|
|
32
|
+
|
|
33
|
+
## Run
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
<engine-venv>/bin/python -m pydartsnut_scratch --sb3 project.sb3 --shm <PDI name> --data-store <dir> [--seed N]
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
The app launches exactly that command line. Errors that the user must read are
|
|
40
|
+
one line on stderr, and the process exits non-zero.
|
|
41
|
+
|
|
42
|
+
## Contract
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
argv: <engine-venv>/bin/python -m pydartsnut_scratch --sb3 <abs path> --shm <PDI shared-memory name> --data-store <dir> [--seed N]
|
|
46
|
+
env: DARTSNUT_DATA_STORE=<dir> (same value as --data-store)
|
|
47
|
+
DARTSNUT_SCRATCH_SEED=<N> (optional; same value as --seed)
|
|
48
|
+
control file: <data-store>/control.json, written atomically by the app, polled every tick:
|
|
49
|
+
{"seq": 12, "action": "greenflag"} | {"seq": 13, "action": "stop"}
|
|
50
|
+
apply only when seq is greater than the last seq applied; ignore unreadable/absent file
|
|
51
|
+
frames: full panel 128x160 RGB888 = 61440 bytes, pushed through pydartsnut's update_frame_buffer
|
|
52
|
+
stage area: 128x96 at the top of the panel (y 0..95); rows 96..159 black
|
|
53
|
+
tick: 30 frames per second
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## What runs
|
|
57
|
+
|
|
58
|
+
The engine dispatches blocks for motion, looks, events, control, sensing,
|
|
59
|
+
operators, variables and lists, custom blocks, pen, and sound timing. Use
|
|
60
|
+
`--vocabulary` to print the implemented opcode list or `--census --sb3 project.sb3`
|
|
61
|
+
to see which opcodes a project uses that the engine cannot run.
|
|
62
|
+
|
|
63
|
+
A dart impact updates the pointer, sets `mouse down?` for one poll, and
|
|
64
|
+
starts the `dart` broadcast and click hats during a run. The pointer keeps
|
|
65
|
+
the most recent landing position. The six panel buttons map to the arrows,
|
|
66
|
+
`a`, and `b`, with Scratch `space` mapped to A.
|
|
67
|
+
|
|
68
|
+
## Limits
|
|
69
|
+
|
|
70
|
+
- The stage and silhouette collisions use the panel’s 128×96 pixel lattice.
|
|
71
|
+
Thin artwork and grazing collisions can differ from Scratch's 480×360 stage.
|
|
72
|
+
- Some graphic effects are approximations. SVG text uses installed fonts, and
|
|
73
|
+
SVG styles and grouped image/text compositing have limited support.
|
|
74
|
+
- Sound blocks track timing and effects. Audible playback requires an audio sink.
|
|
75
|
+
- Stage monitors and `sensing_setdragmode` are unsupported.
|
|
76
|
+
|
|
77
|
+
## Develop
|
|
78
|
+
|
|
79
|
+
```sh
|
|
80
|
+
.venv/bin/python scripts/make_fixture.py # regenerate tests/fixtures/tracer.sb3
|
|
81
|
+
.venv/bin/python -m pytest
|
|
82
|
+
.venv/bin/python scripts/probe_e2e.py # shared-memory probe, with the emulator stopped
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
The control probe generates a separate project and checks that a flag starts
|
|
86
|
+
animation, stop freezes the frame, and a subsequent flag restarts the project.
|
|
87
|
+
Run it with the emulator stopped:
|
|
88
|
+
|
|
89
|
+
```sh
|
|
90
|
+
.venv/bin/python scripts/make_flag_test.py /tmp/dartsnut-flag
|
|
91
|
+
.venv/bin/python scripts/probe_controls.py /tmp/dartsnut-flag/flag-stop-test.sb3
|
|
92
|
+
```
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# pydartsnut-scratch
|
|
2
|
+
|
|
3
|
+
A Scratch 3 interpreter shared by the Dartsnut desktop app and firmware. It
|
|
4
|
+
runs `.sb3` projects at 30 ticks per second and renders a 128×96 stage at the
|
|
5
|
+
top of the 128×160 panel. Unsupported opcodes are logged once and skipped.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
Use Python 3.12 or later. From the repository root on macOS or Linux:
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
python3.12 -m venv .venv
|
|
13
|
+
.venv/bin/python -m pip install -e '.[dev]'
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
On Windows, use `py -3.12 -m venv .venv` and run the Python commands with
|
|
17
|
+
`.venv\Scripts\python.exe`. For desktop development, point
|
|
18
|
+
`DARTSNUT_SCRATCH_ENGINE_DEV` at the absolute path to `.venv` before launching
|
|
19
|
+
the app. The engine uses the `pydartsnut` 1.4.0 shared-memory ABI.
|
|
20
|
+
|
|
21
|
+
## Run
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
<engine-venv>/bin/python -m pydartsnut_scratch --sb3 project.sb3 --shm <PDI name> --data-store <dir> [--seed N]
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
The app launches exactly that command line. Errors that the user must read are
|
|
28
|
+
one line on stderr, and the process exits non-zero.
|
|
29
|
+
|
|
30
|
+
## Contract
|
|
31
|
+
|
|
32
|
+
```
|
|
33
|
+
argv: <engine-venv>/bin/python -m pydartsnut_scratch --sb3 <abs path> --shm <PDI shared-memory name> --data-store <dir> [--seed N]
|
|
34
|
+
env: DARTSNUT_DATA_STORE=<dir> (same value as --data-store)
|
|
35
|
+
DARTSNUT_SCRATCH_SEED=<N> (optional; same value as --seed)
|
|
36
|
+
control file: <data-store>/control.json, written atomically by the app, polled every tick:
|
|
37
|
+
{"seq": 12, "action": "greenflag"} | {"seq": 13, "action": "stop"}
|
|
38
|
+
apply only when seq is greater than the last seq applied; ignore unreadable/absent file
|
|
39
|
+
frames: full panel 128x160 RGB888 = 61440 bytes, pushed through pydartsnut's update_frame_buffer
|
|
40
|
+
stage area: 128x96 at the top of the panel (y 0..95); rows 96..159 black
|
|
41
|
+
tick: 30 frames per second
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## What runs
|
|
45
|
+
|
|
46
|
+
The engine dispatches blocks for motion, looks, events, control, sensing,
|
|
47
|
+
operators, variables and lists, custom blocks, pen, and sound timing. Use
|
|
48
|
+
`--vocabulary` to print the implemented opcode list or `--census --sb3 project.sb3`
|
|
49
|
+
to see which opcodes a project uses that the engine cannot run.
|
|
50
|
+
|
|
51
|
+
A dart impact updates the pointer, sets `mouse down?` for one poll, and
|
|
52
|
+
starts the `dart` broadcast and click hats during a run. The pointer keeps
|
|
53
|
+
the most recent landing position. The six panel buttons map to the arrows,
|
|
54
|
+
`a`, and `b`, with Scratch `space` mapped to A.
|
|
55
|
+
|
|
56
|
+
## Limits
|
|
57
|
+
|
|
58
|
+
- The stage and silhouette collisions use the panel’s 128×96 pixel lattice.
|
|
59
|
+
Thin artwork and grazing collisions can differ from Scratch's 480×360 stage.
|
|
60
|
+
- Some graphic effects are approximations. SVG text uses installed fonts, and
|
|
61
|
+
SVG styles and grouped image/text compositing have limited support.
|
|
62
|
+
- Sound blocks track timing and effects. Audible playback requires an audio sink.
|
|
63
|
+
- Stage monitors and `sensing_setdragmode` are unsupported.
|
|
64
|
+
|
|
65
|
+
## Develop
|
|
66
|
+
|
|
67
|
+
```sh
|
|
68
|
+
.venv/bin/python scripts/make_fixture.py # regenerate tests/fixtures/tracer.sb3
|
|
69
|
+
.venv/bin/python -m pytest
|
|
70
|
+
.venv/bin/python scripts/probe_e2e.py # shared-memory probe, with the emulator stopped
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
The control probe generates a separate project and checks that a flag starts
|
|
74
|
+
animation, stop freezes the frame, and a subsequent flag restarts the project.
|
|
75
|
+
Run it with the emulator stopped:
|
|
76
|
+
|
|
77
|
+
```sh
|
|
78
|
+
.venv/bin/python scripts/make_flag_test.py /tmp/dartsnut-flag
|
|
79
|
+
.venv/bin/python scripts/probe_controls.py /tmp/dartsnut-flag/flag-stop-test.sb3
|
|
80
|
+
```
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "pydartsnut-scratch"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Scratch 3 interpreter for the Dartsnut panel"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.12"
|
|
11
|
+
dependencies = [
|
|
12
|
+
"pydartsnut==1.4.0",
|
|
13
|
+
"skia-python",
|
|
14
|
+
"numpy",
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
[project.optional-dependencies]
|
|
18
|
+
dev = ["pytest"]
|
|
19
|
+
|
|
20
|
+
[project.scripts]
|
|
21
|
+
pydartsnut-scratch = "pydartsnut_scratch.__main__:main"
|
|
22
|
+
|
|
23
|
+
[tool.hatch.build.targets.wheel]
|
|
24
|
+
packages = ["src/pydartsnut_scratch"]
|
|
25
|
+
|
|
26
|
+
[tool.pytest.ini_options]
|
|
27
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
"""Generate ``tests/fixtures/tracer.sb3``.
|
|
2
|
+
|
|
3
|
+
The fixture uses only the tracer block subset, so the committed zip doubles as
|
|
4
|
+
the engine's behavioural test input. Assets are written under their md5 names
|
|
5
|
+
with matching ``assetId``, the way Scratch stores them.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import hashlib
|
|
11
|
+
import json
|
|
12
|
+
import zipfile
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
|
|
15
|
+
ROOT = Path(__file__).resolve().parent.parent
|
|
16
|
+
OUT = ROOT / "tests" / "fixtures" / "tracer.sb3"
|
|
17
|
+
|
|
18
|
+
BACKDROP = """<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="480" height="360" viewBox="0 0 480 360">
|
|
19
|
+
<defs>
|
|
20
|
+
<linearGradient id="sky" x1="0" y1="0" x2="0" y2="1">
|
|
21
|
+
<stop offset="0" stop-color="#274f86"/>
|
|
22
|
+
<stop offset="1" stop-color="#9ed4f0"/>
|
|
23
|
+
</linearGradient>
|
|
24
|
+
</defs>
|
|
25
|
+
<rect x="0" y="0" width="480" height="360" fill="url(#sky)"/>
|
|
26
|
+
<rect x="0" y="246" width="480" height="114" fill="#3f7a3f"/>
|
|
27
|
+
<rect x="0" y="242" width="480" height="4" fill="#1e3d1c"/>
|
|
28
|
+
</svg>
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
SHAPE_A = """<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="64" height="64" viewBox="0 0 64 64">
|
|
32
|
+
<polygon points="32,3 61,59 3,59" fill="#ffd23f" stroke="#3b2b00" stroke-width="5"/>
|
|
33
|
+
</svg>
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
SHAPE_B = """<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="64" height="64" viewBox="0 0 64 64">
|
|
37
|
+
<polygon points="32,3 61,32 32,61 3,32" fill="#f26d6d" stroke="#3b0000" stroke-width="5"/>
|
|
38
|
+
</svg>
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _block(opcode, next=None, parent=None, inputs=None, fields=None, top=False, x=0, y=0):
|
|
43
|
+
return {
|
|
44
|
+
"opcode": opcode,
|
|
45
|
+
"next": next,
|
|
46
|
+
"parent": parent,
|
|
47
|
+
"inputs": inputs or {},
|
|
48
|
+
"fields": fields or {},
|
|
49
|
+
"shadow": False,
|
|
50
|
+
"topLevel": top,
|
|
51
|
+
"x": x,
|
|
52
|
+
"y": y,
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _number(value):
|
|
57
|
+
return [1, [4, str(value)]]
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _text(value):
|
|
61
|
+
return [1, [10, str(value)]]
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _costume(name, svg, center_x, center_y):
|
|
65
|
+
data = svg.encode("utf-8")
|
|
66
|
+
md5 = hashlib.md5(data).hexdigest()
|
|
67
|
+
entry = {
|
|
68
|
+
"name": name,
|
|
69
|
+
"dataFormat": "svg",
|
|
70
|
+
"assetId": md5,
|
|
71
|
+
"md5ext": f"{md5}.svg",
|
|
72
|
+
"rotationCenterX": center_x,
|
|
73
|
+
"rotationCenterY": center_y,
|
|
74
|
+
}
|
|
75
|
+
return entry, (f"{md5}.svg", data)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def build() -> tuple[dict, dict[str, bytes]]:
|
|
79
|
+
backdrop, backdrop_asset = _costume("backdrop", BACKDROP, 240, 180)
|
|
80
|
+
shape_a, asset_a = _costume("shape-a", SHAPE_A, 32, 32)
|
|
81
|
+
shape_b, asset_b = _costume("shape-b", SHAPE_B, 32, 32)
|
|
82
|
+
|
|
83
|
+
blocks = {
|
|
84
|
+
# Move 4 steps a tick and bounce off the stage edge.
|
|
85
|
+
"flag": _block("event_whenflagclicked", next="spin", top=True, x=48, y=40),
|
|
86
|
+
"spin": _block("control_forever", parent="flag", inputs={"SUBSTACK": [2, "move"]}),
|
|
87
|
+
"move": _block(
|
|
88
|
+
"motion_movesteps", next="edge_if", parent="spin", inputs={"STEPS": _number(4)}
|
|
89
|
+
),
|
|
90
|
+
"edge_if": _block(
|
|
91
|
+
"control_if",
|
|
92
|
+
parent="move",
|
|
93
|
+
inputs={"CONDITION": [2, "edge"], "SUBSTACK": [2, "turn"]},
|
|
94
|
+
),
|
|
95
|
+
"edge": _block(
|
|
96
|
+
"sensing_touchingobject",
|
|
97
|
+
parent="edge_if",
|
|
98
|
+
inputs={"TOUCHINGOBJECTMENU": [1, [8, "_edge_"]]},
|
|
99
|
+
),
|
|
100
|
+
"turn": _block("motion_turnright", parent="edge_if", inputs={"DEGREES": _number(180)}),
|
|
101
|
+
# The engine broadcasts ``dart`` for each dart landing.
|
|
102
|
+
"receive": _block(
|
|
103
|
+
"event_whenbroadcastreceived",
|
|
104
|
+
next="score_up",
|
|
105
|
+
fields={"BROADCAST_OPTION": ["dart", "bmsg"]},
|
|
106
|
+
top=True,
|
|
107
|
+
x=48,
|
|
108
|
+
y=300,
|
|
109
|
+
),
|
|
110
|
+
"score_up": _block(
|
|
111
|
+
"data_changevariableby",
|
|
112
|
+
parent="receive",
|
|
113
|
+
fields={"VARIABLE": ["score", "score_id"]},
|
|
114
|
+
inputs={"VALUE": _number(1)},
|
|
115
|
+
),
|
|
116
|
+
# Buttons cycle the costume.
|
|
117
|
+
"left_hat": _block(
|
|
118
|
+
"event_whenkeypressed",
|
|
119
|
+
next="left_next",
|
|
120
|
+
fields={"KEY_OPTION": ["left arrow", None]},
|
|
121
|
+
top=True,
|
|
122
|
+
x=420,
|
|
123
|
+
y=40,
|
|
124
|
+
),
|
|
125
|
+
"left_next": _block("looks_nextcostume", parent="left_hat"),
|
|
126
|
+
"right_hat": _block(
|
|
127
|
+
"event_whenkeypressed",
|
|
128
|
+
next="right_set",
|
|
129
|
+
fields={"KEY_OPTION": ["right arrow", None]},
|
|
130
|
+
top=True,
|
|
131
|
+
x=420,
|
|
132
|
+
y=140,
|
|
133
|
+
),
|
|
134
|
+
"right_set": _block(
|
|
135
|
+
"looks_switchcostumeto", parent="right_hat", inputs={"COSTUME": _text("shape-a")}
|
|
136
|
+
),
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
stage = {
|
|
140
|
+
"isStage": True,
|
|
141
|
+
"name": "Stage",
|
|
142
|
+
"variables": {"score_id": ["score", 0]},
|
|
143
|
+
"lists": {},
|
|
144
|
+
"broadcasts": {"bmsg": "dart"},
|
|
145
|
+
"blocks": {},
|
|
146
|
+
"comments": {},
|
|
147
|
+
"currentCostume": 0,
|
|
148
|
+
"costumes": [backdrop],
|
|
149
|
+
"sounds": [],
|
|
150
|
+
"volume": 100,
|
|
151
|
+
"layerOrder": 0,
|
|
152
|
+
"tempo": 60,
|
|
153
|
+
"videoTransparency": 50,
|
|
154
|
+
"videoState": "off",
|
|
155
|
+
"textToSpeechLanguage": None,
|
|
156
|
+
}
|
|
157
|
+
sprite = {
|
|
158
|
+
"isStage": False,
|
|
159
|
+
"name": "Tracer",
|
|
160
|
+
"variables": {},
|
|
161
|
+
"lists": {},
|
|
162
|
+
"broadcasts": {},
|
|
163
|
+
"blocks": blocks,
|
|
164
|
+
"comments": {},
|
|
165
|
+
"currentCostume": 0,
|
|
166
|
+
"costumes": [shape_a, shape_b],
|
|
167
|
+
"sounds": [],
|
|
168
|
+
"volume": 100,
|
|
169
|
+
"layerOrder": 1,
|
|
170
|
+
"visible": True,
|
|
171
|
+
"x": 0,
|
|
172
|
+
"y": 0,
|
|
173
|
+
"size": 100,
|
|
174
|
+
"direction": 90,
|
|
175
|
+
"draggable": False,
|
|
176
|
+
"rotationStyle": "all around",
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
project = {
|
|
180
|
+
"targets": [stage, sprite],
|
|
181
|
+
"monitors": [],
|
|
182
|
+
"extensions": [],
|
|
183
|
+
"meta": {"semver": "3.0.0", "vm": "4.2.0", "agent": "pydartsnut-scratch fixture generator"},
|
|
184
|
+
}
|
|
185
|
+
assets = dict([backdrop_asset, asset_a, asset_b])
|
|
186
|
+
return project, assets
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def main() -> None:
|
|
190
|
+
project, assets = build()
|
|
191
|
+
OUT.parent.mkdir(parents=True, exist_ok=True)
|
|
192
|
+
payload = {"project.json": json.dumps(project, indent=2, sort_keys=True).encode("utf-8"), **assets}
|
|
193
|
+
with zipfile.ZipFile(OUT, "w", zipfile.ZIP_DEFLATED) as archive:
|
|
194
|
+
for name, data in payload.items():
|
|
195
|
+
info = zipfile.ZipInfo(name, date_time=(2026, 1, 1, 0, 0, 0))
|
|
196
|
+
info.compress_type = zipfile.ZIP_DEFLATED
|
|
197
|
+
archive.writestr(info, data)
|
|
198
|
+
print(f"wrote {OUT} ({OUT.stat().st_size} bytes, {len(assets)} assets)")
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
if __name__ == "__main__":
|
|
202
|
+
main()
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
"""Generate the flag/stop verification project.
|
|
2
|
+
|
|
3
|
+
A project whose two controls are impossible to confuse: the flag sends one
|
|
4
|
+
sprite left to right while it paints a trail, and a second sprite counts the
|
|
5
|
+
seconds it has been running so a frozen frame is obvious at a glance. Stop must
|
|
6
|
+
freeze both; flag must send them back to the start line.
|
|
7
|
+
|
|
8
|
+
Usage::
|
|
9
|
+
|
|
10
|
+
.venv/bin/python scripts/make_flag_test.py ~/dev/dartsnut/scratch-demo
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
import hashlib
|
|
16
|
+
import json
|
|
17
|
+
import sys
|
|
18
|
+
import zipfile
|
|
19
|
+
from pathlib import Path
|
|
20
|
+
|
|
21
|
+
BACKDROP = """<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="480" height="360" viewBox="0 0 480 360">
|
|
22
|
+
<rect x="0" y="0" width="480" height="360" fill="#101a2b"/>
|
|
23
|
+
<rect x="0" y="0" width="480" height="8" fill="#2b3d5c"/>
|
|
24
|
+
<rect x="0" y="352" width="480" height="8" fill="#2b3d5c"/>
|
|
25
|
+
<rect x="236" y="0" width="8" height="360" fill="#2b3d5c"/>
|
|
26
|
+
</svg>
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
RUNNER = """<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="24" height="24" viewBox="0 0 24 24">
|
|
30
|
+
<circle cx="12" cy="12" r="10" fill="#ffd23f" stroke="#3b2b00" stroke-width="2"/>
|
|
31
|
+
</svg>
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
CLOCK = """<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="32" height="32" viewBox="0 0 32 32">
|
|
35
|
+
<rect x="2" y="2" width="28" height="28" rx="4" fill="#4fd2a2" stroke="#06301f" stroke-width="2"/>
|
|
36
|
+
</svg>
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def asset(name: str, data: bytes) -> dict:
|
|
41
|
+
digest = hashlib.md5(data).hexdigest()
|
|
42
|
+
suffix = Path(name).suffix
|
|
43
|
+
return {
|
|
44
|
+
"name": name,
|
|
45
|
+
"md5ext": f"{digest}{suffix}",
|
|
46
|
+
"assetId": digest,
|
|
47
|
+
"data": data,
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def block(opcode, next_id=None, parent=None, inputs=None, fields=None, top=False, x=0, y=0):
|
|
52
|
+
return {
|
|
53
|
+
"opcode": opcode,
|
|
54
|
+
"next": next_id,
|
|
55
|
+
"parent": parent,
|
|
56
|
+
"inputs": inputs or {},
|
|
57
|
+
"fields": fields or {},
|
|
58
|
+
"shadow": False,
|
|
59
|
+
"topLevel": top,
|
|
60
|
+
"x": x,
|
|
61
|
+
"y": y,
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def literal(value: str) -> list:
|
|
66
|
+
return [1, [10, str(value)]]
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def number(value: str) -> list:
|
|
70
|
+
return [1, [4, str(value)]]
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def backdrops() -> list:
|
|
74
|
+
return [asset("dark.svg", BACKDROP.encode())]
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def stage() -> dict:
|
|
78
|
+
return {
|
|
79
|
+
"isStage": True,
|
|
80
|
+
"name": "Stage",
|
|
81
|
+
"variables": {},
|
|
82
|
+
"lists": {},
|
|
83
|
+
"broadcasts": {},
|
|
84
|
+
"blocks": {},
|
|
85
|
+
"comments": {},
|
|
86
|
+
"currentCostume": 0,
|
|
87
|
+
"costumes": [
|
|
88
|
+
{
|
|
89
|
+
"name": "dark",
|
|
90
|
+
"md5ext": backdrops()[0]["md5ext"],
|
|
91
|
+
"assetId": backdrops()[0]["assetId"],
|
|
92
|
+
"rotationCenterX": 240,
|
|
93
|
+
"rotationCenterY": 180,
|
|
94
|
+
}
|
|
95
|
+
],
|
|
96
|
+
"sounds": [],
|
|
97
|
+
"volume": 100,
|
|
98
|
+
"layerOrder": 0,
|
|
99
|
+
"tempo": 60,
|
|
100
|
+
"videoTransparency": 50,
|
|
101
|
+
"videoState": "off",
|
|
102
|
+
"textToSpeechLanguage": None,
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def runner() -> dict:
|
|
107
|
+
ball = asset("ball.svg", RUNNER.encode())
|
|
108
|
+
blocks = {
|
|
109
|
+
"hat": block(
|
|
110
|
+
"event_whenflagclicked",
|
|
111
|
+
next_id="go_home",
|
|
112
|
+
top=True,
|
|
113
|
+
x=40,
|
|
114
|
+
y=40,
|
|
115
|
+
),
|
|
116
|
+
"go_home": block(
|
|
117
|
+
"motion_gotoxy",
|
|
118
|
+
next_id="pen_clear",
|
|
119
|
+
parent="hat",
|
|
120
|
+
inputs={"X": number("-100"), "Y": number("0")},
|
|
121
|
+
),
|
|
122
|
+
"pen_clear": block("pen_clear", next_id="pen_size", parent="go_home"),
|
|
123
|
+
"pen_size": block(
|
|
124
|
+
"pen_setPenSizeTo",
|
|
125
|
+
next_id="pen_down",
|
|
126
|
+
parent="pen_clear",
|
|
127
|
+
inputs={"SIZE": number("3")},
|
|
128
|
+
),
|
|
129
|
+
"pen_down": block("pen_penDown", next_id="forever", parent="pen_size"),
|
|
130
|
+
"forever": block(
|
|
131
|
+
"control_forever",
|
|
132
|
+
inputs={"SUBSTACK": [2, "step"]},
|
|
133
|
+
parent="pen_down",
|
|
134
|
+
),
|
|
135
|
+
"step": block(
|
|
136
|
+
"motion_changexby",
|
|
137
|
+
next_id="bounce",
|
|
138
|
+
parent="forever",
|
|
139
|
+
inputs={"DX": number("3")},
|
|
140
|
+
),
|
|
141
|
+
"bounce": block("motion_ifonedgebounce", parent="step"),
|
|
142
|
+
}
|
|
143
|
+
return {
|
|
144
|
+
"isStage": False,
|
|
145
|
+
"name": "Runner",
|
|
146
|
+
"variables": {},
|
|
147
|
+
"lists": {},
|
|
148
|
+
"broadcasts": {},
|
|
149
|
+
"blocks": blocks,
|
|
150
|
+
"comments": {},
|
|
151
|
+
"currentCostume": 0,
|
|
152
|
+
"costumes": [
|
|
153
|
+
{
|
|
154
|
+
"name": "ball",
|
|
155
|
+
"md5ext": ball["md5ext"],
|
|
156
|
+
"assetId": ball["assetId"],
|
|
157
|
+
"rotationCenterX": 12,
|
|
158
|
+
"rotationCenterY": 12,
|
|
159
|
+
}
|
|
160
|
+
],
|
|
161
|
+
"sounds": [],
|
|
162
|
+
"volume": 100,
|
|
163
|
+
"layerOrder": 1,
|
|
164
|
+
"visible": True,
|
|
165
|
+
"x": -100,
|
|
166
|
+
"y": 0,
|
|
167
|
+
"size": 100,
|
|
168
|
+
"direction": 90,
|
|
169
|
+
"draggable": False,
|
|
170
|
+
"rotationStyle": "all around",
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def clock() -> dict:
|
|
175
|
+
sprite = asset("clock.svg", CLOCK.encode())
|
|
176
|
+
blocks = {
|
|
177
|
+
"hat": block("event_whenflagclicked", next_id="reset", top=True, x=340, y=40),
|
|
178
|
+
"reset": block(
|
|
179
|
+
"data_setvariableto",
|
|
180
|
+
next_id="forever",
|
|
181
|
+
parent="hat",
|
|
182
|
+
inputs={"VALUE": number("0")},
|
|
183
|
+
fields={"VARIABLE": ["seconds", "seconds"]},
|
|
184
|
+
),
|
|
185
|
+
"forever": block("control_forever", inputs={"SUBSTACK": [2, "tick"]}, parent="reset"),
|
|
186
|
+
"tick": block(
|
|
187
|
+
"control_wait",
|
|
188
|
+
next_id="bump",
|
|
189
|
+
parent="forever",
|
|
190
|
+
inputs={"DURATION": number("1")},
|
|
191
|
+
),
|
|
192
|
+
"bump": block(
|
|
193
|
+
"data_changevariableby",
|
|
194
|
+
next_id="show",
|
|
195
|
+
parent="tick",
|
|
196
|
+
inputs={"VALUE": number("1")},
|
|
197
|
+
fields={"VARIABLE": ["seconds", "seconds"]},
|
|
198
|
+
),
|
|
199
|
+
"show": block(
|
|
200
|
+
"looks_say",
|
|
201
|
+
next_id="turn",
|
|
202
|
+
parent="bump",
|
|
203
|
+
inputs={"MESSAGE": [3, [12, "seconds", "seconds"], [10, ""]]},
|
|
204
|
+
),
|
|
205
|
+
"turn": block(
|
|
206
|
+
"motion_pointindirection",
|
|
207
|
+
parent="show",
|
|
208
|
+
inputs={"DIRECTION": [3, "spin", [4, "90"]]},
|
|
209
|
+
),
|
|
210
|
+
"spin": {
|
|
211
|
+
"opcode": "operator_multiply",
|
|
212
|
+
"next": None,
|
|
213
|
+
"parent": "turn",
|
|
214
|
+
"inputs": {
|
|
215
|
+
"NUM1": [3, [12, "seconds", "seconds"], [4, "0"]],
|
|
216
|
+
"NUM2": [1, [4, "30"]],
|
|
217
|
+
},
|
|
218
|
+
"fields": {},
|
|
219
|
+
"shadow": False,
|
|
220
|
+
"topLevel": False,
|
|
221
|
+
},
|
|
222
|
+
}
|
|
223
|
+
return {
|
|
224
|
+
"isStage": False,
|
|
225
|
+
"name": "Clock",
|
|
226
|
+
"variables": {"seconds": ["seconds", 0]},
|
|
227
|
+
"lists": {},
|
|
228
|
+
"broadcasts": {},
|
|
229
|
+
"blocks": blocks,
|
|
230
|
+
"comments": {},
|
|
231
|
+
"currentCostume": 0,
|
|
232
|
+
"costumes": [
|
|
233
|
+
{
|
|
234
|
+
"name": "clock",
|
|
235
|
+
"md5ext": sprite["md5ext"],
|
|
236
|
+
"assetId": sprite["assetId"],
|
|
237
|
+
"rotationCenterX": 16,
|
|
238
|
+
"rotationCenterY": 16,
|
|
239
|
+
}
|
|
240
|
+
],
|
|
241
|
+
"sounds": [],
|
|
242
|
+
"volume": 100,
|
|
243
|
+
"layerOrder": 2,
|
|
244
|
+
"visible": True,
|
|
245
|
+
"x": 150,
|
|
246
|
+
"y": 60,
|
|
247
|
+
"size": 250,
|
|
248
|
+
"direction": 90,
|
|
249
|
+
"draggable": False,
|
|
250
|
+
"rotationStyle": "all around",
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def build() -> tuple[dict, list[dict]]:
|
|
255
|
+
assets = [asset("dark.svg", BACKDROP.encode()),
|
|
256
|
+
asset("ball.svg", RUNNER.encode()),
|
|
257
|
+
asset("clock.svg", CLOCK.encode())]
|
|
258
|
+
project = {
|
|
259
|
+
"targets": [stage(), runner(), clock()],
|
|
260
|
+
"monitors": [],
|
|
261
|
+
"extensions": ["pen"],
|
|
262
|
+
"meta": {"semver": "3.0.0", "vm": "11.1.0", "agent": "dartsnut flag test"},
|
|
263
|
+
}
|
|
264
|
+
return project, assets
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
def main(argv: list[str]) -> int:
|
|
268
|
+
if len(argv) != 2:
|
|
269
|
+
print(__doc__)
|
|
270
|
+
return 2
|
|
271
|
+
target = Path(argv[1]).expanduser()
|
|
272
|
+
target.mkdir(parents=True, exist_ok=True)
|
|
273
|
+
project, assets = build()
|
|
274
|
+
out = target / "flag-stop-test.sb3"
|
|
275
|
+
with zipfile.ZipFile(out, "w", zipfile.ZIP_DEFLATED) as archive:
|
|
276
|
+
archive.writestr("project.json", json.dumps(project))
|
|
277
|
+
for entry in assets:
|
|
278
|
+
archive.writestr(entry["md5ext"], entry["data"])
|
|
279
|
+
print(out)
|
|
280
|
+
return 0
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
if __name__ == "__main__":
|
|
284
|
+
raise SystemExit(main(sys.argv))
|