Pygame for web?
One day I had a thought - is it possible for pygame to run in a browser? I googled and it turned out that it is. Pyjsdl is a project that converts a pygame-based code from python to javascript. Here's my mini review of it.
Performance
The main thing I was concerned about was performance. I made a program that draws a 20x20 pixel sprite to a 600x600 screen at random coordinates and counts frames per second. It also increases sprite count by one each frame. I did the same thing for the pure python version of the program.
Here's the video of the comparison:
Pyjsdl ran slower than the regular pygame (as expected). It dropped down below 30 FPS as quickly as around 150 sprites mark. I ran the test on Firefox 57 and my old i3 machine. It performs a little better on Chrome though.
Here's the link to the pyjsdl version: test it yourself.
Pros and cons
Pros:
- It allows to run pygame code in the browser.
- The size of compiled code is relatively small.
Cons:
- It is slow, slower than the original pygame (which is already slow).
- It "emulates" only a subset of pygame - some features (e.g. custom events) are not available.
- The code has to be refactored to work. Basically, you can't have an endless game loop and need to preload all the images before the game starts.
- Debugging compiled code is impossible.
- It is based on an currently unmaintained (last commit in 2015) project pyjs (formerly pyjamas), that is a python implementation of JWT.
- Pyjsdl was developed by one person and also seems to be unmaintained.
- Pyjsdl depends on some legacy libraries (e.g.: GTK2) that can be a real pain to install, especially for Windows users.
Conclusion
If you want to quickly put your pygame prototype on the web to show your friends, pyjsdl may do the work (you have to refactor you code though). It is VERY slow, but pygame was not desinged for production anyway.
As far as I know, there were no other attempts made to compile pygame to javascript, so I appreciate the work that the author put into this project.
There is also a project that has ported python standard library to run in the browser: skulpt.org. No pygame support, though.
Continued in part two