Skip to content

Commit c96a427

Browse files
committed
Add repeat block
1 parent 5d110d7 commit c96a427

File tree

9 files changed

+37
-5
lines changed

9 files changed

+37
-5
lines changed
Lines changed: 1 addition & 0 deletions
Loading
560 Bytes
Binary file not shown.

assets/9a4fb3ed82f74f5df24ade1a2aa48920.svg

Lines changed: 0 additions & 1 deletion
This file was deleted.

assets/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"targets":[{"isStage":true,"name":"Stage","variables":{},"lists":{},"broadcasts":{},"blocks":{},"comments":{},"currentCostume":0,"costumes":[{"assetId":"cd21514d0531fdffb22204e0ec5ed84a","name":"backdrop1","md5ext":"cd21514d0531fdffb22204e0ec5ed84a.svg","dataFormat":"svg","rotationCenterX":240,"rotationCenterY":180}],"sounds":[],"volume":100,"layerOrder":0,"tempo":60,"videoTransparency":50,"videoState":"on","textToSpeechLanguage":"ro"},{"isStage":false,"name":"Cosmicat","variables":{},"lists":{},"broadcasts":{},"blocks":{"E;=y4~YoyP#A+s:jS0xd":{"opcode":"event_whenflagclicked","next":"*`q9Brc}_w8S^^Qi}rB3","parent":null,"inputs":{},"fields":{},"shadow":false,"topLevel":true,"x":120,"y":312},"*`q9Brc}_w8S^^Qi}rB3":{"opcode":"motion_gotoxy","next":"G!%/o$v|,%8mjWc{:#C,","parent":"E;=y4~YoyP#A+s:jS0xd","inputs":{"X":[1,[4,"0"]],"Y":[1,[4,"0"]]},"fields":{},"shadow":false,"topLevel":false},"G!%/o$v|,%8mjWc{:#C,":{"opcode":"control_wait","next":"jctW5vM^0j?OBU[c_QIX","parent":"*`q9Brc}_w8S^^Qi}rB3","inputs":{"DURATION":[1,[5,"1"]]},"fields":{},"shadow":false,"topLevel":false},"^PPAV3[UEMRuP%`Fky,@":{"opcode":"operator_random","next":null,"parent":"jctW5vM^0j?OBU[c_QIX","inputs":{"FROM":[1,[4,"-240"]],"TO":[1,[4,"240"]]},"fields":{},"shadow":false,"topLevel":false},"jctW5vM^0j?OBU[c_QIX":{"opcode":"motion_setx","next":null,"parent":"G!%/o$v|,%8mjWc{:#C,","inputs":{"X":[3,"^PPAV3[UEMRuP%`Fky,@",[4,"10"]]},"fields":{},"shadow":false,"topLevel":false}},"comments":{},"currentCostume":0,"costumes":[{"assetId":"9a4fb3ed82f74f5df24ade1a2aa48920","name":"Cosmicat","bitmapResolution":1,"md5ext":"9a4fb3ed82f74f5df24ade1a2aa48920.svg","dataFormat":"svg","rotationCenterX":47.2514000162754,"rotationCenterY":57.860045289335886}],"sounds":[],"volume":100,"layerOrder":1,"visible":true,"x":0,"y":0,"size":100,"direction":90,"draggable":false,"rotationStyle":"all around"}],"monitors":[],"extensions":[],"meta":{"semver":"3.0.0","vm":"0.2.0-prerelease.20220302060126","agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36"}}
1+
{"targets":[{"isStage":true,"name":"Stage","variables":{},"lists":{},"broadcasts":{},"blocks":{},"comments":{},"currentCostume":0,"costumes":[{"assetId":"cd21514d0531fdffb22204e0ec5ed84a","name":"backdrop1","md5ext":"cd21514d0531fdffb22204e0ec5ed84a.svg","dataFormat":"svg","rotationCenterX":240,"rotationCenterY":180}],"sounds":[],"volume":100,"layerOrder":0,"tempo":60,"videoTransparency":50,"videoState":"on","textToSpeechLanguage":"ro"},{"isStage":false,"name":"Balloon1","variables":{},"lists":{},"broadcasts":{},"blocks":{"]p0WM]/6Ve;iPTfO~ov:":{"opcode":"event_whenkeypressed","next":"czG|E7P$QI*{rA2iUC-w","parent":null,"inputs":{},"fields":{"KEY_OPTION":["space",null]},"shadow":false,"topLevel":true,"x":48,"y":208},"czG|E7P$QI*{rA2iUC-w":{"opcode":"control_repeat","next":null,"parent":"]p0WM]/6Ve;iPTfO~ov:","inputs":{"TIMES":[1,[6,"10"]],"SUBSTACK":[2,"fMb(Tc_!lhzbgDNihKSq"]},"fields":{},"shadow":false,"topLevel":false},"fMb(Tc_!lhzbgDNihKSq":{"opcode":"motion_changeyby","next":null,"parent":"czG|E7P$QI*{rA2iUC-w","inputs":{"DY":[1,[4,"5"]]},"fields":{},"shadow":false,"topLevel":false},"XDeo_FHmn,lz+e0E9lVL":{"opcode":"event_whenflagclicked","next":"mgHB88^RU,vL)z1QeKRr","parent":null,"inputs":{},"fields":{},"shadow":false,"topLevel":true,"x":48,"y":64},"mgHB88^RU,vL)z1QeKRr":{"opcode":"motion_gotoxy","next":null,"parent":"XDeo_FHmn,lz+e0E9lVL","inputs":{"X":[1,[4,"0"]],"Y":[1,[4,"0"]]},"fields":{},"shadow":false,"topLevel":false}},"comments":{},"currentCostume":0,"costumes":[{"assetId":"3dc0568c0414f091db6da7b90bcc2a64","name":"balloon1-a","bitmapResolution":1,"md5ext":"3dc0568c0414f091db6da7b90bcc2a64.svg","dataFormat":"svg","rotationCenterX":37.8470329351303,"rotationCenterY":43.83676071798408}],"sounds":[{"assetId":"83a9787d4cb6f3b7632b4ddfebf74367","name":"Pop","dataFormat":"wav","format":"","rate":44100,"sampleCount":1032,"md5ext":"83a9787d4cb6f3b7632b4ddfebf74367.wav"}],"volume":100,"layerOrder":1,"visible":true,"x":0,"y":0,"size":100,"direction":90,"draggable":false,"rotationStyle":"all around"}],"monitors":[],"extensions":[],"meta":{"semver":"3.0.0","vm":"0.2.0-prerelease.20220308092939","agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36"}}

