Discuss Scratch

MagicCrayon9342
Scratcher
1000+ posts

[v1.3-beta] sb3topy: Scratch to Python with Pygame

sb3topytoexe?
imfh
Scratcher
1000+ posts

[v1.3-beta] sb3topy: Scratch to Python with Pygame

MagicCrayon9342 wrote:

sb3topytoexe?
Yeah you could probably convert it to an exe fairly easily. I had plans to include a built in export as exe option, but it ended up being low priority. I’ll put it back on the todo list.
AXEstudios
Scratcher
100+ posts

[v1.3-beta] sb3topy: Scratch to Python with Pygame

Someone finally made it
lookupforcheese
New Scratcher
3 posts

[v1.3-beta] sb3topy: Scratch to Python with Pygame

This looks like a neat idea, and I would like to wish you the best of luck on this project.
DifferentDance8
Scratcher
1000+ posts

[v1.3-beta] sb3topy: Scratch to Python with Pygame

Gamer_Logan819 wrote:

mbrick2 wrote:

Wow!!!!

Now do python to scratch, lol
Like this person said, python to scratch would be far better. Considering that you should be able to plop in pygame code to turn into scratch code.
imfh
Scratcher
1000+ posts

[v1.3-beta] sb3topy: Scratch to Python with Pygame

DifferentDance8 wrote:

Gamer_Logan819 wrote:

mbrick2 wrote:

Wow!!!!

Now do python to scratch, lol
Like this person said, python to scratch would be far better. Considering that you should be able to plop in pygame code to turn into scratch code.
Python to Scratch is far more difficult because Python has more features than Scratch. In Python, you can create variables on the fly. How would you do that in Scratch? Pygame can also draw just parts of images and draw on images. You can’t do either of those to costumes with Scratch. In Python, you can also do stuff like open files and read JSON data from them.

I guess you could make a converter which just doesn’t work if you use any feature which isn’t in Scratch, but then it would fail for pretty much every Pygame project unless you specifically wrote it in a special way which doesn’t use any unsupported features.
scratchniceperson
Scratcher
100+ posts

[v1.3-beta] sb3topy: Scratch to Python with Pygame

How scratch can do python coding, scratch is for block coding??
CST1229
Scratcher
1000+ posts

[v1.3-beta] sb3topy: Scratch to Python with Pygame

scratchniceperson wrote:

