setup()
setup() runs once when the sketch starts. It initializes all game objects, establishes the canvas, and prepares the audio system. Notice how objects (survivor, tornado, shelter) are defined as plain JavaScript objects with properties rather than class instances—this is a flexible approach when objects don't need methods. The debris array, however, uses a class (Debris) because each debris item needs update(), display(), and checkCollision() methods.
function setup() {
createCanvas(windowWidth, windowHeight);
noStroke();
// Initialize survivor properties
survivor = {
x: width / 2,
y: height / 2,
size: 20,
speed: 5,
isThrown: false, // New: flag for when survivor is being thrown
thrownVx: 0, // New: horizontal velocity when thrown
thrownVy: 0, // New: vertical velocity when thrown
thrownDuration: 0 // New: how long the thrown state lasts
};
// Initialize tornado properties
tornado = {
x: random(width),
y: random(height),
size: 150, // Max width of the tornado visual
active: true,
noiseOffset: 0, // Used to animate the tornado shape
instantDeathZone: 30 // New: radius for instant death from tornado center
};
// Define shelter area
shelter = {
x: width / 4,
y: height / 4,
w: width / 2,
h: height / 2
};
// Create initial debris objects (fewer initially, as new ones will spawn)
for (let i = 0; i < 10; i++) {
debris.push(new Debris('small')); // Initial debris are small
}
// p5.sound setup
// Wind sound using a sine wave oscillator, modulated by tornado proximity
windOsc = new p5.Oscillator('sine');
windOsc.freq(400); // Base frequency
windOsc.amp(0); // Start silent
windOsc.start();
// Debris sound using white noise, modulated by tornado proximity
debrisSound = new p5.Noise('white');
debrisSound.amp(0); // Start silent
debrisSound.start();
fft = new p5.FFT(); // For potential future audio visualization
// User must interact with the page to start audio playback
// This is a browser security measure to prevent unsolicited sound
userStartAudio();
}
Line-by-line explanation (17 lines)
🔧 Subcomponents:
survivor = {
Defines all properties of the player character including position, speed, and thrown state for physics interaction
tornado = {
Stores tornado position, visual size, animation state, and the radius where instant death occurs
shelter = {
Defines the safe rectangular area where the player is protected from tornado damage and instant death
for (let i = 0; i < 10; i++) {
Creates ten small debris objects at the start to have obstacles ready immediately
windOsc = new p5.Oscillator('sine');
Creates and starts two audio sources (sine wave and white noise) that will be modulated during gameplay based on proximity
createCanvas(windowWidth, windowHeight);- Creates a full-screen canvas that responds to the window size, making the game responsive to screen dimensions
noStroke();- Removes outlines from all shapes drawn afterward, creating cleaner visuals for the game
survivor = {- Starts defining the survivor as an object with multiple properties rather than separate variables
x: width / 2,- Places the survivor horizontally at the center of the canvas
isThrown: false,- A boolean flag that tracks whether the survivor is currently being knocked back by a house collision
tornado = {- Defines the tornado as an object with properties that control its appearance and behavior
x: random(width),- Spawns the tornado at a random horizontal position to make each game different
noiseOffset: 0,- Stores a value that animates over time to drive the Perlin noise function, creating swirling motion
shelter = {- Defines a rectangular safe zone where the player avoids all tornado damage and instant death
w: width / 2,- Sets the shelter width to half the screen, making it a sizable target to reach
h: height / 2- Sets the shelter height to half the screen, creating a centered square safe area
debris.push(new Debris('small'));- Creates a new Debris object of type 'small' and adds it to the debris array using the push method
windOsc = new p5.Oscillator('sine');- Creates a sine wave oscillator that will produce the howling wind sound effect when its amplitude is increased
windOsc.freq(400);- Sets the base frequency of the wind sound to 400 Hz, a mid-range pitch that sounds like wind
windOsc.start();- Starts the oscillator playing (silently at first since amplitude is 0)
debrisSound = new p5.Noise('white');- Creates white noise (all frequencies at once) that represents the chaotic sound of debris flying
userStartAudio();- Calls p5.sound's function to request browser permission to play audio—required by modern browsers for security