block.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def __init__(self):
3838
self.screenRefresh = False # do a screen refresh
3939
self.inEventLoop = False
4040
self.value = None # reported value
41+
self.repeatCounter = None # for repeat block
4142

4243
# Evaluates block value (for reporters)
4344
def evaluateBlockValue(self):
@@ -72,7 +73,7 @@ def getBlockInputValue(self, inputId):
7273

7374
# Returns block input value
7475
def getInputValue(self, inputId, lookIn=(1, 1)):
75-
if self.inputs[inputId.upper()][lookIn[0]][0] in {4, 0, 5}:
76+
if self.inputs[inputId.upper()][lookIn[0]][0] in {4, 0, 5, 6}:
7677
return self.inputs[inputId.upper()][lookIn[0]][1] or 0
7778
elif self.inputs[inputId.upper()][0] == 3:
7879
blockLink = self.inputs[inputId.upper()][1]

config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
# Project file name
3131
# If the "manual" mode is chosen, set the Scratch project file to load.
32-
projectFileName: str = "projects/Math5.sb3"
32+
projectFileName: str = "projects/balloon.sb3"
3333

3434
# Extract on project run
3535
# Set whether to extract the project assets on run.

projects/Untitled-122.sb3

49.4 KB
Binary file not shown.

projects/balloon.sb3

3.4 KB
Binary file not shown.

scratch.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,6 @@ def execute(block, s, keys=[]):
264264
nb = s.target.blocks[inputs["SUBSTACK"][1]]
265265
block.substack.add(nb.blockID)
266266
while nb.next and nb.next != block.blockID:
267-
# TODO: Caution: Don't loop the program
268267
nb.blockRan = False
269268
nb.waiting = False
270269
nb.timeDelay = 0
@@ -273,6 +272,38 @@ def execute(block, s, keys=[]):
273272
block.substack.add(nb.blockID)
274273
nb.next = block.blockID
275274
return nextBlock
275+
276+
elif opcode == "control_repeat": # repeat (10) {..}
277+
if block.repeatCounter is None:
278+
block.repeatCounter = int(block.getInputValue("times"))
279+
# Don't mark the loop as ran until done, and do a screen refresh
280+
if block.repeatCounter > 0:
281+
block.blockRan = False
282+
else:
283+
block.blockRan = True
284+
block.repeatCounter = None
285+
block.screenRefresh = True
286+
287+
# If there are blocks, get them
288+
if inputs["SUBSTACK"][1]:
289+
# No blocks will be flagged as ran inside a forever loop
290+
for b in block.substack:
291+
s.target.blocks[b].blockRan = False
292+
nextBlock = s.target.blocks[inputs["SUBSTACK"][1]]
293+
nb = s.target.blocks[inputs["SUBSTACK"][1]]
294+
block.substack.add(nb.blockID)
295+
while nb.next and nb.next != block.blockID:
296+
nb.blockRan = False
297+
nb.waiting = False
298+
nb.timeDelay = 0
299+
nb.executionTime = 0
300+
nb = s.target.blocks[nb.next]
301+
block.substack.add(nb.blockID)
302+
nb.next = block.blockID
303+
if block.repeatCounter is not None:
304+
block.repeatCounter -= 1
305+
return nextBlock
306+
276307
elif opcode == "procedures_call":
277308
if config.showSALogs:
278309
if block.proccode == "​​log​​ %s": # Scratch Addons log ()

0 commit comments

Comments
 (0)