(#27)
How scratch can do python coding, scratch is for block coding??
Scratch can't do “Python coding”, this tool is a Python program that converts Scratch projects to Python programs. This is not a Scratch project.
bloctans_4
Scratcher
1000+ posts

[v1.3-beta] sb3topy: Scratch to Python with Pygame

this is really cool

im also making a virtual machine/emulator for scratch. and block inputs/fields have been the hard part, how did you do it?
imfh
Scratcher
1000+ posts

[v1.3-beta] sb3topy: Scratch to Python with Pygame

bloctans_4 wrote:

this is really cool

im also making a virtual machine/emulator for scratch. and block inputs/fields have been the hard part, how did you do it?
This actually converts the project, so it's not exactly a vm/emulator. The way I handle inputs/fields is probably very different from how you need to.

I go through each of the field / inputs for the block, and I save them in a dictionary along with their type. For example, field, block, literal, variable. Fields are always just “field”. Inputs can have a variety of types, and I base the type on the number with the input (see here).

Once I have the inputs/fields and their types, I cast them to the type I expect them to be based on a map. For example, the “change variable by” block expects a variable name and a number. If an input is a block/variable, I put a “cast wrapper” around the code generated by the block, for example “int( … )”. I only put the wrapper if the block's return type is not the expect type.
scratchniceperson
Scratcher
100+ posts

[v1.3-beta] sb3topy: Scratch to Python with Pygame

CST1229 wrote:

scratchniceperson wrote:

(#27)
How scratch can do python coding, scratch is for block coding??
Scratch can't do “Python coding”, this tool is a Python program that converts Scratch projects to Python programs. This is not a Scratch project.

Thanks for telling
bloctans_4
Scratcher
1000+ posts

[v1.3-beta] sb3topy: Scratch to Python with Pygame

imfh wrote:

bloctans_4 wrote:

this is really cool

im also making a virtual machine/emulator for scratch. and block inputs/fields have been the hard part, how did you do it?
This actually converts the project, so it's not exactly a vm/emulator. The way I handle inputs/fields is probably very different from how you need to.

I go through each of the field / inputs for the block, and I save them in a dictionary along with their type. For example, field, block, literal, variable. Fields are always just “field”. Inputs can have a variety of types, and I base the type on the number with the input (see here).

Once I have the inputs/fields and their types, I cast them to the type I expect them to be based on a map. For example, the “change variable by” block expects a variable name and a number. If an input is a block/variable, I put a “cast wrapper” around the code generated by the block, for example “int( … )”. I only put the wrapper if the block's return type is not the expect type.
in speaking of how you did things, heres another thing im always banging my head on:
  • does your program properly center sprites
  • and in the case it does, how the heck do you do it

Last edited by bloctans_4 (Aug. 17, 2022 18:52:02)

bloctans_4
Scratcher
1000+ posts

[v1.3-beta] sb3topy: Scratch to Python with Pygame

also, just tested out the program, doesnt pygame support svg now?

also it wont detect cairosvg

Last edited by bloctans_4 (Aug. 17, 2022 19:38:29)

imfh
Scratcher
1000+ posts

[v1.3-beta] sb3topy: Scratch to Python with Pygame

bloctans_4 wrote:

in speaking of how you did things, heres another thing im always banging my head on:
  • does your program properly center sprites
  • and in the case it does, how the heck do you do it
Yes, it does. Here's pseudo code showing how I do it:
center = image.get_size() / 2
offset = Vector2(bitmapResolutionX, bitmapResolutionY)
offset = (center - offset) / bitmapResolution
 
offset = offset * costume_size/100
offset = offset.rotate(direction - 90)
rect = image.get_rect(
    center = offset + Vector2(x_pos + 240, 180 - y_pos))
I don't fully understand how it works anymore (I'm not sure if I ever did), and you might need to make some adjustments. I had to make adjustments accounting screen coordinates at different scales, which I left out of the pseudo code.

Relevant code:
Getting the center and scale
Initial offset calculations
Final rectangle calculations

bloctans_4 wrote:

also, just tested out the program, doesnt pygame support svg now?

also it wont detect cairosvg
Oh wow, it does! I'll have to make an update to use it. Yeah, installing CairoSVG properly is pretty difficult. It's one of the biggest flaws of the program.
bloctans_4
Scratcher
1000+ posts

[v1.3-beta] sb3topy: Scratch to Python with Pygame

so as a performance test i ran https://scratch-mit-edu.ezproxyberklee.flo.org/projects/659120206/ through the converter, and the results are:
the converting assets stage used up all my cpu
the actual project used all my memory
the project cant run in 1920x1080
bloctans_4
Scratcher
1000+ posts

[v1.3-beta] sb3topy: Scratch to Python with Pygame

that is probably all pygame's fault
bloctans_4
Scratcher
1000+ posts

[v1.3-beta] sb3topy: Scratch to Python with Pygame


i feel like this would be too hard to fix
bloctans_4
Scratcher
1000+ posts

[v1.3-beta] sb3topy: Scratch to Python with Pygame

another error with griffpatches gd: ERROR:root:Error in gathered task ‘<Task finished name=’Task-143' coro=<Spritemenu.broadcast_menuin1() done, defined at D:\sb3topy-1.1-beta\sb3topy-1.1-beta\outputs\project.py:2883> exception=NameError(“name ‘co nfig’ is not defined”)>'
Traceback (most recent call last):
File “D:\sb3topy-1.1-beta\sb3topy-1.1-beta\outputs\engine\util.py”, line 170, in _handle_tasks
task.result()
File “D:\sb3topy-1.1-beta\sb3topy-1.1-beta\outputs\project.py”, line 2898, in broadcast_menuin1
await util.sprites.broadcast_getrank(util)
File “D:\sb3topy-1.1-beta\sb3topy-1.1-beta\outputs\project.py”, line 3377, in broadcast_getrank
await self.my_TexttoNumber(util, config.USERNAME)
NameError: name ‘config’ is not defined
bloctans_4
Scratcher
1000+ posts

[v1.3-beta] sb3topy: Scratch to Python with Pygame


how did you fix this?
imfh
Scratcher
1000+ posts

[v1.3-beta] sb3topy: Scratch to Python with Pygame

bloctans_4 wrote:

so as a performance test i ran https://scratch-mit-edu.ezproxyberklee.flo.org/projects/659120206/ through the converter, and the results are:
the converting assets stage used up all my cpu
the actual project used all my memory
the project cant run in 1920x1080
Hmm, yeah that seems like a stress test of the asset conversion.

bloctans_4 wrote:

https://mv-ezproxy-com.ezproxyberklee.flo.org/get_image/.%2E/38373c8c5c08f9f4e7ae8b90e6e2dc40.png
i feel like this would be too hard to fix
Not necessarily… The colors are probably not too hard to fix. The positioning might be a bit harder though.

bloctans_4 wrote:

another error with griffpatches gd: ERROR:root:Error in gathered task ‘<Task finished name=’Task-143' coro=<Spritemenu.broadcast_menuin1() done, defined at D:\sb3topy-1.1-beta\sb3topy-1.1-beta\outputs\project.py:2883> exception=NameError(“name ‘co nfig’ is not defined”)>'
Traceback (most recent call last):
File “D:\sb3topy-1.1-beta\sb3topy-1.1-beta\outputs\engine\util.py”, line 170, in _handle_tasks
task.result()
File “D:\sb3topy-1.1-beta\sb3topy-1.1-beta\outputs\project.py”, line 2898, in broadcast_menuin1
await util.sprites.broadcast_getrank(util)
File “D:\sb3topy-1.1-beta\sb3topy-1.1-beta\outputs\project.py”, line 3377, in broadcast_getrank
await self.my_TexttoNumber(util, config.USERNAME)
NameError: name ‘config’ is not defined
It looks like the username block is broken. I'm not sure how that missed testing. It's an easy fix though.

bloctans_4 wrote:

https://mv-ezproxy-com.ezproxyberklee.flo.org/get_image/.%2E/2129bf83add22918656df9391f6e092d.png
how did you fix this?
Fix what?

Last edited by imfh (Aug. 21, 2022 18:17:46)

Powered by DjangoBB