setup()
setup() runs once when the sketch starts. It prepares your canvas size, colors, fonts, and any global objects that need initialization before the game begins. By using windowWidth and windowHeight, the game adapts to any device size.
function setup() {
createCanvas(windowWidth, windowHeight);
colorMode(HSB, 360, 100, 100);
textFont('sans-serif');
textAlign(CENTER, CENTER);
createPlayer();
// Simple synths from p5.sound:
// https://p5js.org/reference/#/p5.MonoSynth
goodSynth = new p5.MonoSynth();
badSynth = new p5.MonoSynth();
}
Line-by-line explanation (7 lines)
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window, making the game responsive to screen size
colorMode(HSB, 360, 100, 100);- Switches p5.js to HSB (Hue, Saturation, Brightness) color mode instead of RGB—this makes it easy to animate color gradually by changing just the hue number
textFont('sans-serif');- Sets the font for all text drawn by text() to a clean sans-serif typeface
textAlign(CENTER, CENTER);- Makes all text anchor at its center point instead of the default top-left, so coordinates are more intuitive
createPlayer();- Calls a helper function that initializes the player object with starting position and size
goodSynth = new p5.MonoSynth();- Creates a monophonic synthesizer (single note at a time) for happy catch sounds from the p5.sound library
badSynth = new p5.MonoSynth();- Creates a second synthesizer for unhappy sounds (missed happy faces or caught angry faces)