setup()
setup() runs once when the sketch starts. It initializes the canvas, prepares game objects, and schedules future events. Notice how it calls multiple helper functions to organize initialization logic.
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 canvas that fills the entire window
window.addEventListener('blur', releaseAllKeys);
Releases all held keys when the window loses focus, preventing stuck movement
createCanvas(windowWidth, windowHeight);- Creates a p5 canvas that fills the entire browser window width and height
noSmooth();- Disables anti-aliasing for a crisp, pixelated look that fits the arcade aesthetic
textFont('monospace');- Sets the default font to monospace, giving the UI a retro computer feel
initializeLayout();- Calculates positions of safe areas, base, and spawn lines based on canvas size
initializePlayer();- Creates the player object at the base's starting position
populateBrainrots();- Populates the brainrots array with 70 randomly positioned and tiered characters
nextCelestialAt = millis() + celestialInterval;- Schedules the first celestial brainrot spawn to occur 5 minutes from now
scheduleNextWave(500);- Schedules the first tsunami wave to spawn after 500 milliseconds
window.addEventListener('blur', releaseAllKeys);- When the browser tab loses focus, release all movement keys to prevent stuck keys