setup()
setup() runs once when the sketch starts. It's where you initialize your canvas size, load saved data, and set up objects that need to exist before the game begins. The high score is saved in localStorage, which persists even after the browser closes.
function setup() {
createCanvas(windowWidth, windowHeight);
pointerX = width / 2;
pointerY = height / 2;
// Load High Score
if (typeof Storage !== "undefined") {
let saved = localStorage.getItem("abyssalHighScore");
if (saved) highScore = int(saved);
}
// Initialize Audio Objects (suspended state)
setupAudio();
resetGame();
textFont("Courier New");
textAlign(CENTER, CENTER);
}
๐ง Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a full-screen canvas that fills the entire browser window
pointerX = width / 2; pointerY = height / 2;
Sets the initial cursor/touch position to the center of the screen
if (typeof Storage !== "undefined") { let saved = localStorage.getItem("abyssalHighScore"); if (saved) highScore = int(saved); }
Retrieves the saved high score from browser local storage if it exists
Line by Line:
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire window. windowWidth and windowHeight are p5.js variables that automatically update if the window is resized
pointerX = width / 2;- Sets the starting horizontal position of the cursor/touch to the center of the canvas
pointerY = height / 2;- Sets the starting vertical position of the cursor/touch to the center of the canvas
if (typeof Storage !== "undefined") {- Checks if the browser supports localStorage (for saving data between sessions). Not all browsers allow this
let saved = localStorage.getItem("abyssalHighScore");- Retrieves the previously saved high score from browser storage using the key "abyssalHighScore"
if (saved) highScore = int(saved);- If a high score was found, converts it from text to a number and stores it in the highScore variable
setupAudio();- Calls the setupAudio function to initialize all the audio oscillators for music and sound effects
resetGame();- Calls resetGame to initialize all game variables like score, health, enemies array, and the tether object
textFont("Courier New");- Sets the font for all text drawn to the canvas to Courier New (a monospace font that looks retro)
textAlign(CENTER, CENTER);- Makes all text align to the center both horizontally and vertically when drawn