setup()
setup() runs once when the sketch launches. It's where you initialize the canvas size, pre-compute colors, set up audio, and spawn the core game objects. Think of it as the 'load game' function.
function setup() {
createCanvas(windowWidth, windowHeight);
grassColor = color(108, 194, 74);
pathColor = color(214, 203, 144);
waterColor = color(100, 150, 255);
setupAudio();
// Initialize World
goose = new Goose(0, 0);
blanketPos = createVector(0, 0);
pondPos = createVector(-400, 200);
startLevel(1);
userStartAudio();
}
Line-by-line explanation (10 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a full-window canvas that scales with the browser
grassColor = color(108, 194, 74);
Stores reusable colors for the background and world elements
setupAudio();
Initializes oscillators and envelopes for sound effects
goose = new Goose(0, 0);
Creates the goose player at the origin and sets landmark positions
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window; windowWidth and windowHeight update when the window resizes
grassColor = color(108, 194, 74);- Stores an RGB green color in a global variable so it can be reused for the background without hardcoding the numbers
pathColor = color(214, 203, 144);- Stores a sandy beige color for later use (currently unused in the code, but prepared for future path graphics)
waterColor = color(100, 150, 255);- Stores a light blue color for the pond that will be drawn later
setupAudio();- Calls the setupAudio function to initialize p5.sound oscillators and envelopes for honk and victory sounds
goose = new Goose(0, 0);- Creates a new Goose object at coordinates (0, 0); this is the player character you control
blanketPos = createVector(0, 0);- Sets the home/goal location (the blanket) at the origin; items brought here count as 'stolen'
pondPos = createVector(-400, 200);- Sets the pond/lake location 400 pixels left and 200 pixels down from the blanket; items dunked here complete DUNK tasks
startLevel(1);- Generates the first level: picks a random task, spawns the target item and decoys, and positions gardener guards
userStartAudio();- p5.sound requires user interaction to start audio; this call primes the system so sounds play when honk() is called