pythonpython-3.xpygamepyscript

Is it possible to run game made with pygame on browser using pyscript?


I have made a small space invader game using pygame and I was wondering if I could play it on the browser using pyscript. Is this even possible ? Do I have to rewrite everything ?


Solution

  • No, Pygame is not supported in PyScript at this time. I'm not sure what is the best way to find out what packages are supported, but I have managed to piece together the following:

    1. PyScript uses Pyodide to load packages, so only packages supported by Pyodide will be loadable in PyScript. That means either packages built with Pyodide or pure Python packages with wheels available on PyPI or from other URLs.
    2. Pygame is not yet supported by Pyodide.
      Edit: Pygame has been merged in Pyodide and will be available in a future release.

    You can use the following test script to see if a package is supported:

    <html>
        <head>
          <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
          <script defer src="https://pyscript.net/alpha/pyscript.js"></script>
          <py-env>
            - pygame
          </py-env>
        </head>
    
      <body>
        <h1>PyScript import test script</h1>
        <py-script>
    import pygame
    
        </py-script>
      </body>
    </html>
    

    This is basically just a "try it and see what happens" script that will throw an error in the console if a package is not supported. The error you'll see will be

    ValueError: Couldn't find a pure Python 3 wheel for 'pygame'. You can use micropip.install(..., keep_going=True) to get a list of all packages with missing wheels.