1313# import zipfile as zf
1414from tkinter .messagebox import *
1515import os
16- import cairosvg
17- import io
18- import json
19- import time
20-
21-
22- # Basic functions
23- # Those functions are used for basic sprite tasks. They will probably be moved to scratch.py.
24- # Load SVG in pygame
25- def loadSvg (svg_bytes ):
26- newBites = cairosvg .svg2png (bytestring = svg_bytes )
27- byteIo = io .BytesIO (newBites )
28- return pygame .image .load (byteIo )
29-
30-
31- # Render a sprite at its coordinates
32- def render (sprite , x , y , direction ):
33- # upscale sprite
34- sprite = pygame .transform .scale (sprite , (sprite .get_width () * 2 , sprite .get_height () * 2 ))
35- # set direction
36- sprite = pygame .transform .rotate (sprite , 90 - direction )
37- # convert Scratch coordinates into Pygame coordinates
38- finalX = x + WIDTH // 2 - sprite .get_width () // 2
39- finalY = HEIGHT // 2 - y - sprite .get_height () // 2
40- display .blit (sprite , (finalX , finalY ))
41-
42-
43- # Set the stage background
44- def setBackground (bg ):
45- render (bg , 0 , 0 , 90 )
16+ from targetSprite import TargetSprite
4617
4718
4819# Prepare project file
20+ allSprites = pygame .sprite .Group ()
4921projectToLoad = "wait_gotoxy.sb3" # change this to load a different project
5022targets , currentBgFile , project = s2p_unpacker .sb3_unpack (projectToLoad )
23+ for t in targets :
24+ allSprites .add (TargetSprite (t ))
5125wn = tk .Tk () # Start tkinter for popups
5226wn .withdraw () # Hide main tkinter window
53- allSprites = pygame .sprite .Group ()
5427# when needed
5528# wn.deiconify()
5629pygame .init () # Start pygame
5730scratch .startProject ()
5831# Set player size
59- HEIGHT = 720
60- WIDTH = 960
32+ HEIGHT = 360
33+ WIDTH = 480
6134projectName = projectToLoad [:- 4 ] # Set the project name
6235icon = pygame .image .load ("icon.png" )
6336display = pygame .display .set_mode ([WIDTH , HEIGHT ])
6437pygame .display .set_caption (projectName + " - Scratch2Python" )
6538pygame .display .set_icon (icon )
66- currentBg = loadSvg (currentBgFile )
39+ currentBg = scratch . loadSvg (currentBgFile )
6740# currentBgFile = project.read(target["costumes"][target["currentCostume"]]["md5ext"])
6841projectRunning = True
6942
7043display .fill ((255 , 255 , 255 ))
71- setBackground (currentBg )
44+ scratch . setBackground (currentBg , display )
7245while projectRunning :
7346 for event in pygame .event .get ():
7447 # Window quit (ALT-F4 / X button)
@@ -93,22 +66,35 @@ def setBackground(bg):
9366 os .mkdir ("assets" )
9467 project .extractall ("assets" )
9568 display .fill ((255 , 255 , 255 ))
96- setBackground (currentBg )
69+ scratch . setBackground (currentBg , display )
9770 # Move all sprites to current position and direction, run blocks
98- for target in targets :
99- render (loadSvg (target .costumes [target .currentCostume ].file ), target .x * 2 , target .y * 2 , target .direction )
100- for _ , block in target .blocks .items ():
71+ for s in allSprites :
72+ for _ , block in s .target .blocks .items ():
10173 if not block .blockRan :
10274 print ("DEBUG: Running opcode" , block .opcode )
10375 print ("DEBUG: Running ID" , block .blockID )
10476 if block .next :
10577 print ("DEBUG: Next ID" , block .next )
106- nextBlock = target .blocks [block .next ]
78+ nextBlock = s . target .blocks [block .next ]
10779 print ("DEBUG: Next opcode" , nextBlock .opcode )
10880 else :
10981 print ("DEBUG: Last block" )
110- scratch .execute (block , target )
82+ scratch .execute (block , s )
11183 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
11298 allSprites .draw (display )
11399 allSprites .update ()
114100 pygame .display .flip ()
0 commit comments