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)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
/**
* 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();
});