29

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

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

8 Comments

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.
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 )
this was a great answer and, 10 years later, still true... javascript is naturally for the browser, Python is not the natural choice.
@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.
@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...
|
7

Today, I'd recommand using 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.

Installation: pip3 install pygbag

Usage: 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 and you can also start 3D projects.

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.

Comments

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

Comments

3

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 Comment

wouldn't main be async in your 2nd codeblock
1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.