How to Run Pygame or Pyglet in a Browser

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

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.

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.

Why is pygame's display module not working on repl.it?

The problem with Repl is, it doesn't provide you a video card or something. So unlike your own computer, if you try to run a gui program on repl, it will not work - be it Tkinter or Pygame. Unless, you actually intialize a Repl with Pygame support.

Let's talk about what's wrong with the basic python repl.

So, what happens when you try to call pygame.display.init() or pygame.display.set_mode((w,h))? It first tries to figure out the display driver of the device it is running on. Since, repl is giving you just a bash console and online ide, it is not very realistic to expect having a video driver. Hence, you get the error.

However, this raises another question. Can't you run pygame on repl at all? Well, the doc says-

Does not require a GUI to use all functions. You can use pygame from a command line if you want to use it just to process images, get joystick input, or play sounds.

Which means, you can still run pygame and do stuff like, taking input etc. on repl. But nothing related to display because of the absence of video card.

But as you mentioned, The module pygame.display is especially vital., you have only several ways - Install pygame on your local computer or try getting a server that provides you with a GUI.
Or if you want to stick with ReplIt, you can start a Pygame Repl which is installed as a seperate language and isn't supported by the usual Python3 repl.

Edit: Thanks to @CharlesDuffy for updating me with the repl support for Pygame

Making a 2d game in Python - which library should be used?

Pygame is just a wrapper for SDL, so technically a lot of games have been made using it, most of the work is done C-side anyways. It's a little slow, though if you are doing a 2D game, it should be sufficient. Frets-on-Fire was written in pure python with PyGame, and FoFiX was written in python with C extensions, and those games are both pretty great, altough on slower computers FoFiX can have trouble running 4 player.

Pyglet is also pretty nice, though I've only used it for emulator development, drawing single layers, so I don't know how well it actually works for game development. I've found that it's much easier to use direct OpenGL with Pyglet than it is with SDL/PyGame, so if you are doing a 3D game, it's probably better than using SDL. Also, since it's written in python, it's very easy to extend classes to change how a function is handled. For instance, I was trying to load data into an openGL texture through the pyglet Texture class, but the class couldn't load the data in a format I wanted, so I extended that class and changed the function that got the texture type (comes from a string) to include the texture I wanted, quick and painless.

One library I don't see mentioned enough is SFML. The 2.0 API (currently in beta) is pretty stellar and simple to use, and, if I remember correctly, runs a little faster than SDL. It is also more "object oriented" than SDL, which is really nice if you are working in an OO language like Python. It has some nice Python bindings written by Bastien Leonard in Cython that are pretty easy to use (or you can use the old C bindings if you don't want to use the most up to date version). I used this a while back and it was quite an enjoyable experience.

The reason why there aren't a lot of shipped games using Python is pretty simple: You can't close Python source. Even if you "compile" python into an executable using Py2Exe or something, it really just packs up the interpreter and python source into an exe file, and the source can be got just by opening up the exe in a hex editor.

While I'm a supporter of the "write it in Python, write time critical parts in C", if you feel python is going to be too slow, I would probably suggest C or, preferably, C++ as the way to go, simply because you don't get that much of a usability advantage for writing in Java, and the C languages can be quite a bit faster running than Java, and Lua will just take away some of the usability of Python without giving you any noticeable performance enhancement. SFML and SDL can both be used straight from C(++).

Advice if you do decide to use python: Use it as it is supposed to be used, as a scripting language. Do not try to bit twiddle or load images in pure python, either use the interfaces available in the library you choose (they all have very good interfaces for those kinds of things) or write an extension module to do that for you. Use python for logic control and fetching data from your libraries. If you follow that simple rule, you really shouldn't run into any performance issues.



Related Topics



Leave a reply



Submit