setup()
setup() runs once when the sketch starts. It's the perfect place to initialize canvas size, define colors, set up coordinate systems, and populate data structures like the characters array. All the variables you set up here (groundY, jackHouseX, the characters array) are then used throughout draw() and the interaction functions.
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
insideFloorY = height * 0.8; // 80% down from the top is the floor inside houses
// 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, outsideX, outsideY, insideX, insideY, size, color, home, type, message
characters.push({ name: "Jack", outsideX: jackHouseX, outsideY: groundY, insideX: 0, insideY: 0, size: jackSize, color: '#FFD700', home: "jackHouse", type: "player" });
characters.push({ name: "Jack's Mom", outsideX: jackHouseX - 60, outsideY: groundY, insideX: -60, insideY: 0, size: 25, color: '#FF69B4', home: "jackHouse", type: "parent", message: "Jack's mom loves him so much!" });
characters.push({ name: "Jack's Dad", outsideX: jackHouseX + 60, outsideY: groundY, insideX: 60, insideY: 0, size: 25, color: '#1E90FF', home: "jackHouse", type: "parent", message: "Jack's dad is proud of him!" });
characters.push({ name: "Old Consoles", outsideX: jackHouseX, outsideY: groundY, insideX: 0, insideY: -15, size: 40, color: '#696969', home: "jackHouse", type: "object", message: "Jack loves his old Nintendo consoles: N64, GameCube, PS2!" });
characters.push({ name: "Cappie's Mom", outsideX: cappieHouseX - 60, outsideY: groundY, insideX: -60, insideY: 0, size: 25, color: '#FF6347', home: "cappieHouse", type: "parent", message: "Cappie's mom thinks Jack is a sweet boy!" });
characters.push({ name: "Cappie's Dad", outsideX: cappieHouseX + 60, outsideY: groundY, insideX: 60, insideY: 0, size: 25, color: '#32CD32', home: "cappieHouse", type: "parent", message: "Cappie's dad likes Jack's spirit!" });
characters.push({ name: "Cappie", outsideX: cappieHouseX, outsideY: groundY, insideX: 0, insideY: 0, size: 30, color: '#FFC0CB', home: "cappieHouse", type: "girlfriend", message: "Cappie loves Jack! She's so happy to see him!" });
}
Line-by-line explanation (6 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a full-screen canvas and sets up drawing modes for rectangles and text alignment
groundY = height * 0.7; // 70% down from the top is the ground level
Calculates where characters stand outside and inside based on canvas height
jackHouseX = width * 0.2; // Jack's house on the left
Places houses at 20% and 80% of canvas width so they scale with screen size
characters.push({ name: "Jack", outsideX: jackHouseX, outsideY: groundY, insideX: 0, insideY: 0, size: jackSize, color: '#FFD700', home: "jackHouse", type: "player" });
Populates the characters array with all NPCs, setting their initial positions, colors, and messages
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window, making the game responsive to window size
rectMode(CENTER);- Changes rectangle drawing so the x,y position is at the center instead of the top-left corner—makes positioning and transforms cleaner
textAlign(CENTER, BOTTOM);- Centers text horizontally and aligns it to the bottom of its position, useful for name tags below characters
groundY = height * 0.7;- Calculates where the ground is by using 70% of the canvas height; this keeps the ground proportional to any screen size
jackHouseX = width * 0.2;- Places Jack's house at 20% across from the left; using multiplication of width makes it scale when the window resizes
characters.push({ name: "Jack", outsideX: jackHouseX, outsideY: groundY, ... });- Adds Jack to the characters array as an object with properties for position, size, color, home, type, and message