Webbserver - Love Blomberg

Show sourcecode

The following files exists in this folder. Click to view.

public_html/gamla-kurser/webbutv2/projekt/snake_br/js/

cookie-clicker.js
fidget-spinner.js
snake.js

cookie-clicker.js

118 lines ASCII Windows (CRLF)
/**
 * Cookie Clicker Game
 */

// Wait for DOM to fully load before accessing elements
document.addEventListener('DOMContentLoaded', () => {
 // Game variables
 let cookies = 0;
 let autoClickerPrice = 10;
 let autoClickerCount = 0;
 let autoClickerInterval = null;

 // DOM elements
 const cookieButton = document.getElementById('cookieButton');
 const cookieCount = document.getElementById('cookieCount');
 const autoClickerBtn = document.getElementById('autoClickerBtn');

 // Check if elements exist
 if (!cookieButton || !cookieCount || !autoClickerBtn) {
   console.error('Cookie game elements not found');
   return;
 }

 console.log('Cookie clicker initialized'); // Debug line

 // Handle cookie click
 function clickCookie() {
   cookies++;
   updateDisplay();
   
   // Add a click animation
   cookieButton.style.transform = 'scale(0.95)';
   setTimeout(() => {
     cookieButton.style.transform = 'scale(1)';
   }, 100);
 }

 // Update the display
 function updateDisplay() {
   cookieCount.textContent = cookies;
   
   // Enable/disable auto clicker button
   autoClickerBtn.disabled = cookies < autoClickerPrice;
 }

 // Buy an auto clicker
 function buyAutoClicker() {
   if (cookies >= autoClickerPrice) {
     cookies -= autoClickerPrice;
     autoClickerCount++;
     
     // Increase price for next auto clicker
     autoClickerPrice = Math.floor(autoClickerPrice * 1.5);
     autoClickerBtn.textContent = `Auto Clicker (${autoClickerPrice} cookies)`;
     
     updateDisplay();
     updateAutoClickers();
     saveGameData();
   }
 }

 // Update auto clickers
 function updateAutoClickers() {
   // Clear existing interval
   if (autoClickerInterval) {
     clearInterval(autoClickerInterval);
   }
   
   // Set new interval if we have auto clickers
   if (autoClickerCount > 0) {
     autoClickerInterval = setInterval(() => {
       cookies += autoClickerCount;
       updateDisplay();
       saveGameData();
     }, 1000);
   }
 }

 // Save game data
 function saveGameData() {
   const gameData = {
     cookies,
     autoClickerPrice,
     autoClickerCount
   };
   
   localStorage.setItem('cookieClickerData', JSON.stringify(gameData));
 }

 // Load game data
 function loadGameData() {
   const savedData = localStorage.getItem('cookieClickerData');
   
   if (savedData) {
     try {
       const gameData = JSON.parse(savedData);
       cookies = gameData.cookies || 0;
       autoClickerPrice = gameData.autoClickerPrice || 10;
       autoClickerCount = gameData.autoClickerCount || 0;
       
       autoClickerBtn.textContent = `Auto Clicker (${autoClickerPrice} cookies)`;
       updateDisplay();
     } catch (e) {
       console.error('Error loading saved game data', e);
     }
   }
 }

 // Add event listeners
 cookieButton.addEventListener('click', clickCookie);
 autoClickerBtn.addEventListener('click', buyAutoClicker);

 // Load saved game data
 loadGameData();

 // Start auto clickers if any
 updateAutoClickers();
});