setup()
setup() runs once when the sketch first loads. It initializes the canvas, defines all game objects and their initial state, and positions UI elements. This is where you define your game's structure, rooms, and starting conditions.
function setup() {
createCanvas(windowWidth, windowHeight);
noStroke();
textAlign(CENTER, CENTER);
rooms = {
safeZone: { x: 0, y: 0, w: 800, h: 600, color: '#333333' },
floor1Lab: { x: 800, y: 0, w: 600, h: 600, color: '#554444' },
floor2Lab: { x: -600, y: 0, w: 600, h: 600, color: '#445544' }
};
vents = [
{ x: rooms.safeZone.x + 200, y: rooms.safeZone.y + 100, w: 80, h: 40, targetRoom: "floor2Lab", targetPlayerX: rooms.floor2Lab.x + 300, targetPlayerY: rooms.floor2Lab.y + 100 },
{ x: rooms.safeZone.x + 200, y: rooms.safeZone.y + rooms.safeZone.h - 140, w: 80, h: 40, targetRoom: "floor1Lab", targetPlayerX: rooms.floor1Lab.x + 300, targetPlayerY: rooms.floor1Lab.y + rooms.floor1Lab.h - 140 },
{ x: rooms.floor1Lab.x + 500, y: rooms.floor1Lab.y + rooms.floor1Lab.h - 140, w: 80, h: 40, targetRoom: "safeZone", targetPlayerX: rooms.safeZone.x + 600, targetPlayerY: rooms.safeZone.y + rooms.safeZone.h - 140 },
{ x: rooms.floor2Lab.x + 10, y: rooms.floor2Lab.y + 100, w: 80, h: 40, targetRoom: "safeZone", targetPlayerX: rooms.safeZone.x + 100, targetPlayerY: rooms.safeZone.y + 100 },
];
staircases = [
{ x: rooms.floor1Lab.x + 10, y: rooms.floor1Lab.y + rooms.floor1Lab.h - 200, w: 80, h: 100, targetRoom: "safeZone", targetPlayerX: rooms.safeZone.x + rooms.safeZone.w - 100, targetPlayerY: rooms.safeZone.y + rooms.safeZone.h - 100 },
{ x: rooms.floor2Lab.x + rooms.floor2Lab.w - 100, y: rooms.floor2Lab.y + rooms.floor2Lab.h - 200, w: 80, h: 100, targetRoom: "safeZone", targetPlayerX: rooms.safeZone.x + 100, targetPlayerY: rooms.safeZone.y + rooms.safeZone.h - 100 }
];
player = {
x: rooms.safeZone.x + 100,
y: rooms.safeZone.y + rooms.safeZone.h - 50,
w: 30,
h: 50,
vx: 0,
vy: 0,
isJumping: false,
isClimbing: false,
onGround: false
};
joystick = {
baseX: width * 0.2,
baseY: height * 0.8,
radius: 60,
knobX: width * 0.2,
knobY: height * 0.8,
active: false,
touchId: -1,
maxX: width * 0.4
};
jumpButton.x = width * 0.8 - jumpButton.w / 2;
jumpButton.y = height * 0.8 - jumpButton.h / 2;
climbUpButton.x = jumpButton.x;
climbUpButton.y = jumpButton.y - jumpButton.h - 10;
climbDownButton.x = jumpButton.x;
climbDownButton.y = jumpButton.y + jumpButton.h + 10;
for (let i = 0; i < MAX_MONSTERS; i++) {
spawnMonster();
}
}
Line-by-line explanation (9 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a full-screen canvas that fills the entire browser window
rooms = { safeZone: { x: 0, y: 0, w: 800, h: 600, color: '#333333' }, floor1Lab: { x: 800, y: 0, w: 600, h: 600, color: '#554444' }, floor2Lab: { x: -600, y: 0, w: 600, h: 600, color: '#445544' } };
Creates three rooms as objects with position, size, and color—the safe zone is in the center, with labs positioned to the left and right
player = { x: rooms.safeZone.x + 100, y: rooms.safeZone.y + rooms.safeZone.h - 50, w: 30, h: 50, vx: 0, vy: 0, isJumping: false, isClimbing: false, onGround: false };
Creates the player object with position, size, velocity, and state flags for jumping and climbing
for (let i = 0; i < MAX_MONSTERS; i++) { spawnMonster(); }
Creates MAX_MONSTERS enemy instances in lab rooms at the start of the game
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window, allowing the game to adapt to any screen size
noStroke();- Disables outlines on all shapes drawn after this—only filled colors are used
textAlign(CENTER, CENTER);- Sets text alignment so that text() positions anchor at the center of text strings, not the top-left
rooms = { safeZone: { x: 0, y: 0, w: 800, h: 600, color: '#333333' }, floor1Lab: { x: 800, y: 0, w: 600, h: 600, color: '#554444' }, floor2Lab: { x: -600, y: 0, w: 600, h: 600, color: '#445544' } };- Defines three rooms as a dictionary—each room has x, y position in world space, width, height, and a background color hex code
vents = [ { x: rooms.safeZone.x + 200, y: rooms.safeZone.y + 100, w: 80, h: 40, targetRoom: "floor2Lab", targetPlayerX: rooms.floor2Lab.x + 300, targetPlayerY: rooms.floor2Lab.y + 100 }, ... ];- Creates an array of vent objects that act as doorways—each has a position, size, and a target room and player spawn position
player = { x: rooms.safeZone.x + 100, y: rooms.safeZone.y + rooms.safeZone.h - 50, w: 30, h: 50, vx: 0, vy: 0, isJumping: false, isClimbing: false, onGround: false };- Initializes the player as an object with position (x, y), size (w, h), velocity (vx, vy), and boolean flags tracking jump and climb state
joystick = { baseX: width * 0.2, baseY: height * 0.8, radius: 60, knobX: width * 0.2, knobY: height * 0.8, active: false, touchId: -1, maxX: width * 0.4 };- Creates a joystick object positioned at 20% from the left and 80% from the top, with a base circle and a movable knob for directional input
jumpButton.x = width * 0.8 - jumpButton.w / 2;- Positions the jump button horizontally at 80% of the screen width (far right), centered
for (let i = 0; i < MAX_MONSTERS; i++) { spawnMonster(); }- Loops MAX_MONSTERS times, calling spawnMonster() each time to create enemy instances in the lab rooms