setup()
setup() runs once at the start and is where you initialize your canvas, objects, and buttons. Every button created here needs a callback function (mousePressed) to define what happens when clicked.
🔬 This loop creates buttons for all skins and marks Red as purchased. What happens if you change the condition to `skin.name === 'Rainbow'` instead? Which skin would now start unlocked?
skins.forEach((skin) => {
const btn = createButton(`${skin.name} (${skin.cost})`);
btn.mousePressed(() => handleSkinPurchase(skin));
skinButtons.push({ btn, skin });
purchasedSkins[skin.name] = skin.name === 'Red';
});
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 (10 lines)
🔧 Subcomponents:
upgrades.forEach((upgrade) => {
Dynamically creates a clickable button for each multiplier upgrade
skins.forEach((skin) => {
Dynamically creates a clickable button for each available skin
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window
player = { x: width / 2, y: height - 60, w: 110, h: 25 };- Initializes the paddle as an object with position (x, y), width (w), and height (h) at the bottom-center of the canvas
startButton = createButton('Start Game');- Creates an HTML button element labeled 'Start Game' that will appear on screen
startButton.mousePressed(startGame);- Attaches a callback function that runs when the Start Game button is clicked
upgrades.forEach((upgrade) => {- Loops through the upgrades array to create a button for each score multiplier
btn.mousePressed(() => buyMultiplier(upgrade));- Connects each upgrade button to the buyMultiplier function, passing the specific upgrade data
skins.forEach((skin) => {- Loops through the skins array to create a button for each paddle customization option
purchasedSkins[skin.name] = skin.name === 'Red';- Marks the Red skin as already owned; all other skins start as unpurchased
styleButtons();- Applies consistent styling (color, size, border-radius) to all buttons
updateButtons();- Positions all buttons and sets which ones should be visible based on the current game state