setup()
setup() runs once when the sketch starts. It's where you initialize canvas size, set drawing modes, calculate positions based on screen dimensions, and populate your game's data structures. Notice how positions use percentages like width * 0.2 instead of hard-coded pixel numbers—this makes the game responsive to any screen size.
function setup() {
createCanvas(windowWidth, windowHeight);
rectMode(CENTER); // Draw rectangles from their center
textAlign(CENTER, BOTTOM); // Align text for character names
textSize(12); // Default text size for character names
groundY = height * 0.7; // 70% down from the top is the ground level
// Initialize house positions
jackHouseX = width * 0.2; // Jack's house on the left
cappieHouseX = width * 0.8; // Cappie's house on the right
// Define all characters and interactive objects
// Name, x, y, size, color, home (for house location), type, message
characters.push({ name: "Jack", x: jackHouseX, y: groundY, size: jackSize, color: '#FFD700', home: "jackHouse", type: "player" });
characters.push({ name: "Jack's Mom", x: jackHouseX - 60, y: groundY, size: 25, color: '#FF69B4', home: "jackHouse", type: "parent", message: "Jack's mom loves him so much!" });
characters.push({ name: "Jack's Dad", x: jackHouseX + 60, y: groundY, size: 25, color: '#1E90FF', home: "jackHouse", type: "parent", message: "Jack's dad is proud of him!" });
characters.push({ name: "Old Consoles", x: jackHouseX, y: groundY, size: 40, color: '#696969', home: "jackHouse", type: "object", message: "Jack loves his old Nintendo consoles: N64, GameCube, PS2!" });
characters.push({ name: "Cappie's Mom", x: cappieHouseX - 60, y: groundY, size: 25, color: '#FF6347', home: "cappieHouse", type: "parent", message: "Cappie's mom thinks Jack is a sweet boy!" });
characters.push({ name: "Cappie's Dad", x: cappieHouseX + 60, y: groundY, size: 25, color: '#32CD32', home: "cappieHouse", type: "parent", message: "Cappie's dad likes Jack's spirit!" });
characters.push({ name: "Cappie", x: cappieHouseX, y: groundY, size: 30, color: '#FFC0CB', home: "cappieHouse", type: "girlfriend", message: "Cappie loves Jack! She's so happy to see him!" });
}
Line-by-line explanation (7 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a canvas that fills the entire browser window; windowWidth and windowHeight are built-in p5.js variables that get the current window dimensions
characters.push({ name: "Jack", x: jackHouseX, y: groundY, size: jackSize, color: '#FFD700', home: "jackHouse", type: "player" });
Creates character objects with properties like name, position, size, color, and home location, then adds them to the characters array; each character is a complete data record the game uses to render and interact
createCanvas(windowWidth, windowHeight);- Creates a canvas that matches the full window size, so the game fills the entire screen
rectMode(CENTER);- Tells p5.js to draw all rectangles from their center point instead of from their top-left corner—this makes positioning easier
textAlign(CENTER, BOTTOM);- Aligns all text to be centered horizontally and anchored at the bottom, so character names appear above their heads
groundY = height * 0.7;- Sets the ground level to 70% down from the top of the canvas, leaving 30% for sky and leaving room for the title
jackHouseX = width * 0.2;- Places Jack's house 20% from the left edge, making it the left house
cappieHouseX = width * 0.8;- Places Cappie's house 80% from the left edge, making it the right house with space between them
characters.push({ name: "Jack", x: jackHouseX, y: groundY, size: jackSize, color: '#FFD700', home: "jackHouse", type: "player" });- Creates an object that stores all of Jack's data (name, position, appearance, type) and adds it to the characters array—the first character in the array is always Jack, the player