setup()
setup() runs exactly once when the sketch starts. Use it to initialize your canvas, set starting positions, and prepare any variables you need. The special variables windowWidth and windowHeight always contain your current browser window dimensions.
function setup() {
createCanvas(windowWidth, windowHeight); // Tuval boyutunu pencere boyutuna ayarla
// Tip: Kullanıcının pencere boyutuna uyumlu bir tuval için windowWidth/windowHeight kullanın
// Oyuncuyu tuvalin ortasına yerleştir
playerX = width / 2;
playerY = height / 2;
}
Line-by-line explanation (3 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a canvas that spans the entire browser window width and height
playerX = width / 2;
playerY = height / 2;
Positions the player square at the exact center of the canvas
createCanvas(windowWidth, windowHeight);- Creates a canvas with dimensions matching your browser window—it will stretch to fill the entire screen
playerX = width / 2;- Sets the player's starting horizontal position to the middle of the canvas (width divided by 2)
playerY = height / 2;- Sets the player's starting vertical position to the middle of the canvas (height divided by 2)