setup()
setup() runs once when the sketch starts. It initializes the canvas, sets starting variable values, and creates all the UI elements (buttons, displays, containers) that the player will interact with. Everything you set up here persists throughout the game.
function setup() {
// Create a responsive canvas
createCanvas(windowWidth, windowHeight);
// Initialize gun position *after* createCanvas() to use width and height
gunX = width / 2;
gunY = height - gunHeight - 20; // Position gun above the bottom of the screen
// Initialize all game elements
initializeGameElements();
// Create Upgrade Button (now opens the shop to select guns)
upgradeButton = createButton('Select Gun'); // Changed button text
upgradeButton.position(10, 50);
console.log('Upgrade button created and positioned:', upgradeButton.x, upgradeButton.y);
upgradeButton.mousePressed(openShop);
upgradeButton.style('font-size', '16px');
upgradeButton.style('padding', '8px 12px');
upgradeButton.style('background-color', '#4CAF50');
upgradeButton.style('color', 'white');
upgradeButton.style('border', 'none');
upgradeButton.style('border-radius', '5px');
upgradeButton.style('cursor', 'pointer');
// Create Upgrade Cost Display (now just a general info div)
upgradeCostDisplay = createDiv('All guns are free!'); // Info text
upgradeCostDisplay.position(upgradeButton.x + upgradeButton.width + 10, 50);
upgradeCostDisplay.style('font-size', '16px');
upgradeCostDisplay.style('color', 'black');
// updateUpgradeButtonState() is no longer needed here as button is always active
// Create Upgrade Shop Container (initially hidden)
shopContainer = createDiv('');
shopContainer.id('shop-container'); // Add an ID for CSS styling
shopContainer.style('display', 'none'); // Initially hidden
shopContainer.style('position', 'absolute');
shopContainer.style('left', '50%');
shopContainer.style('top', '50%');
shopContainer.style('transform', 'translate(-50%, -50%)');
shopContainer.style('background-color', 'white');
shopContainer.style('border', '2px solid black');
shopContainer.style('padding', '20px');
shopContainer.style('z-index', '1000'); // Ensure it's on top of the canvas
shopContainer.style('width', '80%');
shopContainer.style('max-width', '500px');
shopContainer.style('box-shadow', '0 0 10px rgba(0,0,0,0.5)');
shopContainer.style('border-radius', '10px');
shopContainer.style('text-align', 'center');
shopContainer.style('font-family', 'sans-serif');
console.log('Shop container created and hidden.');
// Add shop title
shopContainer.html('<h2>Taco Shooter Guns</h2>', true); // Changed title
// Add close button (X) at the top
let closeXButton = createButton('X');
closeXButton.parent(shopContainer);
closeXButton.position(10, 10);
closeXButton.style('font-size', '18px');
closeXButton.style('background', 'none');
closeXButton.style('border', 'none');
closeXButton.style('cursor', 'pointer');
closeXButton.style('font-weight', 'bold');
closeXButton.style('color', '#333');
closeXButton.mousePressed(closeShop);
// Add a more prominent "Close Shop" button at the bottom
closeShopButton = createButton('Close Shop');
closeShopButton.parent(shopContainer);
closeShopButton.style('margin-top', '20px'); // Add some space
closeShopButton.style('font-size', '16px');
closeShopButton.style('padding', '8px 12px');
closeShopButton.style('background-color', '#DC3545'); // Red color
closeShopButton.style('color', 'white');
closeShopButton.style('border', 'none');
closeShopButton.style('border-radius', '5px');
closeShopButton.style('cursor', 'pointer');
closeShopButton.mousePressed(closeShop);
// Create Restart Button (initially hidden)
restartButton = createButton('Restart Game');
restartButton.position(width / 2 - 75, height / 2 + 50);
restartButton.mousePressed(restartGame);
restartButton.hide(); // Hidden at start
restartButton.style('font-size', '20px');
restartButton.style('padding', '10px 20px');
restartButton.style('background-color', '#007BFF');
restartButton.style('color', 'white');
restartButton.style('border', 'none');
restartButton.style('border-radius', '5px');
restartButton.style('cursor', 'pointer');
// Add gun listings (will be populated dynamically)
populateShop();
// Apply initial gun properties (Basic Blaster)
applyGunProperties(1);
}
Line-by-line explanation (10 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a responsive canvas that fills the entire window
gunX = width / 2;
gunY = height - gunHeight - 20;
Centers the gun horizontally and positions it near the bottom of the screen
upgradeButton = createButton('Select Gun');
Creates the UI button that opens the gun shop
shopContainer = createDiv('');
Creates a hidden overlay div that will display available guns
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window; windowWidth and windowHeight are p5.js variables that contain the current window dimensions
gunX = width / 2;- Sets the gun's horizontal position to the center of the canvas (width is half the canvas width)
gunY = height - gunHeight - 20;- Positions the gun near the bottom of the canvas, accounting for the gun's height and a 20-pixel margin from the edge
initializeGameElements();- Calls a helper function that populates the tacos, bones, and garlics arrays with new objects
upgradeButton = createButton('Select Gun');- Creates a clickable button with the text 'Select Gun' that will open the gun shop when clicked
upgradeButton.mousePressed(openShop);- Registers the openShop() function to be called whenever the upgrade button is clicked
shopContainer = createDiv('');- Creates an invisible div element that will contain the gun shop; it is initially hidden with display: none
shopContainer.style('display', 'none');- Sets the shop to be invisible when the game starts; it will be shown when the player clicks Select Gun
populateShop();- Calls a helper function that fills the shop container with gun listings and equip buttons
applyGunProperties(1);- Applies the properties of gun level 1 (Basic Blaster) to the global bullet and gun variables