Skip to content

Commit

Permalink
Go to random position block, change ifs to elifs in scratch.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Secret-chest committed Feb 11, 2022
1 parent d6fd894 commit 735d7b6
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 103 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/scratch2python.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion assets/40ce8794cdbb3bc7be6d7d6439ae9e30.svg

This file was deleted.

1 change: 1 addition & 0 deletions assets/c2cf8771e8c36feadfcfebe3bf2d7a82.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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":{},"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":"Marker","variables":{},"lists":{},"broadcasts":{},"blocks":{"_t}LJ30sZsSGC[S9:y#J":{"opcode":"event_whenflagclicked","next":"@+MZW=!?pypAdTR)}ig}","parent":null,"inputs":{},"fields":{},"shadow":false,"topLevel":true,"x":24,"y":318},"@+MZW=!?pypAdTR)}ig}":{"opcode":"control_forever","next":null,"parent":"_t}LJ30sZsSGC[S9:y#J","inputs":{"SUBSTACK":[2,"(%ZjnJ7}|IvolG:r?rrl"]},"fields":{},"shadow":false,"topLevel":false},"(%ZjnJ7}|IvolG:r?rrl":{"opcode":"motion_goto","next":"%7{cFlY#!lMI@:G`T}g}","parent":"@+MZW=!?pypAdTR)}ig}","inputs":{"TO":[1,"No@G@YNiqIe5~Ojypj)Z"]},"fields":{},"shadow":false,"topLevel":false},"No@G@YNiqIe5~Ojypj)Z":{"opcode":"motion_goto_menu","next":null,"parent":"(%ZjnJ7}|IvolG:r?rrl","inputs":{},"fields":{"TO":["_mouse_",null]},"shadow":true,"topLevel":false},"%7{cFlY#!lMI@:G`T}g}":{"opcode":"motion_changeyby","next":null,"parent":"(%ZjnJ7}|IvolG:r?rrl","inputs":{"DY":[1,[4,"20"]]},"fields":{},"shadow":false,"topLevel":false}},"comments":{},"currentCostume":0,"costumes":[{"assetId":"40ce8794cdbb3bc7be6d7d6439ae9e30","name":"Cosmicat","bitmapResolution":1,"md5ext":"40ce8794cdbb3bc7be6d7d6439ae9e30.svg","dataFormat":"svg","rotationCenterX":14.340928909355938,"rotationCenterY":20.569287194044648}],"sounds":[],"volume":100,"layerOrder":1,"visible":true,"x":-161,"y":186,"size":100,"direction":90,"draggable":false,"rotationStyle":"all around"}],"monitors":[],"extensions":[],"meta":{"semver":"3.0.0","vm":"0.2.0-prerelease.20211110140254","agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36"}}
{"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":{"CDI$sU~aZ9+y[j$:3O@/":{"opcode":"event_whenflagclicked","next":"OLRg[rzBhAbI6MNk~9XC","parent":null,"inputs":{},"fields":{},"shadow":false,"topLevel":true,"x":-24,"y":360},"OLRg[rzBhAbI6MNk~9XC":{"opcode":"control_forever","next":null,"parent":"CDI$sU~aZ9+y[j$:3O@/","inputs":{"SUBSTACK":[2,"v`=_*RhtuOV$QI/g=[%l"]},"fields":{},"shadow":false,"topLevel":false},"v`=_*RhtuOV$QI/g=[%l":{"opcode":"control_wait","next":"qC8m#k0,K387C8bpQo.U","parent":"OLRg[rzBhAbI6MNk~9XC","inputs":{"DURATION":[1,[5,"1"]]},"fields":{},"shadow":false,"topLevel":false},"qC8m#k0,K387C8bpQo.U":{"opcode":"motion_goto","next":null,"parent":"v`=_*RhtuOV$QI/g=[%l","inputs":{"TO":[1,"8WIn?-MOo@`.4@QdG^MP"]},"fields":{},"shadow":false,"topLevel":false},"8WIn?-MOo@`.4@QdG^MP":{"opcode":"motion_goto_menu","next":null,"parent":"qC8m#k0,K387C8bpQo.U","inputs":{},"fields":{"TO":["_random_",null]},"shadow":true,"topLevel":false}},"comments":{},"currentCostume":0,"costumes":[{"assetId":"c2cf8771e8c36feadfcfebe3bf2d7a82","name":"Cosmicat","bitmapResolution":1,"md5ext":"c2cf8771e8c36feadfcfebe3bf2d7a82.svg","dataFormat":"svg","rotationCenterX":43.366432297511494,"rotationCenterY":53.10284427353852}],"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.20220208083128","agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36"}}
2 changes: 1 addition & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

