setup()
setup() runs exactly once when the sketch starts. It's where you initialize the canvas, create UI elements, and prepare variables. rectMode(CENTER) is especially important in games because it makes collision detection math cleaner—all positions refer to the center of shapes rather than their corners.
function setup() {
createCanvas(windowWidth, windowHeight);
rectMode(CENTER);
textAlign(CENTER, CENTER);
textSize(24);
pixelDensity(1);
restartButton = createButton('Restart');
restartButton.position(width / 2 - restartButton.width / 2, height / 2 + 100);
restartButton.mousePressed(restartGame);
restartButton.style('font-size', '24px');
restartButton.style('padding', '10px 20px');
restartButton.hide();
jumpButton = createButton('Jump');
jumpButton.size(120, 60);
const handleJump = () => {
if (gameState === LEVEL_9_OBBY_CHALLENGE && !isJumping) {
playerYVelocity = -jumpStrength;
isJumping = true;
} else if (gameState === LEVEL_9_OBBY_CHALLENGE) {
}
};
jumpButton.mousePressed(handleJump);
jumpButton.touchStarted(handleJump);
jumpButton.hide();
leftButton = createButton('Left');
leftButton.size(120, 60);
leftButton.mousePressed(() => playerDirectionX = -1);
leftButton.mouseReleased(() => playerDirectionX = 0);
leftButton.touchStarted(() => playerDirectionX = -1);
leftButton.touchEnded(() => playerDirectionX = 0);
leftButton.hide();
rightButton = createButton('Right');
rightButton.size(120, 60);
rightButton.mousePressed(() => playerDirectionX = 1);
rightButton.mouseReleased(() => playerDirectionX = 0);
rightButton.touchStarted(() => playerDirectionX = 1);
rightButton.touchEnded(() => playerDirectionX = 0);
rightButton.hide();
jumpButton.elt.addEventListener('contextmenu', (event) => event.preventDefault());
jumpButton.elt.addEventListener('touchstart', (event) => event.preventDefault());
leftButton.elt.addEventListener('contextmenu', (event) => event.preventDefault());
leftButton.elt.addEventListener('touchstart', (event) => event.preventDefault());
rightButton.elt.addEventListener('contextmenu', (event) => event.preventDefault());
rightButton.elt.addEventListener('touchstart', (event) => event.preventDefault());
initializeGame();
hideObbyButtons();
}
Line-by-line explanation (9 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
rectMode(CENTER);
textAlign(CENTER, CENTER);
Creates a full-window canvas, sets rectangles to draw from their center point, and centers all text—critical for consistent positioning throughout the game
restartButton = createButton('Restart');
restartButton.position(width / 2 - restartButton.width / 2, height / 2 + 100);
restartButton.mousePressed(restartGame);
restartButton.style('font-size', '24px');
restartButton.style('padding', '10px 20px');
restartButton.hide();
Creates and styles the restart button, positions it at the center-bottom of the screen, and hides it initially until the game ends
jumpButton = createButton('Jump');
jumpButton.size(120, 60);
const handleJump = () => {
if (gameState === LEVEL_9_OBBY_CHALLENGE && !isJumping) {
playerYVelocity = -jumpStrength;
isJumping = true;
}
};
jumpButton.mousePressed(handleJump);
jumpButton.touchStarted(handleJump);
Creates Jump, Left, and Right buttons for the platforming level, with unified handlers for both mouse and touch input to support mobile devices
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window, making the game responsive to different screen sizes
rectMode(CENTER);- Changes rectangle drawing so that (x, y) is the center point rather than the top-left corner—simplifies collision detection and positioning
textAlign(CENTER, CENTER);- Centers all text both horizontally and vertically around the (x, y) coordinates used in text() calls
pixelDensity(1);- Renders at the device's native pixel density for better performance on touch devices by avoiding unnecessary high-resolution rendering
restartButton = createButton('Restart');- Creates an HTML button element labeled 'Restart' and stores it in the global restartButton variable
restartButton.mousePressed(restartGame);- Attaches the restartGame function to the button—when clicked, restartGame() executes
jumpButton.touchStarted(handleJump);- Registers the handleJump function to fire on touch screen events (not just mouse clicks) so the button works on mobile
jumpButton.elt.addEventListener('contextmenu', (event) => event.preventDefault());- Prevents the browser's right-click context menu from appearing on the button, improving the mobile experience
initializeGame();- Calls the function that sets up all game variables and positions to their starting state