is it possible to run pygame or pyglet in a browser?
Asked Answered
M

6

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?

Medico answered 9/12, 2011 at 23:20 Comment(3)
Not sure about pyglet or pygame. Probably not. But you might want to check out (pyjs.org)Hatchett
e.g.: replit.comExtrabold
both pygame and Panda3D can now run in the browser for pygame see github.com/pygame-web ( demo pmp-p.github.io/pygame-wasm/ ) and for panda3d see rdb.name/panda3d-webgl.md.html ( demo rdb.name/panda3d-webgl/editor.html )Punishable
E
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.

Ethereal answered 26/12, 2011 at 9:2 Comment(8)
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.Ethereal
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 )Punishable
this was a great answer and, 10 years later, still true... javascript is naturally for the browser, Python is not the natural choice.Halfslip
@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.Punishable
@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...Halfslip
well i guess you did not see that the answer is there since the 6 august 2022, use pygbag the wasm packaging from pygame-web the endorsed project from pygame-ce . sorry for the harsh comment then and go enjoy some games here itch.io/c/2563651/pygame-wasmPunishable
and now pyglet is half supported if someone really need itPunishable
Skulpt link doesn't workProtrusive
P
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

Punishable answered 6/8, 2022 at 10:41 Comment(0)
T
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

Tent answered 27/6, 2017 at 16:33 Comment(0)
E
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

Extrabold answered 19/2, 2022 at 18:9 Comment(0)
E
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())
Extrabold answered 28/1 at 14:40 Comment(1)
wouldn't main be async in your 2nd codeblockProtrusive
B
-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.

Blue answered 4/4, 2022 at 0:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.