setup()
setup() runs once when the sketch starts. It initializes the canvas size, sets up both players with their control schemes, and loads the first map. The isAI parameter on Player 2 will be changed to true during startGame() if single-player mode is selected.
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:
p1 = new Player(1, color(50, 200, 255), p1Controls, false);
Creates the human-controlled blue player with WASD controls and mouse aiming
p2 = new Player(2, color(255, 50, 100), p2Controls, false);
Creates the second player (red) with arrow key controls—can be human or AI depending on game mode
createCanvas(1000, 600);- Creates a 1000-pixel-wide by 600-pixel-tall canvas—the game's playing field
textFont('Impact');- Sets the font for all text to Impact (a bold, game-like font)
document.addEventListener('contextmenu', event => event.preventDefault());- Disables the right-click menu so the right mouse button triggers parry without interference
let p1Controls = { up: 87, down: 83, left: 65, right: 68 };- Defines Player 1 controls as key codes: 87=W, 83=S, 65=A, 68=D (WASD layout)
let p2Controls = { up: UP_ARROW, down: DOWN_ARROW, left: LEFT_ARROW, right: RIGHT_ARROW, shoot: 190, block: 191 };- Defines Player 2 controls using arrow keys and 190=Period and 191=Slash for shoot/parry
p1 = new Player(1, color(50, 200, 255), p1Controls, false);- Creates a blue Player object with ID 1, WASD controls, and isAI=false (human-controlled)
p2 = new Player(2, color(255, 50, 100), p2Controls, false);- Creates a red Player object with ID 2, arrow controls, and isAI=false (but will be set to true in single-player)
buildMap(currentMapIndex);- Loads the first map (index 0), populates platforms, and positions both players at spawn points