Player constructor()
The constructor is called once per player in setup(). It stores all the data about that player as properties (this.x, this.team, etc.). This data persists across frames and gets updated by move(), display(), and game logic in draw().
constructor(x, y, teamColor, controls) {
this.x = x;
this.y = y;
this.size = 30; // Player circle size
this.speed = 4; // Player movement speed
this.team = teamColor;
this.hasFlag = false;
this.flagCarried = null; // Reference to the flag object if carrying
this.controls = controls; // Object like { up: 'W', down: 'S', left: 'A', right: 'D' }
this.originalX = x;
this.originalY = y;
}
Line-by-line explanation (11 lines)
🔧 Subcomponents:
this.x = x;
this.y = y;
Stores the player's starting position on the canvas
this.team = teamColor;
Stores the player's team color (red or blue) used for display
this.hasFlag = false;
this.flagCarried = null;
Tracks whether this player is carrying a flag and which one
this.controls = controls;
Stores the keyboard key mappings (W/A/S/D for red, arrows for blue)
constructor(x, y, teamColor, controls) {- Constructor function that runs when a new Player object is created; receives spawn position, team color, and keyboard controls
this.x = x;- Stores the player's horizontal position; this.x will be updated each frame to create movement
this.y = y;- Stores the player's vertical position; will be updated each frame to create movement
this.size = 30;- Stores the player's visual diameter in pixels; used for drawing and collision detection
this.speed = 4;- Stores how many pixels the player moves per frame when a movement key is pressed
this.team = teamColor;- Stores the player's team color (passed in from setup); used to draw them the correct color and identify which flag they can score
this.hasFlag = false;- Boolean flag that starts as false; becomes true when the player picks up the opponent's flag
this.flagCarried = null;- Stores a reference to the actual Flag object this player is carrying; null means they are not carrying anything
this.controls = controls;- Stores the control mapping object (e.g., {up: 'W', down: 'S'} for red) so move() knows which keys control this player
this.originalX = x;- Saves the spawn position so respawn() can send the player back to their base when tagged or after scoring
this.originalY = y;- Saves the spawn position's y-coordinate for the same respawn purpose