setup()
setup() runs once when the sketch first loads. It is the perfect place to initialize the canvas size and load all your game data—in this case, all 22 level designs as arrays. The three-dimensional structure (array of levels, each containing an array of rows, each containing an array of tile values) is a common pattern for storing multi-level games.
function setup() {
createCanvas(600, 600);
level = [
[
[A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A],
[A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A],
...
[G, G, G, G, G, G, G, G, G, G, G, G, G, G, G, G, G, G, G, G],
],
// 21 more levels...
];
}
Line-by-line explanation (2 lines)
createCanvas(600, 600);- Creates a 600×600 pixel canvas—this is the playing field where all tiles and the player are drawn
level = [ [ [ ... ] ] ];- Initializes a 3D array: the outer array holds 22 complete levels, each level is a 20×20 2D array, and each cell contains a number representing a tile type (A=empty, G=ground, D=lava, C=goal, E=bounce pad, F=special goal)