setup()
setup() runs once when the sketch first loads. It's the ideal place to initialize all your game data, create objects, and prepare sound resources. Because sound requires user interaction on mobile devices, setup() calls userStartAudio() early to ensure audio works when the player first taps the screen.
function setup() {
createCanvas(windowWidth, windowHeight);
// Initialize menu foods with type property
menuFoods = [
{ emoji: "🥚", name: "Egg", type: "hot" },
{ emoji: "🥓", name: "Bacon", type: "hot" },
{ emoji: "🌭", name: "Hotdog", type: "hot" },
{ emoji: "🧇", name: "Waffle", type: "hot" },
{ emoji: "🍦", name: "Ice Cream", type: "cold" },
{ emoji: "🥗", name: "Salad", type: "cold" },
{ emoji: "🫐", name: "Blueberry", type: "cold" },
{ emoji: "🍍", name: "Pineapple", type: "cold" },
{ emoji: "🥑", name: "Avocado", type: "cold" },
{ emoji: "🥐", name: "Croissant", type: "hot" },
{ emoji: "🧄", name: "Garlic", type: "hot" },
{ emoji: "🫚", name: "Ginger", type: "hot" },
{ emoji: "🥨", name: "Pretzel", type: "hot" },
{ emoji: "🥞", name: "Pancakes", type: "hot" },
{ emoji: "🦴", name: "Bone", type: "hot" },
{ emoji: "🫓", name: "Flatbread", type: "hot" },
{ emoji: "🌯", name: "Burrito", type: "hot" },
{ emoji: "🥫", name: "Canned Food", type: "hot" },
{ emoji: "🍛", name: "Curry", type: "hot" },
{ emoji: "🍤", name: "Shrimp", type: "hot" },
{ emoji: "🥠", name: "Fortune Cookie", type: "hot" }
];
// Calculate initial layout parameters
calculateLayout();
textAlign(CENTER, CENTER);
userStartAudio();
// Create simple sound effects using p5.Oscillator
tapSound = new p5.Oscillator();
tapSound.setType('sine');
tapSound.freq(440);
tapSound.amp(0);
tapSound.start();
dingSound = new p5.Oscillator();
dingSound.setType('triangle');
dingSound.freq(880);
dingSound.amp(0);
dingSound.start();
buzzSound = new p5.Oscillator();
buzzSound.setType('square');
buzzSound.freq(220);
buzzSound.amp(0);
buzzSound.start();
paymentSound = new p5.Oscillator();
paymentSound.setType('sine');
paymentSound.freq(1200);
paymentSound.amp(0);
paymentSound.start();
}
Line-by-line explanation (6 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a full-window canvas that fills the entire screen
menuFoods = [ { emoji: "🥚", name: "Egg", type: "hot" }, ... ];
Initializes array of 21 food objects, each with emoji, name, and hot/cold type for routing to correct cooking station
tapSound = new p5.Oscillator(); tapSound.setType('sine'); tapSound.freq(440); tapSound.amp(0); tapSound.start();
Creates four oscillators (tap, ding, buzz, payment) that will play sound feedback for user interactions
createCanvas(windowWidth, windowHeight);- Creates a canvas that matches the full browser window size, allowing the game to be responsive to any screen dimension
menuFoods = [{ emoji: "🥚", name: "Egg", type: "hot" }, ...];- Populates the menuFoods array with 21 food objects. Each object stores an emoji, human-readable name, and a type (either 'hot' or 'cold') that determines whether it goes to the microwave or fridge
calculateLayout();- Calls a function that calculates all the x, y, width, and height values for each game zone (menu, customer area, microwave, chopping, fridge) based on the current canvas size
textAlign(CENTER, CENTER);- Sets global text alignment so that all emojis and text are centered around their x, y coordinates—this is critical for emoji to appear visually correct
userStartAudio();- p5.sound library function that enables audio on touch devices by requesting user permission—required on mobile before any sound can play
tapSound = new p5.Oscillator(); tapSound.setType('sine'); tapSound.freq(440); tapSound.amp(0); tapSound.start();- Creates an oscillator with sine wave at 440Hz (musical note A4), sets its volume to 0 (silent), and starts it running. When the player taps, the code will ramp the amplitude up then down to create a short beep