setup()
setup() runs once when the sketch starts. It is the perfect place to initialize your canvas, create objects, load saved data, and set starting values. Notice how we store the canvas in a global variable (gameCanvas) so we can resize it later when the admin panel opens and closes.
function setup() {
gameCanvas = createCanvas(windowWidth, windowHeight * 0.85);
gameCanvas.position(0, (windowHeight - gameCanvas.height) / 2);
createAdminPanel();
loadUnlockedOutfits();
if (!outfits[currentOutfitIndex].unlocked) {
currentOutfitIndex = outfits.findIndex(o => o.unlocked) || 0;
}
snake = new Snake();
food = new Food();
food.pickLocation();
frameRate(10);
adminPanelToggle.hide();
}
Line-by-line explanation (9 lines)
🔧 Subcomponents:
gameCanvas = createCanvas(windowWidth, windowHeight * 0.85);
Creates the main drawing canvas at 85% of window height and stores its reference for later resizing
if (!outfits[currentOutfitIndex].unlocked) {
currentOutfitIndex = outfits.findIndex(o => o.unlocked) || 0;
}
Ensures the player starts with an unlocked outfit; if the current choice is locked, switches to the first unlocked one
gameCanvas = createCanvas(windowWidth, windowHeight * 0.85);- Creates the game canvas at full width and 85% of window height, then stores it in gameCanvas so we can resize it later
gameCanvas.position(0, (windowHeight - gameCanvas.height) / 2);- Vertically centers the canvas on the page by positioning it at the midpoint between top and bottom
createAdminPanel();- Builds the developer control panel with buttons and sliders for testing (speed, score, lives, etc.)
loadUnlockedOutfits();- Reads localStorage to restore which outfits the player has already unlocked in previous sessions
if (!outfits[currentOutfitIndex].unlocked) { currentOutfitIndex = outfits.findIndex(o => o.unlocked) || 0; }- Safety check: if the player's selected outfit was somehow locked, switches to the first unlocked outfit (or outfit 0 as fallback)
snake = new Snake();- Creates a new Snake object at position (0, 0) with zero length, ready for the player to control
food = new Food();- Creates a Food object; pickLocation() will place it at a random grid cell
frameRate(10);- Sets the draw loop to run 10 times per second, which is also the speed the snake moves
adminPanelToggle.hide();- Hides the gear icon at the start because the admin panel is not needed on the menu screen