Draft
Refactor web globals for pygbag WebAssembly compilation#65
Conversation
…y imports Co-authored-by: durangogt <10979447+durangogt@users.noreply.github.com>
Co-authored-by: durangogt <10979447+durangogt@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Refactor global variables to main.py
Refactor web globals for pygbag WebAssembly compilation
Feb 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pygbag requires all global variables declared at module level before
asyncio.run(main())for optimal WebAssembly compilation. Previously, 65 globals were scattered acrossweb/aggravation_web.pywith some initialized inside therun()function.Changes
web/main.py
asyncio.run(main()):BOARD_TEMPLATE,FPS,WINDOWWIDTH, color definitions, etc.P1END,P2END,P3END,P4ENDNone(FPSCLOCK,DISPLAYSURF,BASICFONT, button surfaces/rects)web/aggravation_web.py
This follows the pygbag recommended pattern: single source of truth for globals in entry point, initialized upfront for efficient WebAssembly compilation.
Original prompt
Summary
Per pygbag's documentation and best practices, all global variables should be declared at once at the module level in the
main.pyentry point file, beforeasyncio.run(main())is called. This improves WebAssembly compilation performance and ensures globals are efficiently initialized upfront.Currently, the aggravation web version has its globals scattered:
web/aggravation_web.py(lines 50-123):BOARD_TEMPLATE,PLAYER_COLORS,P1END-P4END,FPS,WINDOWWIDTH,WINDOWHEIGHT,REVEALSPEED,SIMSPEED,BOXSIZE,GAPSIZE,BOARDWIDTH,BOARDHEIGHT,BASICFONTSIZE,BLANK,SPOT,XMARGIN,YMARGIN, all color constants (GRAY,NAVYBLUE,WHITE,RED,GREEN,BLUE,YELLOW,ORANGE,PURPLE,CYAN,BLACK), UI color aliases (BUTTONCOLOR,BUTTONTEXTCOLOR,MESSAGECOLOR,TILECOLOR,TEXTCOLOR,BGCOLOR,LIGHTBGCOLOR,BOXCOLOR,HIGHLIGHTCOLOR), and player colors (P1COLOR,P2COLOR,P3COLOR,P4COLOR).run()function withglobaldeclarations (lines 207-210):FPSCLOCK,DISPLAYSURF,BASICFONT,ROLL_SURF,ROLL_RECT,ROLL1_SURF,ROLL1_RECT,EXIT_SURF,EXIT_RECT,OPTION_SURF,OPTION_RECT,CLEAR_SURF,CLEAR_RECT,ROLL6_SURF,ROLL6_RECT,PLAYERROR_SURF,PLAYERROR_RECT,CLEARERROR_SURF,CLEARERROR_RECT,TEST_SURF,TEST_RECT.web/main.pyis minimal (only imports asyncio and aggravation_web, callsrun()).Changes Required
1.
web/main.py— Declare all globals at module levelRestructure
web/main.pyto follow the pygbag pattern. All global variables that are currently inweb/aggravation_web.pyat module level (lines 50-123) should be moved intoweb/main.pyand declared beforeasyncio.run(main()). Specifically:main.py:BOARD_TEMPLATE,FPS,WINDOWWIDTH,WINDOWHEIGHT,REVEALSPEED,SIMSPEED,BOXSIZE,GAPSIZE,BOARDWIDTH,BOARDHEIGHT,BASICFONTSIZE,BLANK,SPOT,XMARGIN,YMARGINGRAY,NAVYBLUE,WHITE,RED,GREEN,BLUE,YELLOW,ORANGE,PURPLE,CYAN,BLACKBUTTONCOLOR,BUTTONTEXTCOLOR,MESSAGECOLOR,TILECOLOR,TEXTCOLOR,BGCOLOR,LIGHTBGCOLOR,BOXCOLOR,HIGHLIGHTCOLORP1COLOR,P2COLOR,P3COLOR,P4COLOR,PLAYER_COLORSP1END,P2END,P3END,P4ENDNoneso they exist at module scope:FPSCLOCK,DISPLAYSURF,BASICFONT,ROLL_SURF,ROLL_RECT,ROLL1_SURF,ROLL1_RECT,EXIT_SURF,EXIT_RECT,OPTION_SURF,OPTION_RECT,CLEAR_SURF,CLEAR_RECT,ROLL6_SURF,ROLL6_RECT,PLAYERROR_SURF,PLAYERROR_RECT,CLEARERROR_SURF,CLEARERROR_RECT,TEST_SURF,TEST_RECTThe file should follow this structure:
2.
web/aggravation_web.py— Import globals from main instead of declaring themmain.py__main__or use a shared module pattern. The simplest approach: import the globals frommainmodule. Since pygbag loadsmain.pyas__main__, use:from __main__ import *or explicitly import each needed global.main.pyand haveaggravation_web.pyimport them withimport __main__and reference them as__main__.VARIABLE_NAME, OR simplyfrom __main__ import BOARD_TEMPLATE, FPS, WINDOWWIDTH, ...etc.globaldeclarations insiderun()(lines 207-210) should still useglobalkeyword since those variables (FPSCLOCK, DISPLAYSURF, etc.) need to be written to at module scope, but now they should reference the pre-declaredNonevalues frommain.py.aggravation_web.pymust still resolve properly.Important Notes
game_engine.pyoraggravation.py— only changeweb/main.pyandweb/aggravation_web.pyasyncio.run(main())call MUST remain the last line in `web/mai...This pull request was created from Copilot chat.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.