setup()
setup() runs exactly once when the sketch loads. It prepares the canvas, initializes all variables, and builds the static city background. Anything you want ready before gameplay starts goes here.
function setup() {
createCanvas(windowWidth, windowHeight);
angleMode(RADIANS);
anchorX = width * 0.5;
anchorY = height * 0.08;
createCityscape();
createStars();
resetGame();
}
Line-by-line explanation (7 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a full-window canvas that responds to screen size
angleMode(RADIANS);
Sets angle measurements to radians (required for sin/cos trigonometry)
anchorX = width * 0.5;
anchorY = height * 0.08;
Places the web anchor point at the top center of the canvas (8% down from top)
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire window; windowWidth and windowHeight make it responsive to screen size
angleMode(RADIANS);- Tells p5.js to measure angles in radians (0 to 2π) instead of degrees—required for sin() and cos() math
anchorX = width * 0.5;- Sets the anchor point's horizontal position to the center of the canvas (50% of width)
anchorY = height * 0.08;- Sets the anchor point's vertical position to 8% down from the top of the canvas
createCityscape();- Generates the random buildings that form the city skyline at the bottom
createStars();- Creates 120 random stars scattered across the sky for the twinkling background
resetGame();- Initializes game variables: score = 0, lives = 3, clears any existing orbs, and sets gameOver to false