Your first game
Bubbles: bubbles float up from the bottom of the screen, and the children pop them with their hands, for one
minute. It uses the npm packages and a bundler (Vite); with script tags the code is the same, with
FabiEngine.createEngine({ play: FabiPlay }) in place of the imports (see Installation).
The code below is Fabi Play’s own copy of the example game, shown as it is: it is type-checked on every change, so this page cannot drift from it.
// Bubbles: pop the bubbles with your hands, for one minute. The example game of the engine guide// (docs.fabiplay.app/engine/first-game/), which shows this file as it is: change it here and the page follows.import { connect } from '@fabiplay/sdk'import { createEngine } from '@fabiplay/engine'
const play = await connect() // waits for Fabi Play (or the simulator) to start the game// Picks WebGL2, WebGL1 or Canvas 2D and a quality tier. The background is drawn only when there is no camera// video behind the game (mirror mode): over the video the stage stays transparent by itself.const engine = await createEngine({ play, background: '#1B2A4A' })const { stage } = engine // 720 units high, as wide as the screen (1280 at 16:9)
stage.add(engine.avatars()) // each child as their avatar, where they stand (nothing over the video)const bubbles = stage.add(engine.group())const pops = stage.add(engine.particles({ type: 'circle', r: 5, fill: '#fff' }, { max: engine.tier.scale(200), gravity: 300 }))engine.bitmapFont('score', { size: 56, outline: true }) // the digits, baked once: the score changes for freeconst label = stage.add(engine.text('0', { bitmapFont: 'score', x: 40, y: 50, align: 'left' }))const scores = new Map<number, number>(play.players.map((player) => [player, 0]))let total = 0let over = false
engine.onFrame((dt, time) => { if (over) return if (time > 60) { over = true // one minute: the screen shows the results and takes over play.end([...scores].map(([player, score]) => ({ player, score, stars: score >= 20 ? 3 : score >= 10 ? 2 : 1 }))) return } if (Math.random() < dt * 1.5) { const r = engine.random(40, 70) // about one and a half new bubbles a second const bubble = engine.shape( { type: 'circle', r, fill: 'rgba(127,168,224,0.45)', stroke: '#ffffff', lw: 4 }, { x: engine.random(r, stage.width - r), y: stage.height + r }, ) bubble.data.speed = engine.random(60, 120) // stage units per second bubbles.add(bubble) } for (const bubble of bubbles.children) { if (bubble.data.popped) continue // fading out: count it once bubble.y -= bubble.data.speed * dt bubble.x += Math.sin(time * 2 + bubble.y / 80) * 20 * dt const hand = engine.touched(bubble, undefined, { padding: 10 }) // any child's hand on the bubble if (hand) { bubble.data.popped = true scores.set(hand.player, (scores.get(hand.player) ?? 0) + 1) play.score(hand.player, scores.get(hand.player)!) label.text = String(++total) pops.burst(bubble.x, bubble.y, engine.tier.scale(20), { tint: hand.color, speed: [80, 260] }) engine.sound.tone(engine.random(600, 1000), 0.12) engine.tween(bubble, { scale: 1.4, alpha: 0 }, 0.2).then(() => bubble.destroy()) } else if (bubble.y < -100) bubble.destroy() // floated away }})
engine.start()play.ready() // the screen hides its loader: say it within 10 secondsWhat each part does
Section titled “What each part does”connect()waits for Fabi Play (or the simulator) to start the game, and gives thePlayobject: who plays, their age range, the backdrop. See Getting started.createEngine({ play, background })picks the renderer and the quality tier, makes the canvas fill the page, and follows the host by itself: pauses, breaks, the backdrop, the hands.backgroundis painted only when there is no video behind the game: in mirror mode the stage is transparent by itself.stage.add(...)puts nodes on the stage, 720 units high. Nodes are drawn in the order they are added: the avatars first, then the bubbles, the particles and the score on top.engine.avatars()draws each child as the avatar they picked, where they stand, and nothing over the video.engine.bitmapFont('score', …)makes a bitmap font of the digits, and the score is drawn with it: it changes at every pop without being baked again.engine.shape(...)makes a shape, drawn once into the atlas and then cheap to move.datais free for your own fields, such as the bubble’s speed.engine.onFrame((dt, time) => …)runs every frame while the game is not paused:dtis the seconds since the last frame,timethe game time. Move things byspeed * dt, never by a fixed step per frame.engine.touched(bubble, …)is the first hand (of any child) on the bubble, ornull: in the avatar’s hand over your background, on the real hand over the video. See Hands and touch.burst,tone,tween: particles in the player’s colour, a sound without a sound file, and a short fade beforedestroy().play.scoreandplay.endtell Fabi Play the scores; at the end the screen shows the results and takes over.play.ready()hides Fabi Play’s loader. Say it within 10 seconds of the page loading, or the TV closes the game.
The rest of the project
Section titled “The rest of the project”<!doctype html><html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Bubbles: a Fabi Play example</title> <style> html, body { margin: 0; height: 100%; overflow: hidden; } </style> </head> <body> <script type="module" src="/src/main.ts"></script> </body></html>The manifest next to the page. backdrop: ["video"] opts in to mirror mode, which the engine
handles. In your own project point $schema at ../node_modules/@fabiplay/sdk/dist/fabi-play.schema.json (the
path below is the one inside Fabi Play’s repository).
{ "$schema": "../../../node_modules/@fabiplay/sdk/dist/fabi-play.schema.json", "id": "bubbles-example", "version": "1.0", "title": { "it": "Bolle", "en": "Bubbles" }, "description": { "it": "Fai scoppiare le bolle con le mani.", "en": "Pop the bubbles with your hands." }, "ageRanges": ["3-5", "6-8"], "movements": ["hands"], "players": { "min": 1, "max": 4 }, "minutes": 1, "entry": "index.html", "sdk": 1, "backdrop": ["video"]}The dev server lets the hosted simulator read fabi-play.json (CORS):
import { defineConfig } from 'vite'
// The example game on http://localhost:5180, for the simulator (local or simulator.fabiplay.app). The simulator reads// fabi-play.json from another origin, so CORS lets both simulators in (see docs.fabiplay.app/sdk/simulator/).export default defineConfig({ root: import.meta.dirname, server: { port: 5180, strictPort: true, cors: { origin: [/^https?:\/\/(?:[^:]+\.)?localhost(?::\d+)?$/, 'https://simulator.fabiplay.app'] }, }, build: { target: 'es2022' },})build.target: 'es2022' allows the top-level await of main.ts. For older targets, wrap the code in an async
function.
Run it in the simulator
Section titled “Run it in the simulator”-
Make the project: a folder with the four files above,
src/main.ts,index.html,public/fabi-play.jsonandvite.config.ts, then install the packages and Vite.Terminal window npm init -ynpm install @fabiplay/sdk @fabiplay/enginenpm install --save-dev viteVite runs TypeScript without checking it. To check the types too, add
typescriptand thetsconfig.jsonof TypeScript types, then runnpx tsc. -
Start the game:
Terminal window npx viteIt is on
http://localhost:5180/(the portvite.config.tssets).(For the Fabi Play team, in the repository:
pnpm install, thenpnpm -F @fabiplay/docs example.) -
Open the simulator on it: simulator.fabiplay.app/?game=http://localhost:5180/index.html. Use Chrome or Firefox: they let an https page frame a game on
http://localhost. -
Play. With «Il mouse sul gioco è una mano» (The mouse over the game is a hand) on, move the mouse over the game: the puppet’s hand follows it and pops the bubbles. The log shows
ready, then ascorefor every pop. -
Try the other cases: 2 to 4 players in «Giocatori», another avatar in «Personaggi», a pause with P, the webcam under the game with «Mirror (webcam)» (the background goes, the bubbles stay), and the Canvas 2D renderer or the low tier in the «Motore» (Engine) panel. The status line under the game shows the renderer, tier and frame rate the engine reports.
The simulator page explains the rest: Testing with the simulator.
Where next
Section titled “Where next”- Make it yours: sprites, shapes, text and groups, your own art with bake and atlases.
- Make it run well on a TV: performance tips.