是否可以使用Pyscript在浏览器上使用PyGame进行游戏?

发布于 2025-01-27 14:19:19 字数 76 浏览 2 评论 0原文

我已经使用Pygame制作了一个小型太空入侵游戏,我想知道是否可以使用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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

只为一人 2025-02-03 14:19:19

不,目前在Pyscript中不支持Pygame。我不确定哪些最好的方法是找出哪些包装 支持,但是我设法将以下内容拼凑在一起:

  1. Pyscript使用 pyodide 加载软件包,因此只有pyodide支持的软件包才能在Pyscript中加载。这意味着要么用泊迪德(Pyodide)或纯Python包装的软件包,其中带有PYPI或其他URL上可用的车轮。
  2. pygame尚未得到pyodide的支持。
    编辑: pygame被合并于毕林德,将在以后的发行版中

您可以使用以下测试脚本来查看是否支持软件包:

<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>

这基本上只是一个“尝试并查看发生的事情”脚本,如果不支持软件包,它将在控制台中丢弃错误。您会看到的错误将是

valueerror:找不到“ pygame”的纯python 3轮。你可以
使用micropip.install(...,keep_oning = true)获取所有列表
带轮子的包装。

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.

趴在窗边数星星i 2025-02-03 14:19:19

是的,这是可能的,但尚未获得,因为:

Pyscript使用陀螺赛,倍陀在游戏中不是优化的,而是用于科学堆栈和笔记本电脑的演示文稿。

今天,我建议使用pygbag https://pypi.org/project/project/pygbag/ 来自 https://pygame-web.github.io 。它使用适用于来自PANDA3D WebGL端口的Pygame的相同原理,使用用于现代浏览器的Web组件。

(而且,如果您真的想支持Pyscript标签,则可能会找到一种方法)。

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 ).

鹿港巷口少年归 2025-02-03 14:19:19

您可以使用pygbag做到这一点。
这是pygbag的链接: pygbag
在这里链接到一篇文章:浏览器中的pygame

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

×纯※雪 2025-02-03 14:19:19

无论如何,您都不能使用大多数Pygame,因为Pyscript只会渲染Python一次,因此您不能做诸如Pygame进行更改帧之类的事情所需的循环之类的事情。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文