|
21 | 21 | projectToLoad = "wait_gotoxy.sb3" # change this to load a different project |
22 | 22 | targets, currentBgFile, project = s2p_unpacker.sb3_unpack(projectToLoad) |
23 | 23 | for t in targets: |
24 | | - allSprites.add(TargetSprite(t)) |
| 24 | + sprite = TargetSprite(t) |
| 25 | + t.sprite = sprite |
| 26 | + allSprites.add(sprite) |
25 | 27 | wn = tk.Tk() # Start tkinter for popups |
26 | 28 | wn.withdraw() # Hide main tkinter window |
27 | 29 | # when needed |
|
31 | 33 | # Set player size |
32 | 34 | HEIGHT = 360 |
33 | 35 | WIDTH = 480 |
34 | | -projectName = projectToLoad[:-4] # Set the project name |
| 36 | +projectName = projectToLoad[:-4] # Set the project name |
35 | 37 | icon = pygame.image.load("icon.png") |
36 | 38 | display = pygame.display.set_mode([WIDTH, HEIGHT]) |
37 | 39 | pygame.display.set_caption(projectName + " - Scratch2Python" ) |
|
40 | 42 | # currentBgFile = project.read(target["costumes"][target["currentCostume"]]["md5ext"]) |
41 | 43 | projectRunning = True |
42 | 44 |
|
| 45 | +clock = pygame.time.Clock() |
43 | 46 | 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 | + |
44 | 54 | scratch.setBackground(currentBg, display) |
45 | 55 | while projectRunning: |
46 | 56 | for event in pygame.event.get(): |
|
68 | 78 | display.fill((255, 255, 255)) |
69 | 79 | scratch.setBackground(currentBg, display) |
70 | 80 | # 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 |
98 | 96 | allSprites.draw(display) |
99 | 97 | allSprites.update() |
100 | 98 | pygame.display.flip() |
101 | 99 | wn.update() |
| 100 | + clock.tick(30) |
102 | 101 | pygame.quit() |
0 commit comments