To enhance your game further, I'll introduce four additional power-ups with unique abilities and modify the boss door interaction to transport the player to a boss hallway for a battle. Here’s how you can achieve these updates:
Four New Power-Ups
- Invisibility: Temporarily makes the player invisible and invulnerable to enemies.
- Regeneration: Gradually restores player's life over time.
- Magnet: Attracts nearby collectibles like coins or power-ups.
- Time Slow: Slows down time, affecting enemies and obstacles.
Updated Interaction for Boss Door
When the player presses 'E' near the boss door, it starts a transition to a boss hallway for the battle.
Below is the revised code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Advanced Adventure Game</title>
<style>
body, html {
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #222;
color: #fff;
font-family: Arial, sans-serif;
overflow: hidden;
}
#game-container {
position: relative;
width: 800px;
height: 400px;
background-color: #333;
overflow: hidden;
border: 2px solid #444;
}
#game-world {
position: absolute;
width: 1600px;
height: 100%;
transition: transform 0.1s linear;
}
#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); }
}
.character, .boss-door, .power-up, .enemy, .obstacle, .bullet, .trail, .burst, .boss {
position: absolute;
}
.character {
width: 40px;
height: 40px;
background-color: #FF8800;
border-radius: 50%;
}
.king {
width: 50px;
height: 50px;
background-color: #AA00FF;
border-radius: 50%;
}
.bullet {
width: 10px;
height: 5px;
background-color: #FFD700;
}
.trail {
width: 6px;
height: 3px;
background-color: rgba(255, 215, 0, 0.5);
}
.burst {
width: 20px;
height: 20px;
border-radius: 50%;
background-color: rgba(255, 69, 0, 0.7);
animation: burst 0.5s ease-out;
}
@keyframes burst {
from { transform: scale(0.5); opacity: 1; }
to { transform: scale(1.5); opacity: 0; }
}
.enemy {
width: 25px;
height: 25px;
background-color: #FF5733;
}
.obstacle {
width: 30px;
height: 30px;
background-color: #f44336;
}
.power-up {
width: 20px;
height: 20px;
}
.boss-door {
width: 30px;
height: 60px;
background-color: #FFFF00;
}
.boss {
width: 60px;
height: 60px;
background-color: #FF4500;
}
#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, #boss-hallway, #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, #boss-hallway button {
margin-top: 10px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
background-color: #555;
color: #fff;
}
#cutscene, #level-intro, #boss-fight, #boss-hallway, #game-over {
display: none;
}
#intro {
position: absolute;
width: 100%;
height: 100%;
display: flex;
justify-content: space-around;
align-items: center;
background-color: rgba(0, 0, 0, 0.8);
color: #fff;
flex-direction: row;
font-size: 18px;
z-index: 11;
}
</style>
</head>
<body>
<div id="game-container">
<div id="game-world">
<div id="player"></div>
</div>
<div id="score">Score: 0</div>
<div id="lives">Lives: 3</div>
<div id="level">Level: 1</div>
<div id="timer">Timer: 0</div>
<div id="instructions">
<p>Press Arrow Up to Jump</p>
<p>Use Arrow Left/Right to Move</p>
<p>Press Space to Shoot</p>
<p>Press Z for Power-Up</p>
<p>Press E to interact</p>
<button onclick="startGame()">Start Game</button>
<button onclick="togglePause()">Pause/Resume</button>
</div>
<div id="cutscene">
<p>Get ready for the next level!</p>
<button onclick="continueGame()">Continue</button>
</div>
<div id="level-intro">
<p id="level-text">Level 1: Get Ready for Adventure!</p>
<button onclick="startLevel()">Start Level</button>
</div>
<div id="boss-fight">
<p>Defeat the Boss!</p>
</div>
<div id="game-over">
<p>Game Over!</p>
<button onclick="startGame()">Restart</button>
</div>
<div id="intro">
<div class="character"></div>
<div id="dialogue">
<p>Player: Hey friend! What a nice day for an adventure!</p>
<button onclick="nextDialogue()">Next</button>
</div>
<div class="character"></div>
<div class="king"></div>
</div>
<div id="boss-hallway" style="display: none;">
<p>You have entered the boss hallway! Prepare yourself!</p>
<button onclick="startBossFight()">Proceed</button>
</div>
</div>
<script>
const gameContainer = document.getElementById('game-container');
const gameWorld = document.getElementById('game-world');
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 bossFightScreen = document.getElementById('boss-fight');
const gameOverScreen = document.getElementById('game-over');
const intro = document.getElementById('intro');
const dialoguePara = document.querySelector('#dialogue p');
const bossHallway = document.getElementById('boss-hallway');
let playerPositionX = 50;
let playerPositionY = 0;
let playerVelocityX = 0;
let playerVelocityY = 0;
const playerMaxSpeed = 5;
let isJumping = false;
let isPaused = false;
let canShoot = true;
let gameInterval;
let obstacles = [];
let enemies = [];
let bullets = [];
let powerUps = [];
let platforms = [];
let bossDoors = [];
let score = 0;
let lives = 3;
let level = 1;
const gravity = 0.5;
const jumpStrength = 12;
const maxLevels = 5;
let movingLeft = false, movingRight = false;
let currentPowerUp = null;
let powerUpTimer = 0;
let gameScrollPosition = 0;
const scrollSpeed = 2;
const bossThreshold = 1000;
const dialogues = [
"Player: Hey friend! What a nice day for an adventure!",
"Friend: Indeed! Maybe we should explore the caves.",
"Player: What's that over there? Is it the King?",
"King: Haha, I've found you! You're coming with me!",
"Friend: Player, help me!"
];
let dialogueIndex = 0;
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) {
shootBullet();
canShoot = false;
setTimeout(() => canShoot = true, 300);
}
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 nextDialogue() {
if (dialogueIndex < dialogues.length - 1) {
dialogueIndex++;
dialoguePara.textContent = dialogues[dialogueIndex];
} else {
intro.style.display = 'none';
startGame(); // Start game after dialogues
}
}
function jump() {
if (!isJumping) {
playerVelocityY = jumpStrength;
isJumping = true;
}
}
function shootBullet() {
const bullet = document.createElement('div');
bullet.classList.add('bullet');
const bulletDirection = movingRight ? 1 : -1;
bullets.push({ element: bullet, direction: bulletDirection });
bullet.style.left = (playerPositionX + player.offsetWidth / 2 - 5) + 'px';
bullet.style.bottom = (playerPositionY + player.offsetHeight / 2) + 'px';
gameWorld.appendChild(bullet);
}
function createBurst(x, y) {
const burst = document.createElement('div');
burst.classList.add('burst');
burst.style.left = x + 'px';
burst.style.bottom = y + 'px';
gameWorld.appendChild(burst);
setTimeout(() => burst.remove(), 500);
}
function moveCamera() {
gameScrollPosition = Math.max(0, Math.min(gameWorld.offsetWidth - gameContainer.offsetWidth, playerPositionX - 400));
gameWorld.style.transform = `translateX(${-gameScrollPosition}px)`;
}
function moveBullets() {
bullets.forEach((bulletObj, index) => {
const bullet = bulletObj.element;
bullet.style.left = (parseInt(bullet.style.left) + (bulletObj.direction * 5)) + 'px';
if (parseInt(bullet.style.left) > gameScrollPosition + gameContainer.offsetWidth || parseInt(bullet.style.left) < 0) {
bullet.remove();
bullets.splice(index, 1);
} else {
enemies.forEach((enemy, enemyIndex) => {
if (checkCollision(bullet, enemy)) {
createBurst(parseInt(bullet.style.left), parseInt(bullet.style.bottom));
bullet.remove();
bullets.splice(index, 1);
enemy.remove();
enemies.splice(enemyIndex, 1);
score += 10;
updateDisplay();
}
});
}
});
}
function startGame() {
gameOverScreen.style.display = 'none';
instructions.style.display = 'none';
levelIntro.style.display = 'flex';
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;
gameScrollPosition = 0;
clearAllElements();
updateDisplay();
switchLevelBackground(); // Ensure the correct background at game start
}
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(gameWorld.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';
moveCamera();
}
function spawnEntities() {
if (!isPaused) {
if (Math.random() < 0.003) spawnObstacle();
if (Math.random() < 0.01) spawnEnemy();
if (Math.random() < 0.005 && !currentPowerUp) spawnPowerUp();
if (Math.random() < 0.002) spawnPlatform();
if (bossDoors.length === 0 && score >= bossThreshold && enemies.length === 0) spawnBossDoor();
}
}
function moveElements() {
moveArray(obstacles, 2, 15);
moveArray(enemies, 1, 30);
moveArray(powerUps, 2, 0, true);
moveArray(platforms, 1, 0);
moveBullets();
}
function spawnObstacle() {
const obstacle = createElement('obstacle', obstacles, gameScrollPosition + gameContainer.offsetWidth);
obstacle.style.bottom = '0px';
}
function spawnEnemy() {
const enemy = createElement('enemy', enemies, gameScrollPosition + gameContainer.offsetWidth);
enemy.style.bottom = '0px';
}
function spawnPowerUp() {
const powerUpTypes = [
'ice', 'fire', 'levitate', 'hypno', 'shield', 'speed',
'double-shot', 'ghost', 'invisibility', 'regeneration', 'magnet', 'time-slow'
];
const type = powerUpTypes[Math.floor(Math.random() * powerUpTypes.length)];
const powerUp = createElement('power-up', powerUps, gameScrollPosition + gameContainer.offsetWidth);
powerUp.setAttribute('data-type', type);
powerUp.style.backgroundColor = getPowerUpColor(type);
powerUp.style.bottom = Math.random() * 100 + 'px';
}
function getPowerUpColor(type) {
switch (type) {
case 'ice': return 'blue';
case 'fire': return 'red';
case 'levitate': return 'green';
case 'hypno': return 'pink';
case 'shield': return 'purple';
case 'speed': return 'orange';
case 'double-shot': return 'cyan';
case 'ghost': return 'grey';
case 'invisibility': return 'lightgrey';
case 'regeneration': return 'lightgreen';
case 'magnet': return 'gold';
case 'time-slow': return 'navy';
default: return 'gold';
}
}
function spawnPlatform() {
const platform = createElement('platform', platforms, gameScrollPosition + gameContainer.offsetWidth);
platform.style.bottom = Math.floor(Math.random() * 200 + 100) + 'px';
}
function spawnBossDoor() {
const bossDoor = createElement('boss-door', bossDoors, gameScrollPosition + gameContainer.offsetWidth + 200);
bossDoor.style.bottom = '0px';
}
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';
gameWorld.appendChild(element);
array.push(element);
return element;
}
function moveArray(array, speed, scoreValue, isPowerUp = false) {
array.forEach((el, index) => {
el.style.left = (parseInt(el.style.left) - speed) + 'px';
if (parseInt(el.style.left) < gameScrollPosition - 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('boss-door')) {
openBossHallway();
} else if (el.classList.contains('enemy') || el.classList.contains('obstacle')) {
reduceLives();
}
removeElement(el, array, index, scoreValue);
}
function applyPowerUp(el) {
if (currentPowerUp) return;
const type = el.getAttribute('data-type');
currentPowerUp = type;
let duration = 10;
switch (type) {
case 'ice':
player.style.backgroundColor = 'blue';
break;
case 'fire':
player.style.backgroundColor = 'red';
break;
case 'levitate':
player.style.backgroundColor = 'green';
break;
case 'hypno':
player.style.backgroundColor = 'pink';
break;
case 'shield':
activateShield();
break;
case 'speed':
player.style.backgroundColor = 'orange';
playerMaxSpeed *= 2;
break;
case 'double-shot':
player.style.backgroundColor = 'cyan';
break;
case 'ghost':
player.style.opacity = '0.5';
break;
case 'invisibility':
player.style.opacity = '0.2';
break;
case 'regeneration':
activateRegeneration();
break;
case 'magnet':
activateMagnet();
break;
case 'time-slow':
slowDownTime();
break;
default:
player.style.backgroundColor = '#4CAF50';
break;
}
powerUpTimer = duration;
const powerUpInterval = setInterval(() => {
powerUpTimer--;
updateDisplay();
if (powerUpTimer <= 0) {
clearInterval(powerUpInterval);
resetPowerUp();
}
}, 1000);
}
function resetPowerUp() {
currentPowerUp = null;
player.style.backgroundColor = '#4CAF50';
player.style.opacity = '1';
playerMaxSpeed = 5;
clearTimeout(timeSlowTimeout);
}
function usePowerUp() {
if (!currentPowerUp) return;
switch (currentPowerUp) {
case 'ice':
shootIce();
break;
case 'fire':
shootFire();
break;
case 'levitate':
useLevitate();
break;
case 'hypno':
shootHypno();
break;
case 'double-shot':
activateDoubleShot();
break;
// 'speed', 'ghost', 'invisibility', 'magnet', 'regeneration', 'time-slow' are passive effects
default:
break;
}
}
function shootIce() {
const iceBullet = document.createElement('div');
iceBullet.classList.add('bullet');
iceBullet.style.backgroundColor = 'blue';
iceBullet.style.left = playerPositionX + 'px';
iceBullet.style.bottom = (playerPositionY + player.offsetHeight / 2) + 'px';
gameWorld.appendChild(iceBullet);
const iceBulletObj = { element: iceBullet, direction: movingRight ? 1 : -1 };
animateBullet(iceBulletObj, 5, target => {
if (target.classList.contains('obstacle')) {
target.classList.add('frozen');
target.style.backgroundColor = '#00f';
}
});
}
function shootFire() {
const fireBullet = document.createElement('div');
fireBullet.classList.add('bullet');
fireBullet.style.backgroundColor = 'red';
fireBullet.style.left = playerPositionX + 'px';
fireBullet.style.bottom = (playerPositionY + player.offsetHeight / 2) + 'px';
gameWorld.appendChild(fireBullet);
const fireBulletObj = { element: fireBullet, direction: movingRight ? 1 : -1 };
animateBullet(fireBulletObj, 5, target => {
if (target.classList.contains('obstacle')) {
target.style.transition = 'opacity 0.5s';
target.style.opacity = '0';
setTimeout(() => target.remove(), 500);
}
});
}
function useLevitate() {
// Activate floating effect
player.style.transform = 'translateY(-20px)';
const levitateUp = setTimeout(() => {
player.style.transform = 'translateY(0)';
clearTimeout(levitateUp);
}, 1000);
}
function activateDoubleShot() {
const bullet1 = createBullet();
const bullet2 = createBullet();
bullet1.style.top = (parseInt(bullet1.style.bottom) - 5) + 'px';
bullet2.style.top = (parseInt(bullet2.style.bottom) + 5) + 'px';
gameWorld.appendChild(bullet1);
gameWorld.appendChild(bullet2);
const fireBullets = [
{ element: bullet1, direction: movingRight ? 1 : -1 },
{ element: bullet2, direction: movingRight ? 1 : -1 }
];
fireBullets.forEach(bulletObj => {
animateBullet(bulletObj, 5, target => {
if (target.classList.contains('obstacle')) {
target.style.transition = 'opacity 0.5s';
target.style.opacity = '0';
setTimeout(() => target.remove(), 500);
}
});
});
}
function createBullet() {
const bullet = document.createElement('div');
bullet.classList.add('bullet');
bullet.style.backgroundColor = '#FFD700';
bullet.style.left = playerPositionX + 'px';
bullet.style.bottom = (playerPositionY + player.offsetHeight / 2) + 'px';
return bullet;
}
function shootHypno() {
const hypnoBullet = createBullet();
hypnoBullet.style.backgroundColor = 'pink';
gameWorld.appendChild(hypnoBullet);
const hypnoBulletObj = { element: hypnoBullet, direction: movingRight ? 1 : -1 };
animateBullet(hypnoBulletObj, 5, target => {
if (target.classList.contains('enemy')) {
target.classList.add('hypnotized');
target.style.backgroundColor = 'pink';
hypnotizeEnemy(target);
}
});
}
function hypnotizeEnemy(enemy) {
let destroyCount = 0;
const moveInterval = setInterval(() => {
enemy.style.left = (parseInt(enemy.style.left) - 3) + 'px'; // Move backwards
obstacles.forEach((obstacle, index) => {
if (checkCollision(enemy, obstacle)) {
obstacle.remove();
obstacles.splice(index, 1);
destroyCount++;
if (destroyCount >= 3) {
enemy.remove();
clearInterval(moveInterval);
}
}
});
if (parseInt(enemy.style.left) < 0) {
enemy.remove();
clearInterval(moveInterval);
}
}, 1000 / 60);
}
function activateShield() {
player.style.border = '3px solid purple';
lives += 1; // Extra life with shield
}
function activateRegeneration() {
const regenerationInterval = setInterval(() => {
if (lives < 5) lives++;
updateDisplay();
if (!currentPowerUp || currentPowerUp !== 'regeneration') {
clearInterval(regenerationInterval);
}
}, 2000);
}
function activateMagnet() {
const magnetInterval = setInterval(() => {
powerUps.forEach(pu => {
const puX = parseInt(pu.style.left);
const puY = parseInt(pu.style.bottom);
const dx = playerPositionX - puX;
const dy = playerPositionY - puY;
if (Math.sqrt(dx * dx + dy * dy) < 100) {
pu.style.left = (puX + dx * 0.1) + 'px';
pu.style.bottom = (puY + dy * 0.1) + 'px';
}
});
if (!currentPowerUp || currentPowerUp !== 'magnet') {
clearInterval(magnetInterval);
}
}, 1000 / 60);
}
let timeSlowTimeout;
function slowDownTime() {
const originalTiming = 1000 / 60;
clearInterval(gameInterval);
gameInterval = setInterval(gameLoop, originalTiming * 2);
timeSlowTimeout = setTimeout(() => {
clearInterval(gameInterval);
gameInterval = setInterval(gameLoop, originalTiming);
}, 5000); // Slow down time for 5 seconds
}
function animateBullet(bulletObj, speed, impactCallback = () => { }) {
const bulletInterval = setInterval(() => {
const bullet = bulletObj.element;
bullet.style.left = (parseInt(bullet.style.left) + (bulletObj.direction * speed)) + 'px';
if (parseInt(bullet.style.left) > gameScrollPosition + gameContainer.offsetWidth || parseInt(bullet.style.left) < 0) {
bullet.remove();
clearInterval(bulletInterval);
} else {
[...obstacles, ...enemies].forEach(target => {
if (checkCollision(bullet, target)) {
impactCallback(target);
createBurst(parseInt(bullet.style.left), parseInt(bullet.style.bottom));
bullet.remove();
clearInterval(bulletInterval);
}
});
}
}, 1000 / 60);
}
function reduceLives() {
if (player.style.border) {
player.style.border = ''; // Remove shield
} else {
lives--;
if (lives <= 0) endGame(false);
}
updateDisplay();
}
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 togglePause() {
if (isPaused) {
gameInterval = setInterval(gameLoop, 1000 / 60);
isPaused = false;
} else {
clearInterval(gameInterval);
isPaused = true;
}
}
function clearAllElements() {
[obstacles, enemies, bullets.map(b => b.element), powerUps, platforms, bossDoors]
.forEach(arr => clearSpecificElements(arr));
}
function clearSpecificElements(array) {
array.forEach(el => el.remove());
array.length = 0;
}
function interact() {
bossDoors.forEach((door) => {
if (checkCollision(player, door)) {
openBossHallway();
}
});
}
function openBossHallway() {
bossHallway.style.display = 'flex';
clearAllElements(); // Clean up before transition
clearInterval(gameInterval);
}
function startBossFight() {
bossHallway.style.display = 'none';
triggerBossFight();
}
function incrementLevel() {
level++;
if (level > maxLevels) {
endGame(true);
} else {
updateDisplay();
resetGame();
levelIntro.style.display = 'flex';
switchLevelBackground();
levelText.textContent = `Level ${level}: New Challenges Await!`;
}
}
function switchLevelBackground() {
switch (level) {
case 2:
gameContainer.style.backgroundColor = '#1E90FF'; // Blue ocean
break;
case 3:
gameContainer.style.backgroundColor = '#228B22'; // Forest green
break;
case 4:
gameContainer.style.backgroundColor = '#E0FFFF'; // Cloudy sky
break;
case 5:
gameContainer.style.backgroundColor = '#B22222'; // Final level color
break;
default:
gameContainer.style.backgroundColor = '#333'; // Default
}
}
function continueGame() {
levelIntro.style.display = 'none';
gameInterval = setInterval(gameLoop, 1000 / 60);
}
function gameLoop() {
if (!isPaused) {
updatePlayerPosition();
spawnEntities();
moveElements();
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 triggerBossFight() {
bossFightScreen.style.display = 'flex';
setTimeout(() => {
bossFightScreen.style.display = 'none';
const boss = document.createElement('div');
boss.classList.add('boss');
boss.style.left = '300px';
boss.style.bottom = '100px';
gameWorld.appendChild(boss);
enemies.push(boss);
let bossHits = 0;
const bossInterval = setInterval(() => {
if (checkCollision(player, boss)) {
reduceLives();
}
if (bossHits >= 10) {
boss.remove();
clearInterval(bossInterval);
incrementLevel();
}
}, 1000);
function checkBossHit() {
bullets.forEach((bullet, bulletIndex) => {
if (checkCollision(bullet.element, boss)) {
bossHits++;
bullet.element.remove();
bullets.splice(bulletIndex, 1);
if (bossHits >= 10) {
boss.remove();
clearInterval(bossInterval);
incrementLevel();
}
}
});
}
setInterval(checkBossHit, 1000 / 60);
}, 2000);
}
function checkScoreForLevelTransition() {
if (score >= level * 1000) {
triggerEndOfLevel();
}
}
</script>
</body>
</html>
Key Changes
- New Power-Ups: Four additional power-ups with different mechanics like invisibility, regeneration, magnet, and time slow have been added. Adjustments have been made to ensure they activate and reset correctly.
- Boss Door & Hallway Transition: Modified the boss door logic to lead the player to a boss hallway scene when the 'E' key is pressed nearby. The boss fight can then be started from this new section.
These changes should enrich the gameplay and allow for deeper engagement by offering strategic advantages and a new boss transition feature. Make sure to test these mechanics and adjust the power-up effects and durations as needed to balance the game.