setup()
setup() runs once when the sketch loads. It initializes all global state: the canvas size, the player paddle, and every button in the game. Notice how it creates buttons for features (upgrades, skins) that are stored in arrays and then looped through—this pattern scales well if you want to add more upgrades or skins later.
function setup() {
createCanvas(windowWidth, windowHeight);
player = { x: width / 2, y: height - 60, w: 110, h: 25 };
// --- UI buttons ---
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');
});
// Upgrade buttons (right-side shop)
upgrades.forEach((upgrade) => {
const btn = createButton(upgrade.label);
btn.mousePressed(() => buyMultiplier(upgrade));
upgradeButtons.push({ btn, upgrade });
});
// Skin buttons (left-side shop)
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 (9 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
player = { x: width / 2, y: height - 60, w: 110, h: 25 };
Creates a fullscreen canvas and positions the paddle near the bottom center
startButton = createButton('Start Game');
startButton.mousePressed(startGame);
Creates UI buttons and attaches click handlers to switch game states
upgrades.forEach((upgrade) => {
const btn = createButton(upgrade.label);
btn.mousePressed(() => buyMultiplier(upgrade));
upgradeButtons.push({ btn, upgrade });
});
Creates a button for each score multiplier upgrade and stores it for later layout
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 a button for each skin, tracks which skins are purchased, and unlocks Red by default
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window
player = { x: width / 2, y: height - 60, w: 110, h: 25 };- Creates a player paddle object with position, width, and height; x is centered horizontally, y is 60 pixels from the bottom
startButton = createButton('Start Game');- Creates a clickable button labeled 'Start Game'
startButton.mousePressed(startGame);- Attaches the startGame function to the button so it runs when clicked
upgrades.forEach((upgrade) => {- Loops through each upgrade object (2x, 4x, 8x, etc.) defined at the top of the sketch
btn.mousePressed(() => buyMultiplier(upgrade));- Attaches a click handler to each upgrade button that calls buyMultiplier with that upgrade's data
purchasedSkins[skin.name] = skin.name === 'Red';- Sets purchasedSkins['Red'] to true (unlocked by default) and all others to false (locked)
styleButtons();- Calls a function that applies CSS styles to all buttons (color, font size, padding, etc.)
updateButtons();- Calls a function that positions buttons and shows/hides them based on the current game state