setup()
setup() runs once when the sketch starts. It initializes the canvas, sets fonts, creates the two players, and loads the first map. This is where you configure your game's starting state.
function setup() {
createCanvas(1000, 600);
textFont('Impact');
document.addEventListener('contextmenu', event => event.preventDefault());
let p1Controls = { up: 87, down: 83, left: 65, right: 68 };
let p2Controls = { up: UP_ARROW, down: DOWN_ARROW, left: LEFT_ARROW, right: RIGHT_ARROW, shoot: 190, block: 191 };
p1 = new Player(1, color(50, 200, 255), p1Controls, false);
p2 = new Player(2, color(255, 50, 100), p2Controls, false);
buildMap(currentMapIndex);
}
Line-by-line explanation (8 lines)
🔧 Subcomponents:
createCanvas(1000, 600);
Sets up the drawing surface to 1000×600 pixels—the arena where all game action occurs
p1 = new Player(1, color(50, 200, 255), p1Controls, false);
Creates the first player with ID 1, blue color, WASD controls, and false (not AI)
createCanvas(1000, 600);- Creates a 1000×600 pixel canvas—the visible game arena. All drawing happens on this canvas.
textFont('Impact');- Sets the font for all text rendering to Impact, matching the bold game aesthetic.
document.addEventListener('contextmenu', event => event.preventDefault());- Disables the browser's right-click context menu so right-click blocking works without interruption.
let p1Controls = { up: 87, down: 83, left: 65, right: 68 };- Defines key codes for player 1: 87=W, 83=S, 65=A, 68=D. These are stored as an object and passed to the Player constructor.
let p2Controls = { up: UP_ARROW, down: DOWN_ARROW, left: LEFT_ARROW, right: RIGHT_ARROW, shoot: 190, block: 191 };- Defines key codes for player 2 using arrow keys for movement, 190 (period) for shoot, 191 (slash) for block.
p1 = new Player(1, color(50, 200, 255), p1Controls, false);- Creates player 1 with ID 1, cyan color, keyboard controls, and isAI=false (human player).
p2 = new Player(2, color(255, 50, 100), p2Controls, false);- Creates player 2 with ID 2, magenta color, arrow key controls, and isAI=false (also human by default, but can change in startGame).
buildMap(currentMapIndex);- Loads the starting map (index 0) and positions both players at their spawn points on that map.