setup()
setup() runs once when the page loads. It prepares the canvas, initializes all audio oscillators, creates UI buttons, and calls restartGame() to start Level 1. All p5.sound oscillators must be created and started here before they can be used in draw().
function setup() {
createCanvas(windowWidth, windowHeight);
appleBuffer = createGraphics(50, 50);
appleBuffer.noStroke();
appleBuffer.fill(220, 20, 60);
appleBuffer.ellipse(25, 30, 40, 40);
appleBuffer.stroke(139, 69, 19);
appleBuffer.strokeWeight(2);
appleBuffer.line(25, 10, 25, 20);
appleBuffer.noStroke();
appleBuffer.fill(34, 139, 34);
appleBuffer.ellipse(35, 15, 15, 8);
userStartAudio();
backgroundMusic = new p5.Oscillator('sine');
backgroundMusic.freq(50);
backgroundMusic.amp(0.05);
backgroundMusic.start();
foundAppleSound = new p5.Oscillator('triangle');
foundAppleSound.freq(440);
foundAppleSound.amp(0);
foundAppleSound.start();
appleShootingSound = new p5.Oscillator('square');
appleShootingSound.freq(1000);
appleShootingSound.amp(0);
appleShootingSound.start();
zombieHitSound = new p5.Oscillator('sine');
zombieHitSound.freq(150);
zombieHitSound.amp(0);
zombieHitSound.start();
zombieDeathSound = new p5.Oscillator('sawtooth');
zombieDeathSound.freq(100);
zombieDeathSound.amp(0);
zombieDeathSound.start();
zombieSpawnSound = new p5.Oscillator('triangle');
zombieSpawnSound.freq(80);
zombieSpawnSound.amp(0);
zombieSpawnSound.start();
fruitChaseSound = new p5.Oscillator('sawtooth');
fruitChaseSound.freq(80);
fruitChaseSound.amp(0);
fruitChaseSound.start();
doorOpenSound = new p5.Oscillator('sine');
doorOpenSound.freq(600);
doorOpenSound.amp(0);
doorOpenSound.start();
playerCaughtSound = new p5.Oscillator('triangle');
playerCaughtSound.freq(250);
playerCaughtSound.amp(0);
playerCaughtSound.start();
cakeSplatSound = new p5.Oscillator('square');
cakeSplatSound.amp(0);
cakeSplatSound.start();
animalOuchSound = new p5.Oscillator('triangle');
animalOuchSound.amp(0);
animalOuchSound.start();
hintButton = createButton('Give me a Hint!');
hintButton.position(width / 2 - hintButton.width / 2, height - 50);
hintButton.mousePressed(showHint);
hintButton.touchStarted(showHint);
restartButton = createButton('Restart Game');
restartButton.position(width / 2 - restartButton.width / 2, height / 2 + 50);
restartButton.mousePressed(restartGame);
restartButton.touchStarted(restartGame);
restartButton.hide();
restartGame();
}
Line-by-line explanation (10 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a fullscreen canvas that fills the entire browser window
appleBuffer = createGraphics(50, 50);
Pre-renders a detailed apple image (50×50 pixels) that can be scaled down to tiny sizes without quality loss
userStartAudio();
Enables audio context in the browser—required before p5.sound oscillators will work
backgroundMusic = new p5.Oscillator('sine');
Creates 9 different oscillator objects for background music and various sound effects throughout the game
hintButton = createButton('Give me a Hint!');
Creates and positions two interactive buttons for hints and game restart
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window; windowWidth and windowHeight are p5.js built-in variables that update when the window resizes
appleBuffer = createGraphics(50, 50);- createGraphics() creates an invisible drawing surface, like a 50×50 pixel canvas, where we can draw detailed graphics once and reuse them at any scale
appleBuffer.ellipse(25, 30, 40, 40);- Draws a red circle onto the buffer to form the apple's body; all subsequent appleBuffer calls draw on this private surface
userStartAudio();- Initializes the p5.sound library and activates the browser's audio context, which is required before oscillators can play sound
backgroundMusic = new p5.Oscillator('sine');- Creates a new oscillator that produces a sine wave (smooth, pure tone); the first of many sound generators
backgroundMusic.freq(50); backgroundMusic.amp(0.05); backgroundMusic.start();- Sets the oscillator's frequency to 50 Hz (very deep bass), volume to 0.05 (quiet), and starts it playing continuously
hintButton = createButton('Give me a Hint!');- Creates a clickable/touchable button that calls showHint() when pressed
hintButton.position(width / 2 - hintButton.width / 2, height - 50);- Positions the button at the bottom-center of the screen by calculating its center point
restartButton.hide();- The restart button is hidden initially and only shown when the player loses a level
restartGame();- Calls the game initialization function, which sets level to 1 and calls setupLevel() to begin playing