setup()
setup() runs once when the sketch first loads. It initializes all the data structures (arrays for customers, food, tables) and places them in their starting positions. The use of nested loops shows how to spawn multiple objects of different types into a shared customers array—a pattern used in games and simulations everywhere.
function setup() {
createCanvas(windowWidth, windowHeight);
textFont('Arial');
textAlign(CENTER, CENTER);
rectMode(CENTER);
tables.push(new Table(width / 3, height / 3, 2));
tables.push(new Table(2 * width / 3, height / 3, 2));
tables.push(new Table(width / 3, 2 * height / 3, 2));
tables.push(new Table(2 * width / 3, 2 * height / 3, 2));
for (let i = 0; i < numInitialCustomers; 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 (6 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a full-window canvas and configures text alignment and rectangle drawing mode for the entire sketch
tables.push(new Table(width / 3, height / 3, 2));
Positions tables in a 2×2 grid layout, each with 2 chairs, forming the core restaurant structure
for (let i = 0; i < numInitialCustomers; i++) {
customers.push(new Customer(random(-100, -50), random(height)));
}
Creates 20 Customer objects starting off-screen to the left, ready to enter the restaurant
for (let i = 0; i < numRobbers; i++) {
customers.push(new Robber(random(-100, -50), random(height)));
}
Creates 1 Robber object, stored in the customers array alongside regular customers
for (let i = 0; i < numPoliceOfficers; i++) {
customers.push(new PoliceOfficer(random(-100, -50), random(height)));
}
Creates 1 PoliceOfficer object, ready to patrol the restaurant and catch robbers
for (let i = 0; i < numBoatPeople; i++) {
customers.push(new BoatPerson(random(-100, -50), random(height)));
}
Creates 1 BoatPerson object with unique appearance and behavior, behaving like a regular customer
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window, allowing the restaurant to scale to any screen size
textFont('Arial');- Sets the font for all text rendered in the sketch; Arial is used because it displays emojis clearly
textAlign(CENTER, CENTER);- Anchors all text at its center point rather than the default top-left; this makes positioning text and emojis intuitive
rectMode(CENTER);- Changes rectangle drawing so that rect(x, y, w, h) treats (x, y) as the center, not the top-left—critical for tables and speech bubbles
tables.push(new Table(width / 3, height / 3, 2));- Creates a Table object at one-third of the canvas width and height; all four tables use similar positioning at different quadrants
customers.push(new Customer(random(-100, -50), random(height)));- Creates a Customer at a random y-position off-screen to the left (x between -100 and -50), so they enter the restaurant smoothly