Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

pygame on browser - itch.io web - set custom FPS pygbag (windows)

NOTE - Two versions: pygbag and older pybag

-------------------------------------------------------------------------------------------------------

PRECHECK

[x] Add Python to environment variables PATH   (python installer)

py -m ensurepip

-------------------------------------------------------------------------------------------------------

Install git to get the latest version of pygbag (windows sdk debugger wrapper)      

py -m pip install git+github.com/pygame-web/pygbag --user --upgrade

(git+https://)

or normal install

py -m pip install pygbag --user --upgrade

-------------------------------------------------------------------------------------------------------

pygbag.exe - Add to PATH

py -m pip show pygbag
C:\Users\ (username) \AppData\Roaming\Python\Python313\Scripts\

WINDOWS system -> advanced system settings -> environment variables -> new or edit

separate PATH directories with ;

echo %PATH%

-------------------------------------------------------------------------------------------------------

Install WinDbg  (windows debugger)

Windows SDK Installer:  check "Debugging tools for Windows" (uncheck everything else)

Alternative 2: Microsoft Store:  WinDbg

-------------------------------------------------------------------------------------------------------

METHOD 1 (BAD) - clock.tick(FPS) - compensates frame rate with a blocking pause.

VSYNC drawing opportunities ->  refresh rate of the device.

import pygame
import asyncio
pygame.init()
screen = pygame.display.set_mode((1280, 720))
FPS = 48
clock = pygame.time.Clock()     
running = True
async def main():
    global running
    while running:
        screen.fill((255,255,255))
        pygame.display.flip()
        await asyncio.sleep(0) #RUN NORMAL VSYNC RENDER WITHOUT SLEEP 0
        clock.tick(FPS)        #JAVASCRIPT BLOCKED
    pygame.quit()
asyncio.run(main())

-------------------------------------------------------------------------------------------------------

METHOD 2 (BETTER) - compensate frame rate directly using asyncio.sleep(). Non-blocking.

Uniform game speed on different platforms. Synced with the javascript engine.

import asyncio
import pygame
pygame.init()
screen = pygame.display.set_mode((1280, 720))
FPS = 48
FRAME_TIME = (1 / FPS) * 1000         #MILLISECONDS TICKS FORMAT
running = True
start_time = pygame.time.get_ticks()
async def main():
    global running
    global start_time
    while running:
        screen.fill((0, 0, 0))
        pygame.display.flip()
        elapsed_time = pygame.time.get_ticks() - start_time
        # max: set zero if value is negative
        # /1000 convert to seconds
        await asyncio.sleep((max(0, FRAME_TIME - elapsed_time))/1000) 
        start_time = pygame.time.get_ticks()   #Full frame cycle
    pygame.quit()
asyncio.run(main())

-------------------------------------------------------------------------------------------------------

Run game -  main.py file inside folder.

py -m pygbag videogamefolder     

-------------------------------------------------------------------------------------------------------

localhost:8000                  (works on firefox)

localhost:8000?-i             (debug information)

localhost:8000#debug

Support this post

Did you like this post? Tell us

Leave a comment

Log in with your itch.io account to leave a comment.