|
24 | 24 | import scratch |
25 | 25 | import pygame |
26 | 26 | import tkinter as tk |
| 27 | +from pathlib import Path |
27 | 28 | # import zipfile as zf |
28 | 29 | from tkinter.messagebox import * |
29 | 30 | import os |
30 | 31 | from targetSprite import TargetSprite |
31 | 32 |
|
32 | | -version = "M5" |
| 33 | +VERSION = "M6" |
33 | 34 |
|
34 | | -# Prepare project file |
| 35 | +# Change this to a different project file |
| 36 | +PROJECT = "projects/forever.sb3" |
| 37 | + |
| 38 | +# Get project data and create sprites |
| 39 | +targets, currentBgFile, project = s2p_unpacker.sb3_unpack(PROJECT) |
35 | 40 | allSprites = pygame.sprite.Group() |
36 | | -projectToLoad = "projects/forever.sb3" # change this to load a different project |
37 | | -targets, currentBgFile, project = s2p_unpacker.sb3_unpack(projectToLoad) |
38 | 41 | for t in targets: |
39 | 42 | sprite = TargetSprite(t) |
40 | 43 | t.sprite = sprite |
41 | 44 | allSprites.add(sprite) |
42 | | -wn = tk.Tk() # Start tkinter for popups |
43 | | -wn.withdraw() # Hide main tkinter window |
44 | | -pygame.init() # Start pygame |
| 45 | + |
| 46 | +# Start tkinter for showing some popups, and hide main window |
| 47 | +wn = tk.Tk() |
| 48 | +wn.withdraw() |
| 49 | + |
| 50 | +# Start Pygame, load fonts and print a debug message |
| 51 | +pygame.init() |
| 52 | +font = pygame.font.SysFont("sans-serif", 16) |
| 53 | +fontXl = pygame.font.SysFont("sans-serif", 36) |
45 | 54 | scratch.startProject() |
| 55 | + |
| 56 | +# Create paused message |
| 57 | +paused = fontXl.render("Paused (Press F6 to resume)", 1, (0, 0, 0)) |
| 58 | +pausedWidth, pausedHeight = fontXl.size("Paused (Press F6 to resume)") |
| 59 | + |
46 | 60 | # Set player size |
47 | 61 | HEIGHT = 360 |
48 | 62 | WIDTH = 480 |
49 | | -projectName = projectToLoad[:-4] # Set the project name for use in the titlebar |
| 63 | + |
| 64 | +# Get project name and set icon |
| 65 | +projectName = Path(PROJECT).stem |
50 | 66 | icon = pygame.image.load("icon.png") |
| 67 | + |
| 68 | +# Create project player and window |
51 | 69 | display = pygame.display.set_mode([WIDTH, HEIGHT]) |
52 | | -pygame.display.set_caption(projectName + " - Scratch2Python") |
| 70 | +pygame.display.set_caption(projectName + " - Scratch2Python" + " " + VERSION) |
53 | 71 | pygame.display.set_icon(icon) |
| 72 | + |
| 73 | +# Get background image |
54 | 74 | currentBg = scratch.loadSvg(currentBgFile) |
55 | | -# currentBgFile = project.read(target["costumes"][target["currentCostume"]]["md5ext"]) |
| 75 | + |
| 76 | +# Set running state |
56 | 77 | projectRunning = True |
| 78 | +isPaused = False |
57 | 79 |
|
| 80 | +# Initialize clock |
58 | 81 | clock = pygame.time.Clock() |
| 82 | + |
| 83 | +# Clear display |
59 | 84 | display.fill((255, 255, 255)) |
| 85 | + |
| 86 | +# Create a block execution queue |
60 | 87 | toExecute = [] |
61 | 88 |
|
| 89 | +# Start green flag scripts |
62 | 90 | for s in allSprites: |
63 | 91 | for _, block in s.target.blocks.items(): |
64 | 92 | if block.opcode == "event_whenflagclicked": |
65 | 93 | print("DEBUG: Running opcode", block.opcode) |
66 | 94 | print("DEBUG: Running ID", block.blockID) |
67 | 95 | nextBlock = scratch.execute(block, block.target.sprite) |
| 96 | + # Error-proof by checking if the scripts are not empty |
68 | 97 | if nextBlock: |
| 98 | + # Add the next block to the queue |
69 | 99 | toExecute.append(nextBlock) |
70 | 100 |
|
| 101 | +# Display the background |
71 | 102 | scratch.setBackground(currentBg, display) |
| 103 | + |
| 104 | +# Mainloop |
72 | 105 | while projectRunning: |
| 106 | + # Process Pygame events |
73 | 107 | for event in pygame.event.get(): |
74 | 108 | # Window quit (ALT-F4 / X button) |
75 | 109 | if event.type == pygame.QUIT: |
76 | 110 | projectRunning = False |
77 | | - # Some controls |
| 111 | + # Debug and utility functions |
78 | 112 | keys = pygame.key.get_pressed() |
79 | 113 | if keys[pygame.K_F1]: # Help |
80 | 114 | showinfo("Work in progress", "Help not currently available") |
|
85 | 119 | print(project.namelist()) |
86 | 120 | if keys[pygame.K_F4]: # Project info |
87 | 121 | showinfo("Work in progress", "Project info coming soon") |
88 | | - if keys[pygame.K_F5]: # Project info |
| 122 | + if keys[pygame.K_F5]: # Extract |
89 | 123 | confirm = askyesno("Extract", "Extract all project files?") |
90 | 124 | if confirm: |
91 | 125 | print("DEBUG: Extracting project") |
92 | 126 | shutil.rmtree("assets") |
93 | 127 | os.mkdir("assets") |
94 | 128 | project.extractall("assets") |
| 129 | + if keys[pygame.K_F6]: # Pause |
| 130 | + isPaused = not isPaused |
95 | 131 | display.fill((255, 255, 255)) |
96 | | - scratch.setBackground(currentBg, display) |
97 | | - # Move all sprites to current position and direction, run blocks |
98 | | - nextBlocks = [] |
99 | | - for block in toExecute: |
100 | | - if block.waiting: |
101 | | - print("DEBUG: Block execution time is", block.executionTime, "delay is", block.timeDelay) |
102 | | - block.executionTime += clock.get_time() |
103 | | - if block.executionTime >= block.timeDelay: |
104 | | - block.waiting = False |
105 | | - block.blockRan = True |
106 | | - nextBlocks.append(block.target.blocks[block.next]) |
107 | | - block.executionTime, block.timeDelay = 0, 0 |
108 | | - print("DEBUG: Wait period ended") |
109 | | - if not block.blockRan: |
110 | | - print("DEBUG: Running opcode", block.opcode) |
111 | | - print("DEBUG: Running ID", block.blockID) |
112 | | - nextBlock = scratch.execute(block, block.target.sprite) |
113 | | - if nextBlock: |
114 | | - print("DEBUG: Next block is", nextBlock.opcode) |
115 | | - nextBlocks.append(nextBlock) |
116 | | - toExecute = nextBlocks[:] |
117 | | - allSprites.draw(display) |
118 | | - allSprites.update() |
| 132 | + if not isPaused: |
| 133 | + scratch.setBackground(currentBg, display) |
| 134 | + # Move all sprites to current position and direction, run blocks |
| 135 | + nextBlocks = [] |
| 136 | + for block in toExecute: |
| 137 | + if block.waiting: |
| 138 | + print("DEBUG: Block execution time is", block.executionTime, "delay is", block.timeDelay) |
| 139 | + block.executionTime += clock.get_time() |
| 140 | + if block.executionTime >= block.timeDelay: |
| 141 | + block.waiting = False |
| 142 | + block.blockRan = True |
| 143 | + nextBlocks.append(block.target.blocks[block.next]) |
| 144 | + block.executionTime, block.timeDelay = 0, 0 |
| 145 | + print("DEBUG: Wait period ended") |
| 146 | + if not block.blockRan: |
| 147 | + print("DEBUG: Running opcode", block.opcode) |
| 148 | + print("DEBUG: Running ID", block.blockID) |
| 149 | + nextBlock = scratch.execute(block, block.target.sprite) |
| 150 | + if nextBlock: |
| 151 | + print("DEBUG: Next block is", nextBlock.opcode) |
| 152 | + nextBlocks.append(nextBlock) |
| 153 | + toExecute = nextBlocks[:] |
| 154 | + allSprites.draw(display) |
| 155 | + allSprites.update() |
| 156 | + else: |
| 157 | + display.blit(paused, (WIDTH // 2 - pausedWidth // 2, WIDTH // 2 - pausedHeight // 2)) |
119 | 158 | pygame.display.flip() |
120 | 159 | wn.update() |
121 | 160 | clock.tick(30) |
|
0 commit comments