setup()
setup() runs once when the sketch starts. It creates the canvas, initializes all game variables, and attaches event listeners. p5.js then enters the draw loop automatically.
function setup() {
createCanvas(windowWidth, windowHeight);
noSmooth();
textFont('monospace');
initializeLayout();
initializePlayer();
populateBrainrots();
nextCelestialAt = millis() + celestialInterval;
scheduleNextWave(500);
window.addEventListener('blur', releaseAllKeys);
window.addEventListener('focus', releaseAllKeys);
}
Line-by-line explanation (9 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a full-screen canvas and disables antialiasing for pixel-perfect graphics
initializeLayout();
Sets up spawn line, safe areas, base position, and red despawn line based on screen size
window.addEventListener('blur', releaseAllKeys);
Prevents stuck movement keys when the browser window loses focus
createCanvas(windowWidth, windowHeight);- Makes a canvas that fills the entire browser window; updateCanvas on resize will handle window changes
noSmooth();- Disables antialiasing so pixels stay crisp and blocky, fitting the retro arcade style
textFont('monospace');- Uses a fixed-width font for all text, making numbers and labels look uniform
initializeLayout();- Calls helper to calculate spawnLineY, redLineY, base position, and safe areas based on current window size
initializePlayer();- Creates the player object and positions them at the base ready to start
populateBrainrots();- Fills the screen with 70 randomly-positioned and tier-ranked brainrots
nextCelestialAt = millis() + celestialInterval;- Schedules the first celestial brainrot spawn for 5 minutes from now (5 * 60 * 1000 ms)
scheduleNextWave(500);- Schedules the first tsunami wave to spawn after a 500ms delay
window.addEventListener('blur', releaseAllKeys);- Clears all movement flags when the player clicks away, preventing stuck keys