setup()
setup() runs exactly once when the sketch starts. It initializes the canvas, wires up all UI interactions, and pre-populates data structures so the game is ready to play. The use of WEBGL is critical here—it unlocks 3D rendering, lighting, and the camera() function that makes the 2.5D effect possible.
function setup() {
createCanvas(windowWidth, windowHeight, WEBGL);
// Setup Buttons
select('#start-btn').mousePressed(() => setUIState('PLAY'));
select('#restart-btn').mousePressed(() => setUIState('PLAY'));
select('#menu-btn').mousePressed(() => setUIState('MENU'));
select('#shop-btn-menu').mousePressed(() => setUIState('SHOP'));
select('#close-shop-btn').mousePressed(() => setUIState('MENU'));
select('#buy-red').mousePressed(buyRed);
select('#buy-jump').mousePressed(buyDoubleJump);
// Generate infinitely looping background mountains/scenery
for (let i = 0; i < 30; i++) {
sceneryParticles.push({
y: groundY - random(20, 150),
z: random(-3000, 1000),
size: random(40, 120),
rot: random(TWO_PI)
});
}
}
Line-by-line explanation (4 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight, WEBGL);
Creates a full-screen WEBGL canvas, enabling 3D rendering with lighting and transforms
select('#start-btn').mousePressed(() => setUIState('PLAY'));
Connects all UI buttons to state-change functions so the game responds to player clicks
for (let i = 0; i < 30; i++) {
Pre-populates the sceneryParticles array with mountains at random heights and depths so parallax scrolling works immediately
createCanvas(windowWidth, windowHeight, WEBGL);- Creates a full-window WEBGL canvas (3D renderer) instead of the default 2D—necessary for translate(), rotateX(), lighting, and 3D transforms
select('#start-btn').mousePressed(() => setUIState('PLAY'));- Finds the HTML button with id 'start-btn' and links it so clicking it calls setUIState('PLAY') to begin the game
for (let i = 0; i < 30; i++) {- Loops 30 times to create 30 mountain particles at the start, pre-filling the background scenery array
sceneryParticles.push({- Adds a new mountain object to the sceneryParticles array with random position, size, and rotation