Game Libraries#
WebTigerPython supports two libraries for programming games. Both draw into their own tab of the canvas area.
Note
gamegrid, pygame and gpanel can not be used together in the same program, and programs using them can not be run in the debugger.
GameGrid (beta)#
GameGrid is the game library known from TigerJython, in which games are built from actors on a grid of cells. It is available since version 1.3.0.
from gamegrid import *
makeGameGrid(10, 10, 60, Color.red)
addActor(Actor("sprites/pony_1.gif"), Location(2, 4))
show()
doRun()
The sprites and sounds known from TigerJython (e.g.
sprites/pony_1.gif) are bundled with WebTigerPython (public/assets/jython_assets/). Own images can be added to the workspace (see Multi-File System).Sound and pixel-accurate collision detection are supported since version 1.5.0.
Further examples can be found here
Pygame#
Pygame is supported with the pygame-ce package of Pyodide, which is installed automatically the first time a program imports pygame.
Note
pygame-ce (Pygame Community Edition) is a fork of Pygame that is supported by Pyodide. It is largely compatible with Pygame and is imported the same way (import pygame), so most Pygame programs and tutorials work unchanged.
The game is shown in its own tab. Closing the tab sends a
QUITevent to the program, andpygame.display.set_caption()sets the title of the tab.Sound is supported since version 1.4.0.
The official Pygame examples are bundled with WebTigerPython and patched so that they run in the browser. Tested and working (click to open them in WebTigerPython):
aliens,dropevent,fonty,pixelarray,stars. They are started like this:from pygame.examples import * aliens.main()
import pygame
pygame.init()
screen = pygame.display.set_mode((400, 300))
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill("lightblue")
pygame.draw.circle(screen, "red", (200, 150), 40)
pygame.display.flip()
clock.tick(60)
pygame.quit()