setup()
setup() runs once when the sketch starts. It prepares the canvas, creates all buttons, and initializes all the data structures (upgrades, skins, purchased items) that the game needs. Notice how it creates buttons for game states that don't exist yet—they're hidden until needed, which keeps the code organized.
function setup() {
createCanvas(windowWidth, windowHeight);
player = { x: width / 2, y: height - 60, w: 110, h: 25 };
startButton = createButton('Start Game');
startButton.mousePressed(startGame);
shopButton = createButton('Shop');
shopButton.mousePressed(() => switchState('shop'));
skinButton = createButton('Skin Shop');
skinButton.mousePressed(() => switchState('skins'));
backButton = createButton('Back');
backButton.mousePressed(() => switchState('menu'));
playAgainButton = createButton('Play Again');
playAgainButton.mousePressed(() => {
resetGame();
switchState('playing');
});
menuButton = createButton('Menu');
menuButton.mousePressed(() => {
resetGame();
switchState('menu');
});
upgrades.forEach((upgrade) => {
const btn = createButton(upgrade.label);
btn.mousePressed(() => buyMultiplier(upgrade));
upgradeButtons.push({ btn, upgrade });
});
skins.forEach((skin) => {
const btn = createButton(`${skin.name} (${skin.cost})`);
btn.mousePressed(() => handleSkinPurchase(skin));
skinButtons.push({ btn, skin });
purchasedSkins[skin.name] = skin.name === 'Red';
});
styleButtons();
updateButtons();
}
Line-by-line explanation (8 lines)
🔧 Subcomponents:
player = { x: width / 2, y: height - 60, w: 110, h: 25 };
Creates the paddle object with starting position at bottom-center and dimensions for collision detection
upgrades.forEach((upgrade) => { const btn = createButton(upgrade.label); btn.mousePressed(() => buyMultiplier(upgrade)); upgradeButtons.push({ btn, upgrade }); });
Creates clickable buttons for each score multiplier upgrade and stores them with their upgrade data
skins.forEach((skin) => { const btn = createButton(`${skin.name} (${skin.cost})`); btn.mousePressed(() => handleSkinPurchase(skin)); skinButtons.push({ btn, skin }); purchasedSkins[skin.name] = skin.name === 'Red'; });
Creates clickable buttons for each paddle skin and marks Red as unlocked by default
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window, making the game responsive to screen size
player = { x: width / 2, y: height - 60, w: 110, h: 25 };- Initializes the paddle as an object with x/y position, width w, and height h; centered horizontally and 60 pixels from the bottom
startButton = createButton('Start Game');- Creates a clickable button labeled 'Start Game' that will trigger the startGame function
startButton.mousePressed(startGame);- Attaches the startGame function to run whenever the startButton is clicked
upgrades.forEach((upgrade) => { const btn = createButton(upgrade.label); btn.mousePressed(() => buyMultiplier(upgrade)); upgradeButtons.push({ btn, upgrade }); });- Loops through the upgrades array, creates a button for each one, attaches a click handler, and stores both the button and upgrade data together
skins.forEach((skin) => { const btn = createButton(`${skin.name} (${skin.cost})`); btn.mousePressed(() => handleSkinPurchase(skin)); skinButtons.push({ btn, skin }); purchasedSkins[skin.name] = skin.name === 'Red'; });- Loops through the skins array, creates a button for each skin showing its name and cost, and marks the Red skin as already purchased
styleButtons();- Calls the styleButtons function to apply consistent visual styling (colors, padding, border-radius) to all buttons
updateButtons();- Calls updateButtons to position all buttons and hide/show them based on the current game state