Skip to content

Commit 88bd8ca

Browse files
committed
Normal size, sprites for each target
1 parent d732fe8 commit 88bd8ca

File tree

5 files changed

+73
-55
lines changed

5 files changed

+73
-55
lines changed

block.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ def __init__(self):
1818
self.shadow = False # if the block is a reporter or boolean block
1919
self.topLevel = False # if the block is a hat block
2020
self.blockRan = False
21+
self.waiting = False
22+
self.executionTime = 0
23+
self.timeDelay = 0
24+
self.target = None

main.py

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
projectToLoad = "wait_gotoxy.sb3" # change this to load a different project
2222
targets, currentBgFile, project = s2p_unpacker.sb3_unpack(projectToLoad)
2323
for t in targets:
24-
allSprites.add(TargetSprite(t))
24+
sprite = TargetSprite(t)
25+
t.sprite = sprite
26+
allSprites.add(sprite)
2527
wn = tk.Tk() # Start tkinter for popups
2628
wn.withdraw() # Hide main tkinter window
2729
# when needed
@@ -31,7 +33,7 @@
3133
# Set player size
3234
HEIGHT = 360
3335
WIDTH = 480
34-
projectName = projectToLoad[:-4] # Set the project name
36+
projectName = projectToLoad[:-4] # Set the project name
3537
icon = pygame.image.load("icon.png")
3638
display = pygame.display.set_mode([WIDTH, HEIGHT])
3739
pygame.display.set_caption(projectName + " - Scratch2Python" )
@@ -40,7 +42,15 @@
4042
# currentBgFile = project.read(target["costumes"][target["currentCostume"]]["md5ext"])
4143
projectRunning = True
4244

45+
clock = pygame.time.Clock()
4346
display.fill((255, 255, 255))
47+
toExecute = []
48+
49+
for s in allSprites:
50+
for _, block in s.target.blocks.items():
51+
if block.opcode == "event_whenflagclicked":
52+
scratch.execute(block, s)
53+
4454
scratch.setBackground(currentBg, display)
4555
while projectRunning:
4656
for event in pygame.event.get():
@@ -68,35 +78,24 @@
6878
display.fill((255, 255, 255))
6979
scratch.setBackground(currentBg, display)
7080
# Move all sprites to current position and direction, run blocks
71-
for s in allSprites:
72-
for _, block in s.target.blocks.items():
73-
if not block.blockRan:
74-
print("DEBUG: Running opcode", block.opcode)
75-
print("DEBUG: Running ID", block.blockID)
76-
if block.next:
77-
print("DEBUG: Next ID", block.next)
78-
nextBlock = s.target.blocks[block.next]
79-
print("DEBUG: Next opcode", nextBlock.opcode)
80-
else:
81-
print("DEBUG: Last block")
82-
scratch.execute(block, s)
83-
block.blockRan = True
84-
# for target in targets:
85-
# scratch.render(scratch.loadSvg(target.costumes[target.currentCostume].file), target.x * 2, target.y * 2, target.direction, display)
86-
# for _, block in target.blocks.items():
87-
# if not block.blockRan:
88-
# print("DEBUG: Running opcode", block.opcode)
89-
# print("DEBUG: Running ID", block.blockID)
90-
# if block.next:
91-
# print("DEBUG: Next ID", block.next)
92-
# nextBlock = target.blocks[block.next]
93-
# print("DEBUG: Next opcode", nextBlock.opcode)
94-
# else:
95-
# print("DEBUG: Last block")
96-
# scratch.execute(block, target)
97-
# block.blockRan = True
81+
nextBlocks = []
82+
for block in toExecute:
83+
if block.waiting:
84+
block.executionTime += clock.get_time()
85+
if block.executionTime >= block.timeDelay:
86+
block.waiting = False
87+
block.executionTime, block.timeDelay = 0, 0
88+
print("DEBUG: Wait period ended")
89+
if not block.blockRan:
90+
print("DEBUG: Running opcode", block.opcode)
91+
print("DEBUG: Running ID", block.blockID)
92+
nextBlock = scratch.execute(block, block.target.sprite)
93+
if nextBlock:
94+
nextBlocks.append(nextBlock)
95+
toExecute = nextBlocks
9896
allSprites.draw(display)
9997
allSprites.update()
10098
pygame.display.flip()
10199
wn.update()
100+
clock.tick(30)
102101
pygame.quit()

