28

I have 3 game libraries installed on my PC: pyglet, pygame and Panda3D.

I would like to create a 2D game and make it a web browser game so i can put it on facebook.

I know that Panda3D has a web-browser-plugin. Panda3D is for 3D games mainly.

Therefore, I'm asking, is it possible to play a pyglet or pygame game in a browser? If not, what Python library do you recommend?

3

6 Answers 6

10

Neither pyglet nor pygame will run in a browser. I wouldn't really recommend using Python at all if you target is a web browser. JavaScript (with HTML5 Canvas), Flash, or Java applets are is better suited for that environment.

If you're dedicated to the idea of using Python, there are a number of projects that can compile Python into JavaScript. There are some mentioned on the Python wiki. Here are a few:

You'll need to write your own graphics and audio systems, though, since none of those projects can convert the native code needed by pyglet and pygame into JavaScript.

8
  • 3
    Ah, you might be interested in Jython, then. I didn't think about it yesterday. A search for "jython applets" will lead you to some information on the subject.
    – Snowball
    Commented Dec 27, 2011 at 8:37
  • 1
    the goal is running in the browser, not running python in javascript. There are efficient alternative to javascript in order run cpython in the browser : like asm.js or webassembly ( which is now supported officialy by cpython )
    – Pmp P.
    Commented Oct 16, 2023 at 12:41
  • this was a great answer and, 10 years later, still true... javascript is naturally for the browser, Python is not the natural choice.
    – darren
    Commented Nov 6, 2023 at 7:52
  • @D.L The question is not about natural choice or your personnal feelings about javascript. The question is running Python ( the interpreter) + pygame + SDL1 or 2 and rendering a 2D game ( that means with graphic at 60FPS, multitouch+gamepad and proper sounds yes that include WAV and MP3 ) not some half baked js solution that will never get it 100% right and cannot decode half the codecs around. btw i'm a pygame-ce maintener and i think i've tried all available solutions around and none would fit until asm.js/WASM came.
    – Pmp P.
    Commented Nov 22, 2023 at 9:22
  • @PmpP. instead of having a go at people, suggest an answer. I can achieve this is javascript, but not in python. So if you have an answer then post it. I would be interested and really pleased to see a good answer / solution...
    – darren
    Commented Nov 22, 2023 at 9:29
5

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

-- edit 22 nov 2023 --

installation is simple : pip3 install pygbag

using it : python3 -m pygbag your_game_folder/main.py

with pygbag tool you can host/run/package directly your pygame-ce/panda3D desktop project in all modern browsers ( and Safari iOS>=15 )

it is already widely adopted for 2D game jams https://itch.io/c/2563651/pygame-wasm and you can also start 3D projects https://itch.io/c/3724091/panda3d-wasm

You can also use pygame-ce wasm in REPLit with pygbag to get a performance gain ( people using it said it makes a big difference ) for that see https://github.com/pygame-web/pygbag/issues/110

4

It requires a bit of reprogramming, but i made a pygame library "port" to the browser/nodewebkit using Brython and GameJS. You can program using a version of pygame and python 3 in the browser. You can check it out at https://github.com/asherwunk/pygjs

3

An option is to use repl.it. It allows creating Python/PyGame scripts and multiplayer coding. When creating a new repl, select the Pygame template:


Example: repl.it/@Rabbid76/PyGame-TransparentShapes

0
2

Another option to run pygame in a browser is to use pygbag. Install pygbag with pip install pygbag

pip install pygbag

Put the code in a file named main.py and adapt your pygame code like this:

import pygame
import asyncio

pygame.init()

# initializations: pygame.display.set_mode, ...
# [...]

def main():

    run = True
    while run:

        # application loop: pygame.event.get(), pygame.display.flip(), ...
        # [...]

        await asyncio.sleep(0)
        
asyncio.run(main())

Build the project with the command:

pygbag .

Minimal example:

Live demo

import pygame
import asyncio

pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()

rect = pygame.Rect(0, 0, 20, 20)
rect.center = window.get_rect().center
vel = 5

async def main():
    run = True
    while run:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            if event.type == pygame.KEYDOWN:
                print(pygame.key.name(event.key))

        keys = pygame.key.get_pressed()
        
        rect.x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * vel
        rect.y += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * vel
            
        rect.centerx = rect.centerx % window.get_width()
        rect.centery = rect.centery % window.get_height()

        window.fill(0)
        pygame.draw.rect(window, "red", rect)
        pygame.display.flip()
        await asyncio.sleep(0)

    pygame.quit()
    exit()

asyncio.run(main())
1
  • wouldn't main be async in your 2nd codeblock
    – Blue Robin
    Commented Jan 31 at 3:03
-2

I just found this website https://trinket.io/features/pygame that allows you to run pygame from this website! But it's extremely slow. It should work from your browser but it'll take forever to run it.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.