setup()
setup() runs once when the sketch starts. Here you initialize the canvas size, create game objects, and set up the starting state. This is where you prepare everything the draw loop will use.
function setup() {
createCanvas(windowWidth, windowHeight);
localPlayerId = 'player_' + floor(random(100000)); // Unique ID for this client's player
localPlayer = new Player(localPlayerId, width / 2, height - 80);
players[localPlayerId] = localPlayer;
noCursor(); // Hide the cursor for better player control
userStartAudio(); // Good practice to ensure audio works if added later
}
Line-by-line explanation (6 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a canvas that fills the entire browser window so the game uses all available screen space
localPlayerId = 'player_' + floor(random(100000));
Generates a unique identifier for this player that would be used by a real server to distinguish players
localPlayer = new Player(localPlayerId, width / 2, height - 80);
Instantiates the Player object at the center-bottom of the canvas
createCanvas(windowWidth, windowHeight);- Creates a canvas matching your entire browser window size, ensuring the game fills the screen
localPlayerId = 'player_' + floor(random(100000));- Generates a unique ID like 'player_47382' that identifies this client's player to the simulated server
localPlayer = new Player(localPlayerId, width / 2, height - 80);- Creates your player spaceship object at the horizontal center and near the bottom of the canvas
players[localPlayerId] = localPlayer;- Stores the local player in the players dictionary so it can be accessed and updated later
noCursor();- Hides the mouse cursor for a cleaner, more immersive game experience
userStartAudio();- Prepares the audio system (good practice for games that might add sound effects later)