setup()
setup() runs once when the sketch starts. It initializes the canvas and buttons, then calls resetGameElements() to set up the initial game state. Notice how each button has both mousePressed and touchStarted handlers—this dual approach ensures the sketch works on desktop and mobile devices.
function setup() {
// Create a canvas that fills the entire window
createCanvas(windowWidth, windowHeight);
// Create the "Orange" button
orangeButton = createButton('Orange');
orangeButton.position(width - 120, 10); // Position it in the top-right
orangeButton.style('font-size', '16px');
orangeButton.style('padding', '10px 15px');
orangeButton.style('background-color', '#4CAF50');
orangeButton.style('color', 'white');
orangeButton.style('border', 'none');
orangeButton.style('border-radius', '5px');
orangeButton.style('cursor', 'pointer');
orangeButton.mousePressed(callPeople); // Attach the function to mousePressed event
orangeButton.touchStarted(function() { // Attach touchStarted handler directly
callPeople();
return false; // Prevent default browser behavior (like scrolling)
});
// Create the "Zombie" button
zombieButton = createButton('Zombie');
zombieButton.position(width - 200, 10); // Position it to the left of the Orange button
zombieButton.style('font-size', '16px');
zombieButton.style('padding', '10px 15px');
zombieButton.style('background-color', '#FF0000'); // Red color for zombie button
zombieButton.style('color', 'white');
zombieButton.style('border', 'none');
zombieButton.style('border-radius', '5px');
zombieButton.style('cursor', 'pointer');
zombieButton.mousePressed(spawnZombie); // Attach the function to mousePressed event
zombieButton.touchStarted(function() { // Attach touchStarted handler directly
spawnZombie();
return false;
});
// NEW: Create the "Van" button
vanButton = createButton('Van');
vanButton.position(width - 280, 10); // Position it to the left of the Zombie button
vanButton.style('font-size', '16px');
vanButton.style('padding', '10px 15px');
vanButton.style('background-color', '#007bff'); // Blue color for van button
vanButton.style('color', 'white');
vanButton.style('border', 'none');
vanButton.style('border-radius', '5px');
vanButton.style('cursor', 'pointer');
vanButton.mousePressed(spawnVan); // Attach the function to mousePressed event
vanButton.touchStarted(function() { // Attach touchStarted handler directly
spawnVan();
return false;
});
// NEW: Create the "Helicopter" button
heliButton = createButton('Heli');
heliButton.position(width - 360, 10); // Position it to the left of the Van button
heliButton.style('font-size', '16px');
heliButton.style('padding', '10px 15px');
heliButton.style('background-color', '#6c757d'); // Gray color for heli button
heliButton.style('color', 'white');
heliButton.style('border', 'none');
heliButton.style('border-radius', '5px');
heliButton.style('cursor', 'pointer');
heliButton.mousePressed(spawnCivilianHelicopter); // Attach the function to mousePressed event
heliButton.touchStarted(function() { // Attach touchStarted handler directly
spawnCivilianHelicopter();
return false;
});
// Reset all game elements
resetGameElements();
}
Line-by-line explanation (6 lines)
createCanvas(windowWidth, windowHeight);- Creates a full-screen canvas that matches the browser window size, allowing the sketch to be responsive.
orangeButton = createButton('Orange');- Creates a button labeled 'Orange' and stores it in the global orangeButton variable so it can be reused later.
orangeButton.position(width - 120, 10);- Positions the Orange button 120 pixels from the right edge and 10 pixels from the top, placing it in the top-right corner.
orangeButton.mousePressed(callPeople);- Attaches the callPeople() function to the button's mouse click event—when clicked, it spawns a new person.
orangeButton.touchStarted(function() { callPeople(); return false; });- Adds touch support so the button works on mobile devices; returning false prevents default browser scrolling behavior.
resetGameElements();- Calls the function that initializes all game elements (houses, orange position, game state) before the draw loop starts.