Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "if ... then else ..." block support #51

Merged
merged 2 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assets/project.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"targets":[{"isStage":true,"name":"Stage","variables":{"`jEk@4|i[#Fk?(8x)AV.-my variable":["variabila mea",0]},"lists":{},"broadcasts":{},"blocks":{},"comments":{},"currentCostume":0,"costumes":[{"name":"decor1","dataFormat":"svg","assetId":"cd21514d0531fdffb22204e0ec5ed84a","md5ext":"cd21514d0531fdffb22204e0ec5ed84a.svg","rotationCenterX":240,"rotationCenterY":180}],"sounds":[{"name":"pop","assetId":"83a9787d4cb6f3b7632b4ddfebf74367","dataFormat":"wav","format":"","rate":44100,"sampleCount":1032,"md5ext":"83a9787d4cb6f3b7632b4ddfebf74367.wav"}],"volume":100,"layerOrder":0,"tempo":60,"videoTransparency":50,"videoState":"on","textToSpeechLanguage":"ro"},{"isStage":false,"name":"Personaj1","variables":{},"lists":{},"broadcasts":{},"blocks":{"i0sF-x|[yQ-#%j@rU%f,":{"opcode":"event_whenflagclicked","next":")u+B7H(M9g25%Em.w=5;","parent":null,"inputs":{},"fields":{},"shadow":false,"topLevel":true,"x":360,"y":168},")u+B7H(M9g25%Em.w=5;":{"opcode":"control_forever","next":null,"parent":"i0sF-x|[yQ-#%j@rU%f,","inputs":{"SUBSTACK":[2,"/B0dn4qYH?K,.K9rc;A`"]},"fields":{},"shadow":false,"topLevel":false},"/B0dn4qYH?K,.K9rc;A`":{"opcode":"control_if","next":"|LMd9D?RO[2$-2d9tSaH","parent":")u+B7H(M9g25%Em.w=5;","inputs":{"CONDITION":[2,"#Uk8A~y8%JVMqn}NHx[`"],"SUBSTACK":[2,"oh,d/q]c)nT*MAUu,[S@"]},"fields":{},"shadow":false,"topLevel":false},"#Uk8A~y8%JVMqn}NHx[`":{"opcode":"sensing_keypressed","next":null,"parent":"/B0dn4qYH?K,.K9rc;A`","inputs":{"KEY_OPTION":[1,"DNHO5a8nfKM+34^x:43}"]},"fields":{},"shadow":false,"topLevel":false},"DNHO5a8nfKM+34^x:43}":{"opcode":"sensing_keyoptions","next":null,"parent":"#Uk8A~y8%JVMqn}NHx[`","inputs":{},"fields":{"KEY_OPTION":["space",null]},"shadow":true,"topLevel":false},"oh,d/q]c)nT*MAUu,[S@":{"opcode":"looks_switchcostumeto","next":null,"parent":"/B0dn4qYH?K,.K9rc;A`","inputs":{"COSTUME":[1,"R^t{wX)}j(l,vXrOTX(g"]},"fields":{},"shadow":false,"topLevel":false},"R^t{wX)}j(l,vXrOTX(g":{"opcode":"looks_costume","next":null,"parent":"oh,d/q]c)nT*MAUu,[S@","inputs":{},"fields":{"COSTUME":["costum2",null]},"shadow":true,"topLevel":false},"|LMd9D?RO[2$-2d9tSaH":{"opcode":"control_if","next":null,"parent":"/B0dn4qYH?K,.K9rc;A`","inputs":{"CONDITION":[2,"46_hm$E}C?N}KA=/3@`u"],"SUBSTACK":[2,"[+(oxNIM5(qM,kq~)/=K"]},"fields":{},"shadow":false,"topLevel":false},"w!`lxu[+VTbb!a@:2IFM":{"opcode":"sensing_keypressed","next":null,"parent":"46_hm$E}C?N}KA=/3@`u","inputs":{"KEY_OPTION":[1,"@`Y@ky)Etn+{XJ-{4Vsb"]},"fields":{},"shadow":false,"topLevel":false},"@`Y@ky)Etn+{XJ-{4Vsb":{"opcode":"sensing_keyoptions","next":null,"parent":"w!`lxu[+VTbb!a@:2IFM","inputs":{},"fields":{"KEY_OPTION":["space",null]},"shadow":true,"topLevel":false},"[+(oxNIM5(qM,kq~)/=K":{"opcode":"looks_switchcostumeto","next":null,"parent":"|LMd9D?RO[2$-2d9tSaH","inputs":{"COSTUME":[1,"laZz,h-Z/Z?46wV,!;C*"]},"fields":{},"shadow":false,"topLevel":false},"laZz,h-Z/Z?46wV,!;C*":{"opcode":"looks_costume","next":null,"parent":"[+(oxNIM5(qM,kq~)/=K","inputs":{},"fields":{"COSTUME":["costum1",null]},"shadow":true,"topLevel":false},"46_hm$E}C?N}KA=/3@`u":{"opcode":"operator_not","next":null,"parent":"|LMd9D?RO[2$-2d9tSaH","inputs":{"OPERAND":[2,"w!`lxu[+VTbb!a@:2IFM"]},"fields":{},"shadow":false,"topLevel":false}},"comments":{},"currentCostume":0,"costumes":[{"name":"costum1","bitmapResolution":1,"dataFormat":"svg","assetId":"5b8a85774c8c983b993b7d1549b46082","md5ext":"5b8a85774c8c983b993b7d1549b46082.svg","rotationCenterX":47.678985050489445,"rotationCenterY":49.499230353205405},{"name":"costum2","bitmapResolution":1,"dataFormat":"svg","assetId":"1bf03016126b8879eb6228066a9a28a2","md5ext":"1bf03016126b8879eb6228066a9a28a2.svg","rotationCenterX":47.67898252524472,"rotationCenterY":49.49923017660271}],"sounds":[{"name":"Miau","assetId":"83c36d806dc92327b9e7049a565c6bff","dataFormat":"wav","format":"","rate":44100,"sampleCount":37376,"md5ext":"83c36d806dc92327b9e7049a565c6bff.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":"1.2.52","agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36"}}
{"targets":[{"isStage":true,"name":"Stage","variables":{"`jEk@4|i[#Fk?(8x)AV.-my variable":["variabila mea",0]},"lists":{},"broadcasts":{},"blocks":{},"comments":{},"currentCostume":0,"costumes":[{"name":"decor1","dataFormat":"svg","assetId":"cd21514d0531fdffb22204e0ec5ed84a","md5ext":"cd21514d0531fdffb22204e0ec5ed84a.svg","rotationCenterX":240,"rotationCenterY":180}],"sounds":[{"name":"pop","assetId":"83a9787d4cb6f3b7632b4ddfebf74367","dataFormat":"wav","format":"","rate":44100,"sampleCount":1032,"md5ext":"83a9787d4cb6f3b7632b4ddfebf74367.wav"}],"volume":100,"layerOrder":0,"tempo":60,"videoTransparency":50,"videoState":"on","textToSpeechLanguage":"ro"},{"isStage":false,"name":"Personaj1","variables":{},"lists":{},"broadcasts":{},"blocks":{"i0sF-x|[yQ-#%j@rU%f,":{"opcode":"event_whenflagclicked","next":")u+B7H(M9g25%Em.w=5;","parent":null,"inputs":{},"fields":{},"shadow":false,"topLevel":true,"x":360,"y":168},")u+B7H(M9g25%Em.w=5;":{"opcode":"control_forever","next":null,"parent":"i0sF-x|[yQ-#%j@rU%f,","inputs":{"SUBSTACK":[2,"|LMd9D?RO[2$-2d9tSaH"]},"fields":{},"shadow":false,"topLevel":false},"|LMd9D?RO[2$-2d9tSaH":{"opcode":"control_if_else","next":"a,Odr3Ov}z^5Jf(!Hu`y","parent":")u+B7H(M9g25%Em.w=5;","inputs":{"CONDITION":[2,"w!`lxu[+VTbb!a@:2IFM"],"SUBSTACK":[2,"[+(oxNIM5(qM,kq~)/=K"],"SUBSTACK2":[2,"8|07x?GC!6r+k@Gn?Ipk"]},"fields":{},"shadow":false,"topLevel":false},"w!`lxu[+VTbb!a@:2IFM":{"opcode":"sensing_keypressed","next":null,"parent":"|LMd9D?RO[2$-2d9tSaH","inputs":{"KEY_OPTION":[1,"@`Y@ky)Etn+{XJ-{4Vsb"]},"fields":{},"shadow":false,"topLevel":false},"@`Y@ky)Etn+{XJ-{4Vsb":{"opcode":"sensing_keyoptions","next":null,"parent":"w!`lxu[+VTbb!a@:2IFM","inputs":{},"fields":{"KEY_OPTION":["space",null]},"shadow":true,"topLevel":false},"[+(oxNIM5(qM,kq~)/=K":{"opcode":"looks_switchcostumeto","next":null,"parent":"|LMd9D?RO[2$-2d9tSaH","inputs":{"COSTUME":[1,"laZz,h-Z/Z?46wV,!;C*"]},"fields":{},"shadow":false,"topLevel":false},"laZz,h-Z/Z?46wV,!;C*":{"opcode":"looks_costume","next":null,"parent":"[+(oxNIM5(qM,kq~)/=K","inputs":{},"fields":{"COSTUME":["costum2",null]},"shadow":true,"topLevel":false},"8|07x?GC!6r+k@Gn?Ipk":{"opcode":"looks_switchcostumeto","next":null,"parent":"|LMd9D?RO[2$-2d9tSaH","inputs":{"COSTUME":[1,"AVNA2_i-lp8Nh=W*Fvdj"]},"fields":{},"shadow":false,"topLevel":false},"AVNA2_i-lp8Nh=W*Fvdj":{"opcode":"looks_costume","next":null,"parent":"8|07x?GC!6r+k@Gn?Ipk","inputs":{},"fields":{"COSTUME":["costum1",null]},"shadow":true,"topLevel":false},"a,Odr3Ov}z^5Jf(!Hu`y":{"opcode":"control_wait","next":null,"parent":"|LMd9D?RO[2$-2d9tSaH","inputs":{"DURATION":[1,[5,"1"]]},"fields":{},"shadow":false,"topLevel":false}},"comments":{},"currentCostume":0,"costumes":[{"name":"costum1","bitmapResolution":1,"dataFormat":"svg","assetId":"5b8a85774c8c983b993b7d1549b46082","md5ext":"5b8a85774c8c983b993b7d1549b46082.svg","rotationCenterX":47.678985050489445,"rotationCenterY":49.499230353205405},{"name":"costum2","bitmapResolution":1,"dataFormat":"svg","assetId":"1bf03016126b8879eb6228066a9a28a2","md5ext":"1bf03016126b8879eb6228066a9a28a2.svg","rotationCenterX":47.67898252524472,"rotationCenterY":49.49923017660271}],"sounds":[{"name":"Miau","assetId":"83c36d806dc92327b9e7049a565c6bff","dataFormat":"wav","format":"","rate":44100,"sampleCount":37376,"md5ext":"83c36d806dc92327b9e7049a565c6bff.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":"1.2.52","agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36"}}
1 change: 1 addition & 0 deletions block.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def __init__(self):
self.timeDelay = 0 # wait time from the wait block
self.target = None # parent
self.substack = set() # blocks inside
self.substack2 = set() # only for if then else
self.script = set() # blocks below
self.screenRefresh = False # do a screen refresh
self.inEventLoop = False
Expand Down
2 changes: 1 addition & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

# Project file name
# If in test mode, set the Scratch project file to load.
projectFileName: str = "projects/ChangeColour.sb3"
projectFileName: str = "projects/ChangeColourWithElse.sb3"

# Download cache size
# Number of recent downloaded projects stored. 0 means infinity.
Expand Down
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""

__version__ = "v0.2.0"
__version__ = "v0.4.0"
__author__ = "Secret-chest"

import tkinter.simpledialog
Expand Down
Binary file added projects/ChangeColourWithElse.sb3
Binary file not shown.
43 changes: 42 additions & 1 deletion scratch.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,11 +387,52 @@ def execute(block, s, events=eventContainer.EventContainer()):
block.blockRan = True
return nextBlock
block.blockRan = True
# TODO why does it hang???
else:
block.blockRan = True
return s.target.blocks[block.next]

elif opcode == "control_if_else": # if <> then {...}
if block.target.blocks[inputs["CONDITION"][1]].evaluateBlockValue(events):
# If there are blocks, get them
if inputs["SUBSTACK"][1]:
# No blocks will be flagged as ran inside a forever loop
for b in block.substack:
s.target.blocks[b].blockRan = False
nextBlock = s.target.blocks[inputs["SUBSTACK"][1]]
nb = s.target.blocks[inputs["SUBSTACK"][1]]
block.substack.add(nb.blockID)
while nb.next and nb.next not in block.substack:
nb.blockRan = False
nb.waiting = False
nb.timeDelay = 0
nb.executionTime = 0
nb = s.target.blocks[nb.next]
block.substack.add(nb.blockID)
nb.next = block.next
block.blockRan = True
return nextBlock
block.blockRan = True
else:
# If there are blocks, get them
if inputs["SUBSTACK2"][1]:
# No blocks will be flagged as ran inside a forever loop
for b in block.substack2:
s.target.blocks[b].blockRan = False
nextBlock = s.target.blocks[inputs["SUBSTACK2"][1]]
nb = s.target.blocks[inputs["SUBSTACK2"][1]]
block.substack2.add(nb.blockID)
while nb.next and nb.next not in block.substack2:
nb.blockRan = False
nb.waiting = False
nb.timeDelay = 0
nb.executionTime = 0
nb = s.target.blocks[nb.next]
block.substack2.add(nb.blockID)
nb.next = block.next
block.blockRan = True
return nextBlock
block.blockRan = True

elif opcode == "looks_switchcostumeto": # switch costume to [... v]
nextBlock = block.getBlockInputValue("costume")
return s.target.blocks[nextBlock]
Expand Down