Skip to content

Commit

Permalink
Merge pull request #50 from Secret-chest/conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
Secret-chest authored Nov 16, 2022
2 parents 56efce9 + 9659baa commit 5bc153e
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 20 deletions.
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":[{"assetId":"cd21514d0531fdffb22204e0ec5ed84a","name":"decor1","md5ext":"cd21514d0531fdffb22204e0ec5ed84a.svg","dataFormat":"svg","rotationCenterX":240,"rotationCenterY":180}],"sounds":[{"assetId":"83a9787d4cb6f3b7632b4ddfebf74367","name":"pop","dataFormat":"wav","format":"","rate":44100,"sampleCount":1032,"md5ext":"83a9787d4cb6f3b7632b4ddfebf74367.wav"}],"volume":100,"layerOrder":0,"tempo":60,"videoTransparency":50,"videoState":"on","textToSpeechLanguage":null},{"isStage":false,"name":"Personaj1","variables":{},"lists":{},"broadcasts":{},"blocks":{"2gXe00(^=5?jAV4GO65H":{"opcode":"event_whenkeypressed","next":"[6l:dI8V6DP/46FRvqWF","parent":null,"inputs":{},"fields":{"KEY_OPTION":["up arrow",null]},"shadow":false,"topLevel":true,"x":221,"y":482},"[6l:dI8V6DP/46FRvqWF":{"opcode":"control_repeat","next":"66+5V9OT]aVmQ~an[lHS","parent":"2gXe00(^=5?jAV4GO65H","inputs":{"TIMES":[1,[6,"10"]],"SUBSTACK":[2,"!5m_j*V4@%-:ZJk_u8BM"]},"fields":{},"shadow":false,"topLevel":false},"66+5V9OT]aVmQ~an[lHS":{"opcode":"control_repeat","next":null,"parent":"[6l:dI8V6DP/46FRvqWF","inputs":{"TIMES":[1,[6,"10"]],"SUBSTACK":[2,"0w,.oaq;Me*YJldkC8F."]},"fields":{},"shadow":false,"topLevel":false},"!5m_j*V4@%-:ZJk_u8BM":{"opcode":"motion_changeyby","next":null,"parent":"[6l:dI8V6DP/46FRvqWF","inputs":{"DY":[1,[4,"10"]]},"fields":{},"shadow":false,"topLevel":false},"0w,.oaq;Me*YJldkC8F.":{"opcode":"motion_changeyby","next":null,"parent":"66+5V9OT]aVmQ~an[lHS","inputs":{"DY":[1,[4,"-10"]]},"fields":{},"shadow":false,"topLevel":false}},"comments":{},"currentCostume":0,"costumes":[{"assetId":"b7853f557e4426412e64bb3da6531a99","name":"costum1","bitmapResolution":1,"md5ext":"b7853f557e4426412e64bb3da6531a99.svg","dataFormat":"svg","rotationCenterX":48,"rotationCenterY":50},{"assetId":"e6ddc55a6ddd9cc9d84fe0b4c21e016f","name":"costum2","bitmapResolution":1,"md5ext":"e6ddc55a6ddd9cc9d84fe0b4c21e016f.svg","dataFormat":"svg","rotationCenterX":46,"rotationCenterY":53}],"sounds":[{"assetId":"83c36d806dc92327b9e7049a565c6bff","name":"Miau","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":"0.2.0-prerelease.20201016122132","agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Scratch/3.18.1 Chrome/80.0.3987.165 Electron/8.2.5 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,"/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"}}
15 changes: 11 additions & 4 deletions block.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import config
import math
import pygame
import eventContainer
from scratch import KEY_MAPPING

i18n.set("locale", config.language)
i18n.set("filename_format", "{locale}.{format}")
Expand Down Expand Up @@ -40,9 +42,10 @@ def __init__(self):
self.inEventLoop = False
self.value = None # reported value
self.repeatCounter = None # for repeat block
self.canRun = None # for if then / if then else

# Evaluates block value (for reporters)
def evaluateBlockValue(self):
def evaluateBlockValue(self, eventContainer=eventContainer.EventContainer):
if self.opcode == "operator_add": # () + ()
self.value = float(self.getInputValue("num1")) + float(self.getInputValue("num2"))
return self.value
Expand Down Expand Up @@ -83,18 +86,22 @@ def evaluateBlockValue(self):
newY = newY - config.screenWidth // 2
self.value = newY
return newY
elif self.opcode == "sensing_keypressed": # key pressed?
return KEY_MAPPING[self.getMenuValue("key_option")] in eventContainer.keys
elif self.opcode == "operator_not": # not <>
return not self.target.blocks[self.getBlockInputValue("operand")].evaluateBlockValue(eventContainer)

# Returns block input value
def getBlockInputValue(self, inputId):
return self.inputs[inputId.upper()][1]

# Returns block input value
def getInputValue(self, inputId, lookIn=(1, 1)):
def getInputValue(self, inputId, lookIn=(1, 1), eventContainer=eventContainer.EventContainer()):
if self.inputs[inputId.upper()][lookIn[0]][0] in {4, 0, 5, 6}:
return self.inputs[inputId.upper()][lookIn[0]][1] or 0
elif self.inputs[inputId.upper()][0] == 3:
blockLink = self.inputs[inputId.upper()][1]
return self.target.blocks[blockLink].evaluateBlockValue()
return self.target.blocks[blockLink].evaluateBlockValue(eventContainer)
else:
pass

Expand All @@ -104,7 +111,7 @@ def getCustomInputValue(self, number, lookIn=(1, 1)):

# Returns dropdown menu value (menus are separate blocks)
def getMenuValue(self, menuId):
return self.inputs[menuId.upper()][1].fields[menuId.upper()][0]
return self.target.blocks[self.inputs[menuId.upper()][1]].fields[menuId.upper()][0]

# Returns field value (menus are separate blocks)
def getFieldValue(self, fieldId, lookIn=0):
Expand Down
5 changes: 2 additions & 3 deletions 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/KeyJump.sb3"
projectFileName: str = "projects/ChangeColour.sb3"

# Download cache size
# Number of recent downloaded projects stored. 0 means infinity.
Expand All @@ -40,14 +40,13 @@

# Enable debug messages
# Set whether debug messages (messages to stderr) should be allowed.
enableDebugMessages: bool = False
enableDebugMessages: bool = True

# Enable pygame welcome message
# Enable or disable the "
# pygame X.Y.Z (SDL X.Y.Z, Python 3.Y.Z)
# Hello from the pygame community. https://www.pygame.org/contribute.html"
# message.

pygameWelcomeMessage: bool = True

# Enable Scratch Addons debugger logs
Expand Down
Binary file added projects/ChangeColour.sb3
Binary file not shown.
Binary file added projects/NotKeyPressed.sb3
Binary file not shown.
48 changes: 37 additions & 11 deletions scratch.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def execute(block, s, events=eventContainer.EventContainer()):
keyEvents = events.keyEvents

if opcode == "motion_gotoxy": # go to x: () y: ()
s.setXy(float(block.getInputValue("x")), float(block.getInputValue("y")))
s.setXy(float(block.getInputValue("x", eventContainer=events)), float(block.getInputValue("y", eventContainer=events)))

elif opcode == "motion_goto":
nextBlock = block.getBlockInputValue("to")
Expand Down Expand Up @@ -194,28 +194,28 @@ def execute(block, s, events=eventContainer.EventContainer()):
return

elif opcode == "motion_setx": # set x to ()
s.setXy(float(block.getInputValue("x")), s.y)
s.setXy(float(block.getInputValue("x", eventContainer=events)), s.y)

elif opcode == "motion_changexby": # change x by ( )
s.setXyDelta(float(block.getInputValue("dx")), 0)
s.setXyDelta(float(block.getInputValue("dx", eventContainer=events)), 0)

elif opcode == "motion_sety": # set y to ()
s.setXy(s.x, float(block.getInputValue("y")))
s.setXy(s.x, float(block.getInputValue("y", eventContainer=events)))

elif opcode == "motion_changeyby": # change y by ()
s.setXyDelta(0, float(block.getInputValue("dy")))
s.setXyDelta(0, float(block.getInputValue("dy", eventContainer=events)))

elif opcode == "motion_turnleft": # turn ccw () degrees
s.setRotDelta(0 - float(block.getInputValue("degrees")))
s.setRotDelta(0 - float(block.getInputValue("degrees", eventContainer=events)))

elif opcode == "motion_turnright": # turn cw () degrees
s.setRotDelta(float(block.getInputValue("degrees")))
s.setRotDelta(float(block.getInputValue("degrees", eventContainer=events)))

elif opcode == "control_wait": # wait () seconds
block.screenRefresh = True
if not block.waiting:
# Get time delay and convert it to milliseconds
block.timeDelay = int(round(float(float(block.getInputValue("duration"))) * 1000))
block.timeDelay = int(round(float(float(block.getInputValue("duration", eventContainer=events))) * 1000))
block.waiting = True
block.executionTime = 0
print(_("debug-prefix"), _("block-waiting", time=block.timeDelay), file=sys.stderr)
Expand Down Expand Up @@ -332,7 +332,7 @@ def execute(block, s, events=eventContainer.EventContainer()):

elif opcode == "control_repeat": # repeat (10) {...}
if block.repeatCounter is None:
block.repeatCounter = int(block.getInputValue("times"))
block.repeatCounter = int(block.getInputValue("times", eventContainer=events))
# Don't mark the loop as ran until done, and do a screen refresh
if block.repeatCounter > 0:
block.blockRan = False
Expand Down Expand Up @@ -366,6 +366,32 @@ def execute(block, s, events=eventContainer.EventContainer()):
nb.next = block.blockID
return nextBlock

elif opcode == "control_if": # 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
# TODO why does it hang???
else:
block.blockRan = True
return s.target.blocks[block.next]

elif opcode == "looks_switchcostumeto": # switch costume to [... v]
nextBlock = block.getBlockInputValue("costume")
return s.target.blocks[nextBlock]
Expand Down Expand Up @@ -429,9 +455,9 @@ def execute(block, s, events=eventContainer.EventContainer()):
if block.proccode == "​​log​​ %s": # Scratch Addons log ()
print("[", datetime.now().strftime("%H:%M:%S:%f"), "]", _("project-log"), block.getCustomInputValue(0), file=sys.stderr)
elif block.proccode == "​​warn​​ %s": # Scratch Addons warn ()
print(_("project-warn"), block.getCustomInputValue(0), file=sys.stderr)
print("[", datetime.now().strftime("%H:%M:%S:%f"), "]", _("project-warn"), block.getCustomInputValue(0), file=sys.stderr)
elif block.proccode == "​​error​​ %s": # Scratch Addons error ()
print(_("project-error"), block.getCustomInputValue(0), file=sys.stderr)
print("[", datetime.now().strftime("%H:%M:%S:%f"), "]", _("project-error"), block.getCustomInputValue(0), file=sys.stderr)

else:
print(_("unknown-opcode"), opcode)
Expand Down
2 changes: 1 addition & 1 deletion targetSprite.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def setXy(self, x, y):
# Set X and Y
self.x = x
self.y = y
print(_("debug-prefix"), _("new-sprite-position", x=x, y=y, name=self.name), file=sys.stderr)
# print(_("debug-prefix"), _("new-sprite-position", x=x, y=y, name=self.name), file=sys.stderr)
# Scratch really is weird.
if self.isBitmap:
self.rect.x = self.x + scratch.WIDTH // 2 - round(self.target.costumes[self.target.currentCostume].rotationCenterX) + round(self.imageSize[0] / 2)
Expand Down

0 comments on commit 5bc153e

Please sign in to comment.