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

4

6

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 ?

Carroty answered 10/5, 2022 at 6:27 Comment(0)
D
7

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.

Dorisdorisa answered 10/5, 2022 at 16:57 Comment(1)
The list for packages supported with pyodide is here: pyodide.org/en/stable/usage/packages-in-pyodide.htmlPhysician
R
2

Yes it's possible but has not been made available because :

pyscript uses pyodide, pyodide is NOT optimized for games but for scientific stack instead and notebooks presentations.

Today, i'd recommand using pygbag https://pypi.org/project/pygbag/ from https://pygame-web.github.io. It uses the same principles applied to pygame coming from Panda3D webgl port using Web Assembly for modern browsers.

( and if you really want to support pyscript tags you could probably find a way ).

Reply answered 6/8, 2022 at 10:52 Comment(0)
N
1

You can use PygBag do do so. Here is the link to PygBag : PygBag And here a link to an article: Pygame in a Browser

Nuts answered 16/3 at 14:10 Comment(1)
Might want to add "update: 2024" to your answer, so users can recognize it (as a later answer).Aromaticity
S
0

You can't use most of pygame anyway because pyscript will only render python once, so you can't do stuff like loops which are necessary for pygame to do stuff like change frames.

Scavenger answered 25/4, 2023 at 23:9 Comment(1)
Not true: it does support dynamic content. One of the examples on their website demos a clock using a while loop: pyscript.net/examples/simple_clock.html. Perhaps you meant something specific to graphical rendering or something that I'm not , so it would be better if you could add details that were more specific to what you meant. Cheers!Etna

© 2022 - 2024 — McMap. All rights reserved.