New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pygame for web browser #718
Comments
This would be a game changer (no pun intended) for education, as students will want to easily share their games with friends. The web is the ideal distribution platform due to its low barrier of entry. Also as online IDEs such as Repl.it become more popular, they should be able to serve as a fantastic education platform that anyone can use. I see the most potential in a WASM solution -- skulpt and similar projects have the issue of needing to implement the entire pygame API, and will always be behind (as you can see, not all of pygame is implemented). Since both CPython and SDL2 are working on emscripten, there should feasible path to this (not sure about audio, though?) |
This would be very helpful! I don't use Pygame very much, but when I think of anything related to WASM / programs usually on PC in the web browser, I think of Pygame. All 4 of the other engines/libraries I have used already have it either through emscripten or their own compilation (Raylib, SDL2, libGDX, Unity). |
This would be extremely beneficial for Play to Earn cryptocurrency game development. Many Play to Earn cryptocurrency games are taking off right now and will dominate 2022. |
https://github.com/ethanhs/python-wasm is especially promising, as it is just a bunch of shell scripts that compile regular CPython without any patches! |
@robertpfeiffer: In my experience, combining any of those "compile X to WASM+Emscripten" projects are basically unusable for combining with any project (they're just REPLs and can't really be expanded upon to provide graphics etc. - I tried to add some graphics on top of such a Lua project) |
AFAIK getting CPython to compile with emscripten was the hard part all along. SDL2 already supports emscripten, so we have graphics, we just need to put it together. |
no there are projects already very advanced.
indeed, but it won't support threading/socket/remote IO unless all your python code is fully async and handle all those if you stay on browser thread. wasm WILL get better with cpython 3.11. as it builds fine and can run in a web worker But there are still things to tinker to get all SDL2 running from inside the worker i managed to init SDL2 along with pymain 3.11 yesterday in a worker so i'll try to build pygame soon. sadly pygame does not access SDL2 via ffi/ctypes, that complicates things a lot and makes everything huge. |
this would be very useful! |
when regenerating C sources using cython 3.0.0dev, it can be built against python3.11a5 with minor modifications, main one is force-removing v4l2 because it thinks it's Linux next step is to gather all objects files and build a static module to link against libpython ( that's how Panda3d does ) with a custom main to call the module init. |
it's alive, for now set_mode, 2D draw primitives, font rendering/mouse seem to work sprites/sound/keyboard not yet tested or working https://github.com/pmp-p/pygame-wasm/tree/pygame-wasm for those insterested by this very WIP, testing happens on pygame._sdl2 of Pygame Community discord https://discord.gg/ZjxmYHzXV6 requires Linux env + emscripten sdk portable + https://github.com/pmp-p/pydk (wasm arch ) + https://github.com/ethanhs/python-wasm |
i'll try to put WIP demo each time a step is reached here step 0 for now :
|
since most of wasm port is working, i'll begin to upstream changes so more people can test it and find corner cases. |
my dumb checker missed one because of the newline at 80cols. following pygame#3080 (comment) for pygame#718 @dr0id indeed, the real C parser is the way !
this should be harmless but helps fixing static build for pygame#718.
next steps are :
|
The base port is ready when cumulating PR listed above, so next step is intensive testing of Pygame functions. socket support + basic I/O are outside Pygame code scope |
newer demos added here https://pmp-p.github.io/pygame-wasm basically juste have
that will turn into
the setup/loop scheme i came from is taken from Arduino platform . |
and now thanks to (very) basic asyncio support :
|
notes: using lzma compression the webloader ( ie python+pygame + minimal stdlib for startup including asyncio and debug/trace tools ) emsdk does not provide lzma emsdk requires python 3.8+ at least SDL2 rwops cannot read from anything else but a real file unless Line 60 in d965bf6
Line 475 in d965bf6
are fixed to use function pointer with void return values |
pygame sprite module in action thx @GuilhermeJuventino : sidescrolling and sdl2.Window by @CAPTAIN1947 : https://pmp-p.github.io/pygame-wasm/pygame.html?gh.captain1947.flappybird |
now with automated CI publishing for github https://github.com/pmp-p/python-wasm-plus/blob/main/support/__EMSCRIPTEN__.yml and the result: https://github.com/CAPTAIN1947/Flappy-Bird/tree/gh-pages note : won't be public until you add the missing "index.html" that maybe could load the game in an iframe. |
added https://pmp-p.github.io/pgzero-wasm/pygame.html?gh.lordmauve.pgzero (should) support pygame zero scripts without modification. this time a dynamic module is used and loaded : also feature interpreter recycling, you can now change program without reloading ( that's a step forward to running testsuite ) Pygame for web browser was featured at PyCon US 2022 see the slides https://twitter.com/ChristianHeimes/status/1519409483205685250 |
this should be harmless but helps fixing static build for pygame#718.
code changes have been merged, now static building is possible. But to reach fully automated wasm artefacts there are still some steps:
|
This comment was marked as spam.
This comment was marked as spam.
And finally with less than 10 lines modification : |
and a promising CI system ( fast ! ) to review : |
CI is now running fine now with 3.10.5 backport, it was possible to import numpy (built from pyodide and got from their CDN) A game using that https://pmp-p.github.io/pygame-wasm-plus/python310.html?beatgame thx @bydariogamer & @Sogolumbo |
Is it useable with pyscript? |
@Python-Ninja-Hebi probably when pyscript is rebuilt against pyodide 3.11 i will probably add dynamic loading, please follow pyodide/pyodide#289 Be aware that https://pygame-web.github.io is more performance oriented and could execute pyscript tags quite easily pygame-web/pygbag#24 while still using cpython upstream. |
not really related to the port, but interesting enough, mypyc wasm output is quite good and so pygame in the browser could run slightly faster than uncompiled native ( on fibonaci around 80% faster ) |
While waiting for pyodide modules and its core to bump to 3.11 ( should be in 3 months), since pyscript cannot load pygame and also cannot run on old browsers like on a plethora of mobile devices stuck without updates. I've made a "pygame-script" based upon pygbag runtime that don't use bigints and can run on mobile starting with android 4.4 and chrome/bromite 81.0.4044.83 demo: https://pygame-web.github.io/showroom/pygame-scripts/org.pygame.touchpong.html doc: https://pygame-web.github.io/wiki/pygame-script/ Page with QR codes: https://pmp-p.itch.io/ pygame scripts files can also run on desktop with "python3 -x" using the old ms-dos shebang hack incorporated long ago in cpython. |
dynamic loading is ready pending the linked above PR. next step is to change bdist_wheel so it only includes pygame_static library which wrap all .so in only one. pypa/installer is a suitable tool for installing that kind of wheels directly in browsers. side note : now black https://github.com/psf/black/releases/tag/22.10.0 supports "-x" like python and can format python scripts that have an html header |
Has work on bringing pygame to the web browser stalled? |
Not at all, @pmp-p is the mastermind of it https://github.com/pygame-web/pygbag |
@ryansobol now it is more a matter of distribution - pypa is not ready for wasm yet - so as linked above your best bet is on pygbag and the small custom repo hosted on github pages. The wheel is here https://github.com/pygame-web/archives/blob/main/repo/pkg/pygame_static-1.0-cp311-cp311-wasm32_mvp_emscripten.whl and a suitable runtime aware of wasm wheels is here https://pygame-web.github.io/showroom/python.html ( same used in pygbag or cpython web tetsuite ) |
@pmp-p This is great information, thank you! I'm curious, how does this wheel fit in with the Pyodide runtime? FYI, the Pyodide team is currently working on an upgrade to Python 3.11, which is likely to be released in v0.23. When this work is complete, do you expect this wheel be runnable in Pyodide? |
No i don't for multiple reasons, They most likely won't add that excess weight on standard release. But some people have already forked pyodide and added SDL2 support so it should be possible to switch runtimes eg in pyscript and load wheels. second is that pyodide compatibility with wasm standard is quite poor ( lots of extensions are used ), while the pygame wheel linked above is made for wasm 1.0 ( chrome 81 no threads ). So pyodide or its forks would probably require at least a wheel using "BigInt" extension ( and test simmd/neon accel btw ). third, game loop would not be optimal in pyodide ( setTimeout instead of requestAnimationFrame on vsync ). Meanwhile pygbag's goal is game engines and their specific needs, use stock libpython and stick to wasm standards. |
There are various python and pygame on web browser projects.
The text was updated successfully, but these errors were encountered: