setup()
setup() runs once when the sketch starts and is the perfect place to initialize all your game variables and canvas configuration. By storing the player data in an object, we can easily add new properties like direction or score later.
function setup() {
createCanvas(windowWidth, windowHeight);
player = {
x: width / 2,
y: height - 120,
w: 40,
h: 80,
speed: 7
};
resetGame(false); // set initial values but stay on start screen
}
Line-by-line explanation (6 lines)
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window, allowing the game to be responsive to different screen sizes.
player = {- Initializes the player as an object with x/y position, width/height dimensions (the hitbox), and a movement speed value.
x: width / 2,- Places the player horizontally at the center of the canvas.
y: height - 120,- Places the player near the bottom of the screen where the hallway starts, leaving room for the UI above.
speed: 7- Defines how many pixels the player moves per frame when the player presses movement keys.
resetGame(false);- Calls resetGame with false so it initializes game variables (score, lives, enemies, collectibles) but keeps gameState as 'start' to display the title screen.