setup()
setup() runs once when the sketch loads. It initializes the canvas size, creates all game objects, and sets up the fixed obstacle course. This is where the dino and ground get their starting state before the draw loop begins.
function setup() {
createCanvas(windowWidth, windowHeight);
ground = new Ground();
dino = new Dino();
textSize(24);
textAlign(RIGHT, TOP);
initializeObstacles(); // Initialize the fixed course
}
Line-by-line explanation (6 lines)
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire window, responsive to screen size.
ground = new Ground();- Instantiates the Ground object, which creates the scrolling ground texture that gives the illusion of forward motion.
dino = new Dino();- Creates the Dino object at its starting position, ready to receive inputs and execute actions.
textSize(24);- Sets the font size for all text drawn in the sketch (score, high score, learning list).
textAlign(RIGHT, TOP);- Aligns text to the right and top of the canvas for score display.
initializeObstacles(); // Initialize the fixed course- Populates the obstacles array with all cacti and birds from the fixedObstacleCourse array in their predetermined positions.