# Project file name
# If the "manual" mode is chosen, set the Scratch project file to load.
projectFileName: str = "projects/gotomouse-up.sb3"
projectFileName: str = "projects/random-position.sb3"

# Extract on project run
# Set whether to extract the project assets on run.
Expand Down
15 changes: 8 additions & 7 deletions 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__ = "M11 (development version)"
__version__ = "M12 (development version)"
__author__ = "Secret-chest"

from platform import system
Expand Down Expand Up @@ -173,12 +173,13 @@
if not isPaused:
for e in eventHandlers:
if e.opcode == "event_whenkeypressed" and keys:
nextBlock = scratch.execute(block, block.target.sprite, keys)
if nextBlock:
if isinstance(nextBlock, list):
nextBlocks.extend(nextBlock)
else:
nextBlocks.append(nextBlock)
# nextBlock = scratch.execute(block, block.target.sprite, keys)
# if nextBlock:
# if isinstance(nextBlock, list):
# nextBlocks.extend(nextBlock)
# else:
# nextBlocks.append(nextBlock)
pass
while toExecute and not doScreenRefresh:
# Run blocks
nextBlocks = []
Expand Down
Binary file added projects/random-position.sb3
Binary file not shown.
Binary file added projects/rotation-center.sb3
Binary file not shown.
193 changes: 102 additions & 91 deletions scratch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Basically it emulates Scratch in Pygame, hence the name.
"""
import sys
import random

import pygame.time
import cairosvg
Expand All @@ -15,90 +16,90 @@

# Key maps to convert the key option in blocks to Pygame constants
KEY_MAPPING = {
"up arrow": pygame.K_UP,
"down arrow": pygame.K_DOWN,
"left arrow": pygame.K_LEFT,
"up arrow" : pygame.K_UP,
"down arrow" : pygame.K_DOWN,
"left arrow" : pygame.K_LEFT,
"right arrow": pygame.K_RIGHT,
"space": pygame.K_SPACE,
"a": pygame.K_a,
"b": pygame.K_b,
"c": pygame.K_c,
"d": pygame.K_d,
"e": pygame.K_e,
"f": pygame.K_f,
"g": pygame.K_g,
"h": pygame.K_h,
"i": pygame.K_i,
"j": pygame.K_j,
"k": pygame.K_k,
"l": pygame.K_l,
"m": pygame.K_m,
"n": pygame.K_n,
"o": pygame.K_o,
"p": pygame.K_p,
"q": pygame.K_q,
"r": pygame.K_r,
"s": pygame.K_s,
"t": pygame.K_t,
"u": pygame.K_u,
"v": pygame.K_v,
"w": pygame.K_w,
"x": pygame.K_x,
"y": pygame.K_y,
"z": pygame.K_z,
"0": pygame.K_0,
"1": pygame.K_1,
"2": pygame.K_2,
"3": pygame.K_3,
"4": pygame.K_4,
"5": pygame.K_5,
"6": pygame.K_6,
"7": pygame.K_7,
"8": pygame.K_8,
"9": pygame.K_9,
"space" : pygame.K_SPACE,
"a" : pygame.K_a,
"b" : pygame.K_b,
"c" : pygame.K_c,
"d" : pygame.K_d,
"e" : pygame.K_e,
"f" : pygame.K_f,
"g" : pygame.K_g,
"h" : pygame.K_h,
"i" : pygame.K_i,
"j" : pygame.K_j,
"k" : pygame.K_k,
"l" : pygame.K_l,
"m" : pygame.K_m,
"n" : pygame.K_n,
"o" : pygame.K_o,
"p" : pygame.K_p,
"q" : pygame.K_q,
"r" : pygame.K_r,
"s" : pygame.K_s,
"t" : pygame.K_t,
"u" : pygame.K_u,
"v" : pygame.K_v,
"w" : pygame.K_w,
"x" : pygame.K_x,
"y" : pygame.K_y,
"z" : pygame.K_z,
"0" : pygame.K_0,
"1" : pygame.K_1,
"2" : pygame.K_2,
"3" : pygame.K_3,
"4" : pygame.K_4,
"5" : pygame.K_5,
"6" : pygame.K_6,
"7" : pygame.K_7,
"8" : pygame.K_8,
"9" : pygame.K_9,

# Scratch supports these keys internally
"enter": pygame.K_RETURN,
"<": pygame.K_LESS,
">": pygame.K_GREATER,
"+": pygame.K_PLUS,
"-": pygame.K_MINUS,
"=": pygame.K_EQUALS,
".": pygame.K_PERIOD,
",": pygame.K_COMMA,
"%": pygame.K_PERCENT,
"$": pygame.K_DOLLAR,
"#": pygame.K_HASH,
"@": pygame.K_AT,
"!": pygame.K_EXCLAIM,
"^": pygame.K_CARET,
"&": pygame.K_AMPERSAND,
"*": pygame.K_ASTERISK,
"(": pygame.K_LEFTPAREN,
")": pygame.K_RIGHTPAREN,
"[": pygame.K_LEFTBRACKET,
"]": pygame.K_RIGHTBRACKET,
"?": pygame.K_QUESTION,
"\\": pygame.K_BACKSLASH,
"/": pygame.K_SLASH,
"'": pygame.K_QUOTE,
"\"": pygame.K_QUOTEDBL,
"`": pygame.K_BACKQUOTE,
"enter" : pygame.K_RETURN,
"<" : pygame.K_LESS,
">" : pygame.K_GREATER,
"+" : pygame.K_PLUS,
"-" : pygame.K_MINUS,
"=" : pygame.K_EQUALS,
"." : pygame.K_PERIOD,
"," : pygame.K_COMMA,
"%" : pygame.K_PERCENT,
"$" : pygame.K_DOLLAR,
"#" : pygame.K_HASH,
"@" : pygame.K_AT,
"!" : pygame.K_EXCLAIM,
"^" : pygame.K_CARET,
"&" : pygame.K_AMPERSAND,
"*" : pygame.K_ASTERISK,
"(" : pygame.K_LEFTPAREN,
")" : pygame.K_RIGHTPAREN,
"[" : pygame.K_LEFTBRACKET,
"]" : pygame.K_RIGHTBRACKET,
"?" : pygame.K_QUESTION,
"\\" : pygame.K_BACKSLASH,
"/" : pygame.K_SLASH,
"'" : pygame.K_QUOTE,
"\"" : pygame.K_QUOTEDBL,
"`" : pygame.K_BACKQUOTE,

# Scratch2Python only
"backspace": pygame.K_BACKSPACE,
"f1": pygame.K_F1,
"f2": pygame.K_F2,
"f3": pygame.K_F3,
"f4": pygame.K_F4,
"f5": pygame.K_F5,
"f6": pygame.K_F6,
"f7": pygame.K_F7,
"f8": pygame.K_F8,
"f9": pygame.K_F9,
"f10": pygame.K_F10,
"f11": pygame.K_F11,
"f12": pygame.K_F12
"backspace" : pygame.K_BACKSPACE,
"f1" : pygame.K_F1,
"f2" : pygame.K_F2,
"f3" : pygame.K_F3,
"f4" : pygame.K_F4,
"f5" : pygame.K_F5,
"f6" : pygame.K_F6,
"f7" : pygame.K_F7,
"f8" : pygame.K_F8,
"f9" : pygame.K_F9,
"f10" : pygame.K_F10,
"f11" : pygame.K_F11,
"f12" : pygame.K_F12
}


Expand All @@ -123,31 +124,40 @@ def execute(block, s, keys=[]):
if opcode == "motion_gotoxy": # go to x: () y: ()
s.setXy(int(block.getInputValue("x")), int(block.getInputValue("y")))

if opcode == "motion_goto":
elif opcode == "motion_goto":
nextBlock = block.getBlockInputValue("to")
return s.target.blocks[nextBlock]

if opcode == "motion_goto_menu":
elif opcode == "motion_goto_menu":
if block.getFieldValue("to") == "_mouse_": # go to [mouse pointer v]
newX, newY = pygame.mouse.get_pos()
newX = newX - WIDTH // 2
newY = HEIGHT // 2 - newY
s.setXy(newX, newY)
return s.target.blocks[s.target.blocks[block.parent].next]

if opcode == "motion_setx": # set x to ()
elif block.getFieldValue("to") == "_random_": # go to [random position v]
minX = 0 - WIDTH // 2
maxX = WIDTH // 2
minY = 0 - HEIGHT // 2
maxY = HEIGHT // 2
newX, newY = (random.randint(minX, maxX), random.randint(minY, maxY))
s.setXy(newX, newY)
return s.target.blocks[s.target.blocks[block.parent].next]

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

if opcode == "motion_changexby": # change x by ()
elif opcode == "motion_changexby": # change x by ()
s.setXy(s.x + int(block.getInputValue("dx")), s.y)

if opcode == "motion_sety": # set y to ()
elif opcode == "motion_sety": # set y to ()
s.setXy(s.x, int(block.getInputValue("y")))

if opcode == "motion_changeyby": # change y by ()
elif opcode == "motion_changeyby": # change y by ()
s.setXy(s.x, s.y + int(block.getInputValue("dy")))

if opcode == "control_wait": # wait () seconds
elif opcode == "control_wait": # wait () seconds
block.screenRefresh = True
if not block.waiting:
# Get time delay and convert it to milliseconds
Expand All @@ -157,10 +167,10 @@ def execute(block, s, keys=[]):
print("DEBUG: Waiting for", block.timeDelay, "ms", file=sys.stderr)
return block

if opcode == "event_whenflagclicked": # when green flag clicked
elif opcode == "event_whenflagclicked": # when green flag clicked
pass

if opcode == "event_whenkeypressed": # when key [... v] pressed
elif opcode == "event_whenkeypressed":
# if not block.waiting:
# # Get time delay and convert it to milliseconds
# block.timeDelay = 500
Expand All @@ -169,7 +179,7 @@ def execute(block, s, keys=[]):
# print("DEBUG: Waiting for", block.timeDelay, "ms")
key = block.getFieldValue("key_option", lookIn=0)

if key == "any":
if key == "any": # when key [any v] pressed
print("DEBUG: Handling key", key, file=sys.stderr)
if keys:
nb = block # s.target.blocks[block.next]
Expand All @@ -187,7 +197,8 @@ def execute(block, s, keys=[]):
nextBlock = s.target.blocks[block.next]
block.blockRan = False
return nextBlock
elif KEY_MAPPING[key] in keys and block.next:

elif KEY_MAPPING[key] in keys and block.next: # when key [. . . v] pressed
print("DEBUG: Handling key", key, file=sys.stderr)
nb = block # s.target.blocks[block.next]
nb.blockRan = False
Expand All @@ -204,7 +215,7 @@ def execute(block, s, keys=[]):
nextBlock = s.target.blocks[block.next]
return nextBlock

if opcode == "control_forever": # forever {..}
elif opcode == "control_forever": # forever {..}
# Don't mark the loop as ran, and do a screen refresh
block.blockRan = False
block.screenRefresh = True
Expand Down

0 comments on commit 735d7b6

Please sign in to comment.