setup()
setup() runs exactly once when the sketch starts. It's the right place to create the canvas and call any one-time initialization function like resetGame().
function setup() {
createCanvas(windowWidth, windowHeight);
resetGame(); // Initialize the game
}
Line-by-line explanation (2 lines)
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window, using p5's built-in windowWidth and windowHeight variables.
resetGame(); // Initialize the game- Calls the resetGame function to set up the snake's starting position, score, and first food item before drawing begins.