s2p_unpacker.py

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,31 @@ def sb3_unpack(sb3):
2929
for target_obj in project_json['targets']:
3030
if target_obj["isStage"]:
3131
current_bg_file = project.read(target_obj["costumes"][target_obj["currentCostume"]]["assetId"] + "." + target_obj["costumes"][target_obj["currentCostume"]]["dataFormat"])
32-
else:
33-
t = target.Target()
32+
t = target.Target()
33+
if "x" in target_obj:
3434
t.x = target_obj["x"]
3535
t.y = target_obj["y"]
3636
t.direction = target_obj["direction"]
37-
t.currentCostume = target_obj["currentCostume"]
38-
for costume_obj in target_obj["costumes"]:
39-
c = costume.Costume()
40-
if "md5ext" in costume_obj:
41-
c.md5ext = costume_obj["md5ext"]
42-
c.file = project.read(costume_obj["assetId"] + "." + costume_obj["dataFormat"])
43-
t.costumes.append(c)
44-
for block_id, block_obj in target_obj["blocks"].items():
45-
b = block.Block()
46-
b.blockID = block_id
47-
b.opcode = block_obj["opcode"]
48-
b.next = block_obj["next"]
49-
b.parent = block_obj["parent"]
50-
b.shadow = block_obj["shadow"]
51-
b.topLevel = block_obj["topLevel"]
52-
b.inputs = block_obj["inputs"]
53-
b.fields = block_obj["inputs"]
54-
b.blockRan = False
55-
t.blocks[block_id] = b
56-
targets.append(t)
37+
t.currentCostume = target_obj["currentCostume"]
38+
for costume_obj in target_obj["costumes"]:
39+
c = costume.Costume()
40+
if "md5ext" in costume_obj:
41+
c.md5ext = costume_obj["md5ext"]
42+
c.file = project.read(costume_obj["assetId"] + "." + costume_obj["dataFormat"])
43+
t.costumes.append(c)
44+
for block_id, block_obj in target_obj["blocks"].items():
45+
b = block.Block()
46+
b.blockID = block_id
47+
b.opcode = block_obj["opcode"]
48+
b.next = block_obj["next"]
49+
b.parent = block_obj["parent"]
50+
b.shadow = block_obj["shadow"]
51+
b.topLevel = block_obj["topLevel"]
52+
b.inputs = block_obj["inputs"]
53+
b.fields = block_obj["inputs"]
54+
b.blockRan = False
55+
b.target = t
56+
t.blocks[block_id] = b
57+
targets.append(t)
5758
return targets, current_bg_file, project
5859

scratch.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,27 @@ def startProject():
3535

3636
# Run the given block object
3737
def execute(block, s):
38-
# TODO: Multi-threading
3938
opcode = block.opcode
4039
id = block.blockID
4140
blockRan = block.blockRan
4241
inputs = block.inputs
4342
fields = block.fields
43+
nextBlock = None
4444
if opcode == "motion_gotoxy":
4545
s.setXy(int(inputs["X"][1][1]), int(inputs["Y"][1][1]))
46-
# if opcode == "control_wait":
47-
# timeDelay = int(round(float(inputs["DURATION"][1][1]) * 1000))
48-
# pygame.time.delay(timeDelay)
46+
if opcode == "control_wait":
47+
block.timeDelay = int(round(float(inputs["DURATION"][1][1]) * 1000))
48+
block.waiting = True
49+
block.executionTime = 0
50+
print("DEBUG: Waiting for", block.timeDelay, "ms")
51+
return block
52+
if opcode == "event_whenflagclicked":
53+
pass
54+
if block.next:
55+
print("DEBUG: Next ID", block.next)
56+
nextBlock = s.target.blocks[block.next]
57+
print("DEBUG: Next opcode", nextBlock.opcode)
58+
else:
59+
print("DEBUG: Script finished")
60+
block.blockRan = True
61+
return nextBlock

target.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ def __init__(self):
2525
self.direction = 90
2626
self.draggable = False
2727
self.rotationStyle = "all around" # all around, left-right or do not rotate
28+
self.sprite = None

0 commit comments

Comments
 (0)