setup()
setup() runs once when the sketch starts. It initializes your game state (the upgrades array), creates all interactive elements (buttons), and loads any saved progress. Notice how each upgrade is an object containing multiple properties—this is a common pattern for managing game data.
🔬 This code defines the Cursor upgrade with baseCost: 15 and baseCPS: 0.1. What happens if you change baseCPS to 0.5 or 1? How does that change how fast you can earn cookies?
upgrades.push({
name: "Cursor",
baseCost: 15,
cost: 15,
baseCPS: 0.1,
cps: 0.1,
count: 0,
button: null
});
function setup() {
createCanvas(windowWidth, windowHeight);
textAlign(CENTER, CENTER);
textSize(24);
upgrades.push({
name: "Cursor",
baseCost: 15,
cost: 15,
baseCPS: 0.1,
cps: 0.1,
count: 0,
button: null
});
upgrades.push({
name: "Grandma",
baseCost: 100,
cost: 100,
baseCPS: 1,
cps: 1,
count: 0,
button: null
});
upgrades.push({
name: "Farm",
baseCost: 1100,
cost: 1100,
baseCPS: 8,
cps: 8,
count: 0,
button: null
});
upgrades.push({
name: "Mine",
baseCost: 12000,
cost: 12000,
baseCPS: 47,
cps: 47,
count: 0,
button: null
});
upgrades.push({
name: "Factory",
baseCost: 130000,
cost: 130000,
baseCPS: 260,
cps: 260,
count: 0,
button: null
});
upgrades.push({
name: "Bank",
baseCost: 1400000,
cost: 1400000,
baseCPS: 1400,
cps: 1400,
count: 0,
button: null
});
upgrades.push({
name: "Temple",
baseCost: 20000000,
cost: 20000000,
baseCPS: 7800,
cps: 7800,
count: 0,
button: null
});
let buttonY = 50;
for (let i = 0; i < upgrades.length; i++) {
let upgrade = upgrades[i];
let btn = createButton('');
btn.addClass('upgrade-button');
btn.position(width - 200, buttonY);
btn.mouseClicked(() => buyUpgrade(i));
upgrade.button = btn;
buttonY += 70;
}
let resetButton = createButton('Reset Game');
resetButton.addClass('reset-button');
resetButton.position(width - 200, height - 50);
resetButton.mouseClicked(resetGame);
loadGame();
autoSaveInterval = setInterval(saveGame, 5000);
}
Line-by-line explanation (8 lines)
🔧 Subcomponents:
upgrades.push({name: "Cursor", baseCost: 15, ...})
Defines all seven upgrade types with their base cost and CPS, which scale as the player purchases them
for (let i = 0; i < upgrades.length; i++) { let btn = createButton(''); ... }
Creates a clickable p5.Button for each upgrade, positions it on the right side, and stores a reference to it in the upgrade object
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window, making the game responsive to screen size
textAlign(CENTER, CENTER);- Centers all text horizontally and vertically, so text draws from its center point rather than its top-left corner
upgrades.push({name: "Cursor", baseCost: 15, cost: 15, baseCPS: 0.1, cps: 0.1, count: 0, button: null});- Adds the first upgrade object to the upgrades array with its name, base cost, CPS value, count (initially 0), and a button placeholder
let btn = createButton('');- Creates an empty button—its text will be updated in draw() every frame with current cost and count
btn.mouseClicked(() => buyUpgrade(i));- Assigns a click handler that calls buyUpgrade with the index i, so each button knows which upgrade to purchase
upgrade.button = btn;- Stores the button object reference in the upgrade's 'button' property so draw() can update it later
loadGame();- Restores the player's previous score, upgrade counts, and costs from localStorage if they exist
autoSaveInterval = setInterval(saveGame, 5000);- Starts an automatic save timer that calls saveGame() every 5000 milliseconds (5 seconds)