The inability of the skulpt interpreter to provide support for pygame and sqlite3 is what caused my selection into this year's GSoC program, well mostly(😉). One of my major under-takings this summer is to make the Pygame lectures interactive so that students may test their code on the go as they learn, which is the case for earlier exercises. This blog describes in detail how I reached to the solution(for now 😁) and what is left to be done.
PYGJS to the rescue
After several unsuccessful attempts of making the old brython fork work, I decided it was time I tried something else. It was then I came across this abandoned fork of pygjs. This library was originally intended to serve the same purpose I was currently looking for, making Pygame work in the browser. Initially it seemed liked the previous brython fork that I had to deal witth, but as I dove deeper into the code I found that this library uses gamejs along with brython for its implementation. Now, all that was left was to find a way to make use of this library considering it worked(😅).
Nothing is as easy as it seems
Although pygjs proved to be a very good option there will still some problems that need to be sorted before it could be used in the lectures. The first problem I faced was due to the project being abandoned. Though most of the code was usable, there were changes or modifications required to make the library behave as it was expected. For example, there were bugs in the key and mouse modules of pygjs that were causing unexpected behaviour. Secondly, the library needed to be used in a specific way . What I mean here is running some very basic instructions in brython like endless while loops, caused the JS thread to be blocked and the crashed the window. This problem was solved using timer.set_interval function offered by brython. One of the other problems was speed. Brython allows import of packages if they have a __init__.py present. But importing packages in this way in the browser makes the loading very slow. To solve this I had to package the pygame module into a pygame.brython.js file. This made loading the pyagame package mush simpler and faster. This PR serves as the final POC for the pygame wrapper and can be used to develop the exercises later.
Does it work?
I think it's safe for to say that Yes, it works for now and can be used to teach Pygame to the students. I have tried using all of the functionalities that may be required to teach Pygame to beginners and this library handled them all, starting from basic drawing to making animations and to playing music in the browser. You can check this out for yourself here: http://pyar.github.io/PyZombis/222/index.html .