pyodide-kernel icon indicating copy to clipboard operation
pyodide-kernel copied to clipboard

pygame-ce

Open westurner opened this issue 1 year ago • 5 comments

Problem

  • the pygame-ce package is in pyodide but,:
    • https://pyodide.org/en/latest/usage/packages-in-pyodide.html
    • https://github.com/pyodide/pyodide/issues/289
  • https://pyga.me/docs/ > # Example file showing a circle moving on screen doesn't yet work if pasted into a JupyterLite notebook cell

Proposed Solution

  • how to run pygame-ce with JupyterLite notebooks

Additional context

westurner avatar Sep 20 '24 02:09 westurner

did you try that loop model instead ? https://pygame-web.github.io/wiki/python-wasm/#main-loop-example-must-be-asynchronous

pmp-p avatar Sep 20 '24 05:09 pmp-p

This doesn't seem to work:

# Example file showing a circle moving on screen, modified  
import asyncio
import pygame

# pygame setup
pygame.init()

screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock()
running = True
dt = 0

player_pos = pygame.Vector2(screen.get_width() / 2, screen.get_height() / 2)

async def main():
    global running
    while running:
        # poll for events
        # pygame.QUIT event means the user clicked X to close your window
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
    
        # fill the screen with a color to wipe away anything from last frame
        screen.fill("purple")
    
        pygame.draw.circle(screen, "red", player_pos, 40)
    
        keys = pygame.key.get_pressed()
        if keys[pygame.K_w]:
            player_pos.y -= 300 * dt
        if keys[pygame.K_s]:
            player_pos.y += 300 * dt
        if keys[pygame.K_a]:
            player_pos.x -= 300 * dt
        if keys[pygame.K_d]:
            player_pos.x += 300 * dt
    
        # flip() the display to put your work on screen
        pygame.display.flip()
    
        # limits FPS to 60
        # dt is delta time in seconds since last frame, used for framerate-
        # independent physics.
        dt = clock.tick(60) / 1000

asyncio.run(main())
# pygame.quit()
  • [ ] Create a pygame.ipynb example notebook

westurner avatar Sep 21 '24 04:09 westurner

Is is very unlikely to work in jupyterlite. To run pygame in the browser you likely need to run pyodide in the main-thread. In lite, kernels typically run in a webworker, therefore this will not work.

There might be a theoretical solution using an offscreeen-canvas, but even that is likely not working

DerThorsten avatar Jun 04 '25 08:06 DerThorsten

Which rendering engine would work better with pyodide? Something wgpu-based?

There are many educational resources for pygame, which would work in pyodide on Chromebooks

On Wed, Jun 4, 2025, 4:38 AM Thorsten Beier @.***> wrote:

DerThorsten left a comment (jupyterlite/pyodide-kernel#195) https://github.com/jupyterlite/pyodide-kernel/issues/195#issuecomment-2939133469

Is is very unlikely to work in jupyterlite. To run pygame in the browser you likely need to run pyodide in the main-thread. Pyodide is running in a webworker therefore this will not work.

There might be a theoretical solution using an offscreeen-canvas, but even that is likely not working

— Reply to this email directly, view it on GitHub https://github.com/jupyterlite/pyodide-kernel/issues/195#issuecomment-2939133469, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAMNSZNZSIIETODF2NDHK33B2V77AVCNFSM6AAAAAB6R4DW6OVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDSMZZGEZTGNBWHE . You are receiving this because you authored the thread.Message ID: @.***>

westurner avatar Jun 04 '25 16:06 westurner

Which rendering engine would work better with pyodide? Something wgpu-based?

There are many educational resources for pygame, which would work in pyodide on Chromebooks

its not about pyodide itself. In jupyterlite the kernels run in a webworker. That makes things more complicated

DerThorsten avatar Jun 04 '25 17:06 DerThorsten