setup()
setup() runs exactly once when the sketch starts. Use it to initialize your canvas, define constants, and create the objects your game needs. After setup(), draw() takes over and runs repeatedly at 60 frames per second.
function setup() {
createCanvas(windowWidth, windowHeight);
groundY = height - 50; // Define the ground level
gameSpeed = 6; // Initial speed of the game
dino = new Dino();
resetGame(); // Initialize game state
}
Line-by-line explanation (5 lines)
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window so the game responds to any screen size
groundY = height - 50;- Sets the ground line 50 pixels from the bottom of the canvas—this is where the dino stands and cacti spawn
gameSpeed = 6;- Stores the initial speed at which cacti move left across the screen; you can increase this to make the game harder over time
dino = new Dino();- Creates a new Dino object at position (50, groundY) with default jump and gravity properties
resetGame();- Calls the resetGame() function to initialize score, cacti array, and game state flags