its not working when u press z on the powerup u have man cmon fix it all add all the functions levels and all
#game-container {
position: relative;
width: 800px;
height: 400px;
background-color: #333;
overflow: hidden;
border: 2px solid #444;
transition: background-color 0.5s, box-shadow 0.5s;
}
#player {
position: absolute;
bottom: 0;
left: 50px;
width: 30px;
height: 30px;
background-color: #4CAF50;
animation: idle 1s infinite alternate;
}
@keyframes idle {
from { transform: translateY(0); }
to { transform: translateY(2px); }
}
.obstacle, .enemy, .bullet, .boss, .ladder, .cave, .water, .enemy-bullet, .collectible,
.special-zone, .platform, .end-door, .boss-door, .power-up {
position: absolute;
bottom: 0;
}
.obstacle, .platform, .boss-area {
width: 30px;
height: 30px;
background-color: #f44336;
}
.ladder {
width: 30px;
height: 60px;
background-color: #8B4513;
}
.cave {
width: 100px;
height: 80px;
background-color: #2F4F4F;
}
.water {
width: 100px;
height: 60px;
background-color: #1E90FF;
}
.enemy {
width: 25px;
height: 25px;
background-color: #FF5733;
}
.bullet, .enemy-bullet {
width: 10px;
height: 5px;
background-color: #FFD700;
}
.boss {
width: 60px;
height: 60px;
background-color: #FF4500;
}
.power-up-ice, .power-up-fire, .power-up-levitate, .power-up-hypno, .power-up-boss {
width: 20px;
height: 20px;
position: absolute;
bottom: 0;
}
.power-up-ice { background-color: #00f; }
.power-up-fire { background-color: #f00; }
.power-up-levitate { background-color: #0f0; }
.power-up-hypno { background-color: #f0f; }
.power-up-boss {
background: linear-gradient(90deg, red, orange, yellow, green, blue, indigo, violet);
animation: rainbow 3s infinite linear;
}
@keyframes rainbow {
0% { background-position: 0%; }
100% { background-position: 100%; }
}
.collectible {
width: 15px;
height: 15px;
background-color: #FFD700;
}
.special-zone {
width: 100px;
height: 50px;
background-color: #6A5ACD;
}
.end-door, .boss-door {
width: 30px;
height: 60px;
background-color: #FFFF00;
}
#score, #lives, #level, #timer {
position: absolute;
top: 10px;
font-size: 16px;
}
#score {
left: 10px;
}
#lives {
right: 10px;
}
#level {
left: 150px;
}
#timer {
left: 250px;
}
#instructions, #cutscene, #level-intro, #boss-fight, #game-over {
position: absolute;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
background-color: rgba(0, 0, 0, 0.8);
color: #fff;
flex-direction: column;
font-size: 18px;
z-index: 10;
}
#instructions button, #cutscene button, #level-intro button, #boss-fight button, #game-over button {
margin-top: 10px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
background-color: #555;
color: #fff;
}
#cutscene, #level-intro, #boss-fight, #game-over {
display: none;
}
@keyframes splash-screen {
0% { opacity: 0; }
50% { opacity: 1; }
100% { opacity: 0; }
}
#splash-screen {
display: none;
position: absolute;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.9);
color: #fff;
font-size: 24px;
display: flex;
justify-content: center;
align-items: center;
animation: splash-screen 3s linear infinite;
}
</style>
<script>
const gameContainer = document.getElementById('game-container');
const player = document.getElementById('player');
const scoreDisplay = document.getElementById('score');
const livesDisplay = document.getElementById('lives');
const levelDisplay = document.getElementById('level');
const timerDisplay = document.getElementById('timer');
const instructions = document.getElementById('instructions');
const cutscene = document.getElementById('cutscene');
const levelIntro = document.getElementById('level-intro');
const levelText = document.getElementById('level-text');
const splashScreen = document.getElementById('splash-screen');
const bossFightScreen = document.getElementById('boss-fight');
const gameOverScreen = document.getElementById('game-over');
let playerPositionX = 50;
let playerPositionY = 0;
let playerVelocityX = 0;
let playerVelocityY = 0;
let playerMaxSpeed = 5;
let isJumping = false;
let isPaused = false;
let canShoot = true;
let gameInterval;
let obstacles = [];
let enemies = [];
let bullets = [];
let enemyBullets = [];
let powerUps = [];
let ladders = [];
let caves = [];
let waters = [];
let collectibles = [];
let specialZones = [];
let platforms = [];
let levelDoors = [];
let bosses = [];
let score = 0;
let lives = 3;
let level = 1;
let secretBossActive = false;
let currentPowerUp = null;
let powerUpTimer = 0;
const gravity = 0.5;
const jumpStrength = 12;
const maxLevels = 5;
let bossFight = false;
let movingLeft = false, movingRight = false;
document.addEventListener('keydown', handleKeydown);
document.addEventListener('keyup', handleKeyup);
function handleKeydown(e) {
switch (e.key) {
case 'ArrowLeft':
movingLeft = true;
break;
case 'ArrowRight':
movingRight = true;
break;
case 'ArrowUp':
if (!isJumping) jump();
break;
case ' ':
if (canShoot) {
shoot();
canShoot = false;
setTimeout(() => canShoot = true, 500); // 0.5 second cooldown
}
break;
case 'z':
case 'Z':
usePowerUp();
break;
case 'e':
case 'E':
interact();
break;
case 'p':
case 'P':
togglePause();
break;
}
}
function handleKeyup(e) {
switch (e.key) {
case 'ArrowLeft':
movingLeft = false;
break;
case 'ArrowRight':
movingRight = false;
break;
}
}
function jump() {
if (!isJumping) {
playerVelocityY = jumpStrength;
isJumping = true;
}
}
function shoot() {
const bullet = document.createElement('div');
bullet.classList.add('bullet');
let bulletDirection = movingRight ? 1 : -1;
bullets.push({ element: bullet, direction: bulletDirection });
bullet.style.left = (bulletDirection > 0 ? playerPositionX + player.offsetWidth : playerPositionX) + 'px';
bullet.style.bottom = (playerPositionY + player.offsetHeight / 2) + 'px';
gameContainer.appendChild(bullet);
}
function startGame() {
instructions.style.display = 'none';
levelIntro.style.display = 'flex';
levelText.textContent = `Level 1: Get Ready for Adventure!`;
resetGame();
}
function startLevel() {
levelIntro.style.display = 'none';
gameInterval = setInterval(gameLoop, 1000 / 60);
}
function resetGame() {
score = 0;
lives = 3;
level = 1;
playerPositionX = 50;
playerPositionY = 0;
playerVelocityX = 0;
playerVelocityY = 0;
isJumping = false;
clearAllElements();
updateDisplay();
}
function updateDisplay() {
scoreDisplay.textContent = `Score: ${score}`;
livesDisplay.textContent = `Lives: ${lives}`;
levelDisplay.textContent = `Level: ${level}`;
timerDisplay.textContent = `Timer: ${powerUpTimer}`;
}
function updatePlayerPosition() {
if (movingLeft) {
playerVelocityX = -playerMaxSpeed;
} else if (movingRight) {
playerVelocityX = playerMaxSpeed;
} else {
playerVelocityX = 0;
}
playerPositionX += playerVelocityX;
playerPositionX = Math.max(0, Math.min(gameContainer.offsetWidth - player.offsetWidth, playerPositionX));
player.style.left = playerPositionX + 'px';
let onPlatform = false;
platforms.forEach(platform => {
if (checkCollision(player, platform) && playerVelocityY < 0) {
playerPositionY = parseInt(platform.style.bottom) + platform.offsetHeight;
playerVelocityY = 0;
isJumping = false;
onPlatform = true;
}
});
if (!onPlatform || isJumping || playerPositionY > 0) {
playerVelocityY -= gravity;
playerPositionY += playerVelocityY;
if (playerPositionY <= 0) {
playerPositionY = 0;
isJumping = false;
}
}
player.style.bottom = playerPositionY + 'px';
}
function spawnEntities() {
if (!isPaused) {
if (Math.random() < 0.01) spawnObstacle();
if (Math.random() < 0.005) spawnEnemy();
if (Math.random() < 0.002 && !currentPowerUp) spawnPowerUps();
if (Math.random() < 0.002) spawnCollectible();
if (Math.random() < 0.001) spawnPlatform();
if (Math.random() < 0.001) spawnSpecialZone();
if (Math.random() < 0.002) spawnEndDoor();
}
}
function moveElements() {
moveArray(obstacles, 2, 15);
moveArray(enemies, 1, 30);
moveArray(powerUps, 2, 0, true);
moveArray(collectibles, 1, 5);
moveArray(platforms, 1, 0);
moveArray(specialZones, 0, 50);
moveArray(levelDoors, 0, 0);
moveBullets();
if (Math.random() < 0.002) enemiesFireBullets();
}
function spawnObstacle() {
createElement('obstacle', obstacles, gameContainer.offsetWidth);
}
function spawnEnemy() {
createElement('enemy', enemies, gameContainer.offsetWidth);
}
function spawnPowerUps() {
const powerUpTypes = ['ice', 'fire', 'levitate', 'hypno'];
const type = powerUpTypes[Math.floor(Math.random() * powerUpTypes.length)];
const powerUp = createElement(`power-up-${type}`, powerUps, gameContainer.offsetWidth, type);
powerUp.style.bottom = '0px';
}
function spawnCollectible() {
const collectible = createElement('collectible', collectibles, gameContainer.offsetWidth);
collectible.style.bottom = (Math.random() * 100 + 50) + 'px';
}
function spawnPlatform() {
const platform = createElement('platform', platforms, gameContainer.offsetWidth);
platform.style.bottom = Math.floor(Math.random() * 200 + 100) + 'px';
}
function spawnSpecialZone() {
const specialZone = createElement('special-zone', specialZones, gameContainer.offsetWidth);
specialZone.style.bottom = (Math.random() * 50 + 50) + 'px';
}
function spawnEndDoor() {
if (Math.random() < 0.2) {
const endDoor = createElement('end-door', levelDoors, gameContainer.offsetWidth);
endDoor.style.bottom = '0px';
}
}
function createDayNightCycle() {
if (Math.random() < 0.0001) {
gameContainer.style.backgroundColor = gameContainer.style.backgroundColor === '#333' ? '#111' : '#333';
}
}
function createElement(className, array, leftPosition, type = '') {
const element = document.createElement('div');
element.classList.add(className);
if (type) element.setAttribute('data-type', type);
element.style.left = leftPosition + 'px';
gameContainer.appendChild(element);
array.push(element);
return element;
}
function moveArray(array, speed, scoreValue, isPowerUp = false) {
array.forEach((el, index) => {
if (speed > 0) {
el.style.left = (parseInt(el.style.left) - Math.max(speed, 1)) + 'px';
}
if (parseInt(el.style.left) < -el.offsetWidth) {
removeElement(el, array, index, scoreValue);
} else if (checkCollision(player, el)) {
handleCollision(el, array, index, scoreValue, isPowerUp);
}
});
}
function removeElement(el, array, index, scoreValue) {
el.remove();
array.splice(index, 1);
if (scoreValue) {
score += scoreValue;
}
updateDisplay();
}
function handleCollision(el, array, index, scoreValue, isPowerUp = false) {
if (isPowerUp) {
applyPowerUp(el);
} else if (el.classList.contains('special-zone')) {
score *= 2;
} else if (el.classList.contains('end-door')) {
triggerEndOfLevel();
} else {
reduceLives();
}
removeElement(el, array, index, scoreValue);
}
function moveBullets() {
[{ bullets: bullets, attackable: enemies }, { bullets: enemyBullets, attackable: [player] }]
.forEach(group => {
group.bullets.forEach((bulletObj, index) => {
const bullet = bulletObj.element;
bullet.style.left = (parseInt(bullet.style.left) + (bulletObj.direction * 5)) + 'px';
if (parseInt(bullet.style.left) > gameContainer.offsetWidth || parseInt(bullet.style.left) < 0) {
bullet.remove();
group.bullets.splice(index, 1);
}
group.attackable.forEach((entity, entityIndex) => {
if (checkCollision(bullet, entity)) {
bullet.remove();
group.bullets.splice(index, 1);
if (entity !== player) {
entity.remove();
enemies.splice(entityIndex, 1);
score += 10;
} else {
reduceLives();
}
updateDisplay();
}
});
});
});
}
function applyPowerUp(el) {
if (currentPowerUp) return;
const type = el.getAttribute('data-type');
currentPowerUp = type;
let duration;
switch (type) {
case 'ice':
player.style.backgroundColor = 'blue';
duration = 30;
break;
case 'fire':
player.style.backgroundColor = 'red';
duration = 25;
break;
case 'levitate':
player.style.backgroundColor = 'green';
duration = 20;
break;
case 'hypno':
player.style.backgroundColor = 'pink';
duration = 25;
break;
case 'boss':
player.style.background = 'linear-gradient(90deg, red, orange, yellow, green, blue, indigo, violet)';
player.style.animation = 'rainbow 3s infinite linear';
duration = 25;
break;
default:
player.style.backgroundColor = '#4CAF50';
duration = 0;
}
powerUpTimer = duration;
const powerUpInterval = setInterval(() => {
powerUpTimer--;
updateDisplay();
if (powerUpTimer <= 0) {
clearInterval(powerUpInterval);
resetPowerUp();
}
}, 1000);
}
function resetPowerUp() {
currentPowerUp = null;
player.style.backgroundColor = '#4CAF50';
player.style.background = '';
player.style.animation = '';
}
function usePowerUp() {
if (!currentPowerUp) return;
switch (currentPowerUp) {
case 'ice':
shootIce();
break;
case 'fire':
shootFire();
break;
case 'levitate':
useLevitate();
break;
case 'hypno':
shootHypno();
break;
case 'boss':
performBossPowers();
break;
default:
break;
}
}
function shootIce() {
obstacles.forEach(obstacle => {
if (Math.abs(parseInt(obstacle.style.left) - playerPositionX) < 80) {
obstacle.classList.add('frozen');
obstacle.style.backgroundColor = '#00f';
}
});
}
function shootFire() {
obstacles.forEach(obstacle => {
if (Math.abs(parseInt(obstacle.style.left) - playerPositionX) < 80) {
obstacle.classList.add('burnt');
obstacle.style.backgroundColor = '#f55';
}
});
}
let levitatedEnemy = null;
function useLevitate() {
if (levitatedEnemy) {
levitatedEnemy.style.left = parseInt(levitatedEnemy.style.left) + 100 + 'px';
levitatedEnemy = null;
} else {
const nearestEnemy = enemies.reduce((prev, curr) => {
const prevDistance = Math.hypot(playerPositionX - parseInt(prev.style.left), playerPositionY - parseInt(prev.style.bottom));
const currDistance = Math.hypot(playerPositionX - parseInt(curr.style.left), playerPositionY - parseInt(curr.style.bottom));
return (currDistance < prevDistance ? curr : prev);
}, enemies[0]);
levitatedEnemy = nearestEnemy;
if (nearestEnemy) {
nearestEnemy.style.left = playerPositionX + 40 + 'px';
}
}
}
function shootHypno() {
enemies.forEach(enemy => {
if (Math.abs(parseInt(enemy.style.left) - playerPositionX) < 80) {
enemy.classList.add('hypnotized');
enemy.style.left = parseInt(enemy.style.left) - 100 + 'px';
}
});
}
function performBossPowers() {
const laser = document.createElement('div');
laser.classList.add('bullet');
laser.style.left = playerPositionX + 'px';
laser.style.bottom = (playerPositionY + player.offsetHeight / 2) + 'px';
laser.style.width = '5px';
laser.style.height = gameContainer.offsetHeight + 'px';
laser.style.backgroundColor = '#f00';
obstacles.forEach(obstacle => obstacle.remove());
obstacles = [];
gameContainer.appendChild(laser);
}
function reduceLives() {
lives--;
updateDisplay();
if (lives <= 0) endGame();
}
function checkCollisionWithClass(entity, className) {
return [...document.querySelectorAll(`.${className}`)].some(el => checkCollision(entity, el));
}
function checkCollision(rect1, rect2) {
const rect1Bounds = rect1.getBoundingClientRect();
const rect2Bounds = rect2.getBoundingClientRect();
return !(rect1Bounds.top > rect2Bounds.bottom || rect1Bounds.bottom < rect2Bounds.top ||
rect1Bounds.left > rect2Bounds.right || rect1Bounds.right < rect2Bounds.left);
}
function enemiesFireBullets() {
enemies.forEach(enemy => {
if (Math.random() < 0.010) {
const bullet = document.createElement('div');
bullet.classList.add('enemy-bullet');
bullet.style.left = parseInt(enemy.style.left) + 'px';
bullet.style.bottom = parseInt(enemy.style.bottom) + enemy.offsetHeight / 2 + 'px';
enemyBullets.push({ element: bullet, direction: -1 });
gameContainer.appendChild(bullet);
}
});
}
function togglePause() {
if (isPaused) {
splashScreen.style.display = 'none';
gameInterval = setInterval(gameLoop, 1000 / 60);
isPaused = false;
} else {
clearInterval(gameInterval);
splashScreen.style.display = 'flex';
isPaused = true;
}
}
function clearAllElements() {
[
obstacles, enemies, bullets.map(b => b.element), enemyBullets.map(e => e.element), powerUps, ladders,
caves, waters, collectibles, specialZones, platforms, levelDoors
].forEach(arr => clearSpecificElements(arr));
}
function clearSpecificElements(array) {
array.forEach(el => el.remove());
array.length = 0;
}
function interact() {
levelDoors.forEach((door) => {
if (checkCollision(player, door)) {
secretBossActive = true;
triggerSecretBoss();
}
});
}
function incrementLevel() {
level++;
if (level > maxLevels) {
endGame(true);
} else {
updateDisplay();
levelIntro.style.display = 'flex';
levelText.textContent = `Level ${level}: New Challenges Await!`;
switch (level) {
case 2:
gameContainer.style.transition = 'background-color 2s';
gameContainer.style.backgroundColor = '#004466';
break;
case 3:
gameContainer.style.backgroundColor = '#224422';
break;
case 4:
gameContainer.style.backgroundColor = '#555555';
break;
case 5:
gameContainer.style.backgroundColor = '#aaaaff';
break;
}
}
}
function showCutscene() {
splashScreen.style.display = 'flex';
setTimeout(() => continueGame(), 3000);
}
function continueGame() {
splashScreen.style.display = 'none';
incrementLevel();
gameInterval = setInterval(gameLoop, 1000 / 60);
}
function gameLoop() {
if (!isPaused) {
updatePlayerPosition();
spawnEntities();
moveElements();
createDayNightCycle();
checkScoreForLevelTransition();
}
}
function endGame(isWin = false) {
clearInterval(gameInterval);
gameOverScreen.style.display = 'flex';
gameOverScreen.innerHTML = `<p>${isWin ? 'Congratulations! You won!' : 'Game Over!'}</p>
<button onclick="startGame()">Restart</button>`;
}
function triggerEndOfLevel() {
clearInterval(gameInterval);
splashScreen.style.display = 'flex';
setTimeout(() => {
splashScreen.style.display = 'none';
incrementLevel();
startLevel();
}, 3000);
}
function triggerSecretBoss() {
if (!secretBossActive) return;
bossFightScreen.style.display = 'flex';
setTimeout(() => {
bossFightScreen.style.display = 'none';
clearAllElements();
const boss = document.createElement('div');
boss.classList.add('boss');
boss.style.left = '300px';
boss.style.bottom = '100px';
gameContainer.appendChild(boss);
bosses.push(boss);
let bossKillCount = 0;
const bossInterval = setInterval(() => {
if (checkCollision(player, boss)) {
reduceLives();
}
const enemiesToShoot = 10;
if (enemiesToShoot > bossKillCount) {
spawnEnemy();
bossKillCount++;
}
}, 1000 / 60);
}, 2000);
}
function checkScoreForLevelTransition() {
if (score >= level * 1000) {
triggerEndOfLevel();
}
}
</script>
make it so the powerups actually shoot if u press z
alright make it so when u touch a Power-Up ok so there will be a blue one called ice. and once touched it turns ur character blue for 30 seconds and when u press z it shoots ice and freezes blocks not enemies and u can jump on them to break them. and kill them.
next powerups fire once touched u turn red and when u press z u shoot fire. and u can shoot blocks and burn them . power last for 25 seconds.
next powerups levitate. the block is green once touched u turn green when u press z the nearest enemy gets dragged to u. (make sure it deals no damage) and u can keep it hold untill u press z again and it shoots it back and it destroys 2 blocks. powerups last for 20 secounds
next powerups is hypno. its a pink block and once touch u turn pink. and when u press z it shoots hypno shoot. and once touched enemy it goes the obisite direction and the nemy turns pink and goes to the blocks and when touch it destroys only 3 and then disappears.
every 1000 score goes to next level and pauses.
also make it so that in levels there will be secret places like a door once u click e it might lead to boss foight hall or it will just lead to a secret level for a secretobstacle course.
level 1: normal - this is the starter
level 2: underwater. this turns the place under water and u can actually swim up and down. but particles are spawned randomly around so u have to dodge it.
level 3: jungle. his turns the place green and enemy's can fall from the sky.
level 4: factory - this will be the hardest level summoning enemy's from the sky slamming down.
level 5: just jump - this will be where its not a whole platform. u have to jump on other platforms and not fall out the clouds int he gap and if u do it will say u fell down restart.
boss fight. there's a rare chance 25% that when ur playing a blue door will spawn once u press e next to it. the screen pauses and freezes saying going to boss hallway. once ur there u have to follow the path. and go to the other door which goes to the boss. room. the boss will have a diauluge like ahh so you came to me. destroying my cubes? your next! and he shoots enemy's and it goes after u untill u shoot at it. also the boss has a shield around him and once all the 10 enemy's are killed his shield turns off for 6 secoudns and the text says attack him now. shoot at him. *also powerup can spawn from the sky to the floor with a good chance) and once the 6 seconds r up the shield comes on and it loops untill the boss gets shot 50 times. and once he dies. he says nooo. and it drops a secret powerup called boss powerup. so how this powerup works is it saves so when u start up the game it saves and what this powerup does it will be a rainbow make it have rainbow animation and once touched u turn rainbow and a splash screen text will appear saying GO CRAZY WITH A RAINBOW CHANGING TEXT and it disappears 3 seconds so what this powerup does is the first move z is a laser shoots a red laser that clears all the blocks. next one is c when c is clicked it summons lightning in a random place where u at and it knocks block out the frame. and move e once clicked it summons rainbow enemies that charges after the block destroying in instant and disappears 2 seconds later. this power up last for 25 seconds.
note once u have apwoerup another one will not spawn till 50 seconds past.
and if u died to the boss. it will say u died to the boss press restart or go back to the level u were on.
make the full script with everything now