// Game variables
let points = 0;
let goldenCubes = 0;
let cubeValue = 1;
let cubeValueCost = 10;
let spawnInterval = 2000; // milliseconds
let spawnCost = 20;
let autoCollectLevel = 0;
let autoCollectCost = 15;
let autoCollectInterval = null;
let rarerCubesUnlocked = false;
let rarerCost = 50;
let goldenValueCost = 5;
// DOM elements
const pointsElem = document.getElementById('points');
const goldenCubesElem = document.getElementById('goldenCubes');
const gameContainer = document.getElementById('gameContainer');
const increaseValueButton = document.getElementById('increaseValue');
const increaseSpawnRateButton = document.getElementById('increaseSpawnRate');
const unlockRarerCubesButton = document.getElementById('unlockRarerCubes');
const autoCollectButton = document.getElementById('autoCollect');
const goldenIncreaseValueButton = document.getElementById('goldenIncreaseValue');
const valueCostElem = document.getElementById('valueCost');
const spawnCostElem = document.getElementById('spawnCost');
const rarerCostElem = document.getElementById('rarerCost');
const autoCollectCostElem = document.getElementById('autoCollectCost');
const goldenValueCostElem = document.getElementById('goldenValueCost');
// Update cost display
function updateCosts() {
valueCostElem.textContent = cubeValueCost;
spawnCostElem.textContent = spawnCost;
rarerCostElem.textContent = rarerCost;
autoCollectCostElem.textContent = autoCollectCost;
goldenValueCostElem.textContent = goldenValueCost;
}
// Update stats display
function updateStats() {
pointsElem.textContent = Math.floor(points);
goldenCubesElem.textContent = goldenCubes;
}
// Spawn cubes
function spawnCube() {
const cube = document.createElement('div');
cube.classList.add('cube');
// Determine cube type
let cubeType = 'white'; // Basic cube
if (rarerCubesUnlocked) {
const rand = Math.random();
if (rand < 0.05) {
cubeType = 'blue'; // Rare cube
} else if (rand < 0.2) {
cubeType = 'green'; // Uncommon cube
}
}
// Golden cube chance
if (Math.random() < 0.01) { // 1/100 chance
cubeType = 'golden';
}
cube.classList.add(cubeType);
// Random horizontal position
cube.style.left = Math.random() * (gameContainer.clientWidth - 30) + 'px';
gameContainer.appendChild(cube);
// Animate cube falling
let fallSpeed = 1 + Math.random() * 2; // Random speed between 1 and 3
let position = -30;
const fallInterval = setInterval(() => {
position += fallSpeed;
cube.style.top = position + 'px';
// Remove cube if it reaches the bottom
if (position > gameContainer.clientHeight) {
clearInterval(fallInterval);
gameContainer.removeChild(cube);
}
}, 20);
// Click event to collect cube
cube.addEventListener('click', () => {
collectCube(cubeType);
clearInterval(fallInterval);
gameContainer.removeChild(cube);
});
}
// Collect cube
function collectCube(type) {
let value = cubeValue;
if (type === 'green') {
value *= 2; // Uncommon cubes are worth double
} else if (type === 'blue') {
value *= 5; // Rare cubes are worth five times
} else if (type === 'golden') {
goldenCubes += 1;
updateStats();
return;
}
points += value;
updateStats();
}
// Auto-collect cubes
function autoCollectCubes() {
const cubes = gameContainer.getElementsByClassName('cube');
while (cubes.length > 0) {
const cubeType = cubes[0].classList.contains('green') ? 'green' :
cubes[0].classList.contains('blue') ? 'blue' :
cubes[0].classList.contains('golden') ? 'golden' : 'white';
collectCube(cubeType);
gameContainer.removeChild(cubes[0]);
}
}
// Upgrade functions
increaseValueButton.addEventListener('click', () => {
if (points >= cubeValueCost) {
points -= cubeValueCost;
cubeValue += 1;
cubeValueCost *= 1.5;
cubeValueCost = Math.floor(cubeValueCost);
updateCosts();
updateStats();
}
});
increaseSpawnRateButton.addEventListener('click', () => {
if (points >= spawnCost) {
points -= spawnCost;
spawnInterval *= 0.9; // Increase spawn rate by decreasing interval
spawnCost *= 1.7;
spawnCost = Math.floor(spawnCost);
clearInterval(spawnIntervalID);
spawnIntervalID = setInterval(spawnCube, spawnInterval);
updateCosts();
updateStats();
}
});
unlockRarerCubesButton.addEventListener('click', () => {
if (points >= rarerCost && !rarerCubesUnlocked) {
points -= rarerCost;
rarerCubesUnlocked = true;
unlockRarerCubesButton.disabled = true;
updateStats();
}
});
autoCollectButton.addEventListener('click', () => {
if (points >= autoCollectCost) {
points -= autoCollectCost;
autoCollectLevel += 1;
autoCollectCost *= 2;
autoCollectCost = Math.floor(autoCollectCost);
// Increase auto-collect frequency
if (autoCollectInterval) {
clearInterval(autoCollectInterval);
}
const intervalTime = Math.max(1000 / autoCollectLevel, 100);
autoCollectInterval = setInterval(autoCollectCubes, intervalTime);
updateCosts();
updateStats();
}
});
goldenIncreaseValueButton.addEventListener('click', () => {
if (goldenCubes >= goldenValueCost) {
goldenCubes -= goldenValueCost;
cubeValue += 5; // Increase cube value significantly
goldenValueCost *= 2;
goldenValueCost = Math.floor(goldenValueCost);
updateCosts();
updateStats();
}
});
// Start spawning cubes
let spawnIntervalID = setInterval(spawnCube, spawnInterval);
updateCosts();
updateStats();