Skip to content

Commit d732fe8

Browse files
committed
Normal size, sprites for each target
1 parent 28454a4 commit d732fe8

File tree

4 files changed

+81
-48
lines changed

4 files changed

+81
-48
lines changed

main.py

Lines changed: 27 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,62 +13,35 @@
1313
# import zipfile as zf
1414
from tkinter.messagebox import *
1515
import 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()
4921
projectToLoad = "wait_gotoxy.sb3" # change this to load a different project
5022
targets, currentBgFile, project = s2p_unpacker.sb3_unpack(projectToLoad)
23+
for t in targets:
24+
allSprites.add(TargetSprite(t))
5125
wn = tk.Tk() # Start tkinter for popups
5226
wn.withdraw() # Hide main tkinter window
53-
allSprites = pygame.sprite.Group()
5427
# when needed
5528
# wn.deiconify()
5629
pygame.init() # Start pygame
5730
scratch.startProject()
5831
# Set player size
59-
HEIGHT = 720
60-
WIDTH = 960
32+
HEIGHT = 360
33+
WIDTH = 480
6134
projectName = projectToLoad[:-4] # Set the project name
6235
icon = pygame.image.load("icon.png")
6336
display = pygame.display.set_mode([WIDTH, HEIGHT])
6437
pygame.display.set_caption(projectName + " - Scratch2Python" )
6538
pygame.display.set_icon(icon)
66-
currentBg = loadSvg(currentBgFile)
39+
currentBg = scratch.loadSvg(currentBgFile)
6740
# currentBgFile = project.read(target["costumes"][target["currentCostume"]]["md5ext"])
6841
projectRunning = True
6942

7043
display.fill((255, 255, 255))
71-
setBackground(currentBg)
44+
scratch.setBackground(currentBg, display)
7245
while 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()

scratch.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,48 @@
11
# Scratch project functions
2-
3-
42
# Green flag event
53
import pygame.time
4+
import cairosvg
5+
import io
6+
HEIGHT = 360
7+
WIDTH = 480
8+
9+
10+
# Load SVG
11+
def loadSvg(svg_bytes):
12+
newBites = cairosvg.svg2png(bytestring=svg_bytes)
13+
byteIo = io.BytesIO(newBites)
14+
return pygame.image.load(byteIo)
15+
16+
17+
# Render a sprite at its coordinates
18+
def render(sprite, x, y, direction, display):
19+
# set direction
20+
sprite = pygame.transform.rotate(sprite, 90 - direction)
21+
# convert Scratch coordinates into Pygame coordinates
22+
finalX = x + WIDTH // 2 - sprite.get_width() // 2
23+
finalY = HEIGHT // 2 - y - sprite.get_height() // 2
24+
display.blit(sprite, (finalX, finalY))
25+
26+
27+
# Set the stage background
28+
def setBackground(bg, display):
29+
render(bg, 0, 0, 90, display)
630

731

832
def startProject():
933
print("DEBUG: Project start event")
1034

1135

1236
# Run the given block object
13-
def execute(block, target):
37+
def execute(block, s):
38+
# TODO: Multi-threading
1439
opcode = block.opcode
1540
id = block.blockID
1641
blockRan = block.blockRan
1742
inputs = block.inputs
1843
fields = block.fields
1944
if opcode == "motion_gotoxy":
20-
target.x = int(inputs["X"][1][1])
21-
target.y = int(inputs["Y"][1][1])
45+
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)

target.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
# TODO: Add pygame sprite for each target
21
"""
32
Target(sprite) class
43
54
======= CLASS INFO =======
65
The various files with classes are used by s2p_unpacker and the correct data is
76
set. Those are then used to build the project in main.py.
87
"""
9-
import pygame.sprite
108

119

1210
class Target:

targetSprite.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import pygame
2+
import cairosvg
3+
import io
4+
import scratch
5+
6+
7+
class TargetSprite(pygame.sprite.Sprite):
8+
def __init__(self, target):
9+
pygame.sprite.Sprite.__init__(self)
10+
self.target = target
11+
# load and upscale
12+
sprite = scratch.loadSvg(target.costumes[target.currentCostume].file)
13+
sprite = pygame.transform.rotate(sprite, 90 - target.direction)
14+
self.image = sprite
15+
self.rect = self.image.get_rect()
16+
# convert Scratch coordinates into Pygame coordinates
17+
self.rect.x = target.x + scratch.WIDTH // 2 - self.rect.width // 2
18+
self.rect.y = scratch.HEIGHT // 2 - target.y - self.rect.height // 2
19+
20+
def setXy(self, x, y):
21+
self.rect.x = x + scratch.WIDTH // 2 - self.rect.width // 2
22+
self.rect.y = scratch.HEIGHT // 2 - y - self.rect.height // 2

0 commit comments

Comments
 (0)