setup()
setup() runs exactly once when the sketch first loads. It's where you initialize the canvas, prompt for player input, create all the initial game objects, and set up game state. Changes here affect the entire simulation.
🔬 This loop spawns 19 customers off the left edge of the screen. What happens if you change numInitialAICustomers to 5? To 50? How does crowd size affect the chaos?
for (let i = 0; i < numInitialAICustomers; i++) {
customers.push(new Customer(random(-100, -50), random(height)));
}
function setup() {
createCanvas(windowWidth, windowHeight);
textFont('Arial');
textAlign(CENTER, CENTER);
rectMode(CENTER);
houseArea.height = windowHeight;
houseEntrance.y = windowHeight / 2;
let colorInput = prompt("Choose your character's color (e.g., 'red', '#00FF00', or RGB like '255,100,0'):", playerCustomColor);
if (colorInput) playerCustomColor = colorInput;
let sizeInput = prompt("Choose your character's size (e.g., '30', '50'):", playerCustomSize);
if (sizeInput && !isNaN(sizeInput)) playerCustomSize = float(sizeInput);
let iconInput = prompt("Choose your character's emoji icon (e.g., '😊', '🚀', '👑'):", playerCustomIcon);
if (iconInput) playerCustomIcon = iconInput;
initializeTables();
player = new PlayerControlledCustomer(houseEntrance.x, houseEntrance.y, playerCustomColor, playerCustomSize, playerCustomIcon);
customers.push(player);
for (let i = 0; i < numInitialAICustomers; i++) {
customers.push(new Customer(random(-100, -50), random(height)));
}
for (let i = 0; i < numRobbers; i++) {
customers.push(new Robber(random(-100, -50), random(height)));
}
for (let i = 0; i < numPoliceOfficers; i++) {
customers.push(new PoliceOfficer(random(-100, -50), random(height)));
}
for (let i = 0; i < numBoatPeople; i++) {
customers.push(new BoatPerson(random(-100, -50), random(height)));
}
}
Line-by-line explanation (11 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a full-screen canvas and configures text rendering defaults
let colorInput = prompt("Choose your character's color...", playerCustomColor);
Asks the player to customize their character's appearance before the game starts
for (let i = 0; i < numInitialAICustomers; i++) { customers.push(new Customer(random(-100, -50), random(height))); }
Creates initial AI customers, robbers, police officers, and boat people and adds them to the customers array
createCanvas(windowWidth, windowHeight);- Creates a p5.js canvas that fills the entire browser window, so the simulation scales to any screen size
textFont('Arial');- Sets the font for all text rendering to Arial, a clean, readable sans-serif font
textAlign(CENTER, CENTER);- Configures text alignment so text centers at the x,y coordinates you provide—easier for centering emojis and speech bubbles
rectMode(CENTER);- Makes rect() draw from the center point instead of the top-left, matching how circle() works and simplifying layout calculations
houseArea.height = windowHeight;- Sets the house interior's height to fill the whole screen, so it scales responsively
let colorInput = prompt("Choose your character's color...", playerCustomColor);- Opens a browser prompt where you type your player's color name, hex code, or RGB values before the game starts
if (colorInput) playerCustomColor = colorInput;- If you entered a color, saves it; otherwise keeps the default '#FF00FF' (bright magenta)
initializeTables();- Calls a helper function that creates the four restaurant tables and positions them evenly across the screen
player = new PlayerControlledCustomer(houseEntrance.x, houseEntrance.y, playerCustomColor, playerCustomSize, playerCustomIcon);- Creates your player character with your custom color, size, and emoji icon, positioned at the house entrance
customers.push(player);- Adds your player to the customers array so they're updated and drawn every frame like any other character
for (let i = 0; i < numInitialAICustomers; i++) { customers.push(new Customer(random(-100, -50), random(height))); }- Spawns 19 AI customers at random positions off the left edge, so they 'arrive' walking in from off-screen