Skip to content

Commit

Permalink
Merge pull request #2155 from Jivanjamadar/feature
Browse files Browse the repository at this point in the history
Added folder of SpaceBubbleDodger Game on main repo.
  • Loading branch information
iamrahulmahato authored Nov 6, 2024
2 parents 1db7d0d + 19c99ca commit 51e5f85
Show file tree
Hide file tree
Showing 3 changed files with 249 additions and 0 deletions.
22 changes: 22 additions & 0 deletions SpaceBubbleDodger_Game/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Space Bubble Dodger</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="game-container">
<canvas id="gameCanvas"></canvas>
<div id="score">Score: <span id="scoreValue">0</span></div>
<div id="game-over" class="hidden">
<h2>Game Over!</h2>
<p>Your final score: <span id="finalScore"></span></p>
<button id="restartButton">Play again</button>
</div>
</div>
<div id="level-up-popup" class="popup hidden"></div>
<script src="script.js"></script>
</body>
</html>
164 changes: 164 additions & 0 deletions SpaceBubbleDodger_Game/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
canvas.width = 800;
canvas.height = 600;

let score = 0;
let bubbles = [];
let gameOver = false;
let level = 1;
let spaceship = {
x: canvas.width / 2,
y: canvas.height - 50,
width: 60,
height: 60,
speed: 10 // Increased base speed
};

let bubbleBaseSpeed = 2;
let bubbleSpeedIncrease = 0.5; // Bubble speed increase per level
let spaceshipSpeedIncrease = 2; // Speed increase per level for spaceship

function createBubble() {
const size = Math.random() * (30 - 10) + 10;
const x = Math.random() * (canvas.width - size);
bubbles.push({ x, y: 0, size, speed: bubbleBaseSpeed + bubbleSpeedIncrease * (level - 1) });
}

function drawSpaceship() {
ctx.save();
ctx.translate(spaceship.x, spaceship.y);

// Draw the main body
ctx.fillStyle = '#00ffff';
ctx.beginPath();
ctx.moveTo(0, -spaceship.height / 2);
ctx.lineTo(-spaceship.width / 2, spaceship.height / 2);
ctx.lineTo(spaceship.width / 2, spaceship.height / 2);
ctx.closePath();
ctx.fill();

// Draw the cockpit
ctx.fillStyle = '#ffffff';
ctx.beginPath();
ctx.arc(0, 0, spaceship.width / 6, 0, Math.PI * 2);
ctx.fill();

// Draw the wings
ctx.fillStyle = '#ff00ff';
ctx.beginPath();
ctx.moveTo(-spaceship.width / 2, spaceship.height / 4);
ctx.lineTo(-spaceship.width, spaceship.height / 2);
ctx.lineTo(-spaceship.width / 2, spaceship.height / 2);
ctx.closePath();
ctx.fill();

ctx.beginPath();
ctx.moveTo(spaceship.width / 2, spaceship.height / 4);
ctx.lineTo(spaceship.width, spaceship.height / 2);
ctx.lineTo(spaceship.width / 2, spaceship.height / 2);
ctx.closePath();
ctx.fill();

ctx.restore();
}

function drawBubbles() {
bubbles.forEach(bubble => {
ctx.fillStyle = `rgba(${Math.random() * 255}, ${Math.random() * 255}, ${Math.random() * 255}, 0.7)`;
ctx.beginPath();
ctx.arc(bubble.x, bubble.y, bubble.size, 0, Math.PI * 2);
ctx.fill();
});
}

function update() {
if (gameOver) return;

bubbles.forEach((bubble, index) => {
bubble.y += bubble.speed;

// Collision detection
if (
bubble.y + bubble.size > spaceship.y - spaceship.height / 2 &&
bubble.y - bubble.size < spaceship.y + spaceship.height / 2 &&
bubble.x + bubble.size > spaceship.x - spaceship.width / 2 &&
bubble.x - bubble.size < spaceship.x + spaceship.width / 2
) {
gameOver = true;
document.getElementById('game-over').classList.remove('hidden');
document.getElementById('finalScore').textContent = score;
}

if (bubble.y - bubble.size > canvas.height) {
bubbles.splice(index, 1);
score++;
document.getElementById('scoreValue').innerText = score;

// Level up every 5 points
if (score % 5 === 0) {
level++;
showLevelUpPopup();
spaceship.speed += spaceshipSpeedIncrease; // Increased speed increment
bubbleSpeedIncrease += 0.5; // Increase bubble speed increment
createBubble();
}
}
});

if (bubbles.length < 5) {
createBubble();
}
}

function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawSpaceship();
drawBubbles();
}

function gameLoop() {
update();
draw();
if (!gameOver) {
requestAnimationFrame(gameLoop);
}
}

document.addEventListener('keydown', (e) => {
if (gameOver) return;
const moveDistance = spaceship.speed;
if (e.code === 'ArrowLeft' && spaceship.x > spaceship.width / 2) {
spaceship.x = Math.max(spaceship.x - moveDistance, spaceship.width / 2);
} else if (e.code === 'ArrowRight' && spaceship.x < canvas.width - spaceship.width / 2) {
spaceship.x = Math.min(spaceship.x + moveDistance, canvas.width - spaceship.width / 2);
}
});

document.getElementById('restartButton').addEventListener('click', () => {
score = 0;
bubbles = [];
gameOver = false;
level = 1;
spaceship.speed = 10; // Reset to new base speed
document.getElementById('scoreValue').innerText = score;
document.getElementById('game-over').classList.add('hidden');
spaceship.x = canvas.width / 2;
gameLoop();
});

function showLevelUpPopup() {
const popup = document.getElementById('level-up-popup');
popup.textContent = `Level Up! Now at Level ${level}`;
popup.classList.remove('hidden');
popup.style.opacity = 1;
setTimeout(() => {
popup.style.opacity = 0;
setTimeout(() => {
popup.classList.add('hidden');
}, 500);
}, 2000);
}

// Start the game loop
gameLoop();
63 changes: 63 additions & 0 deletions SpaceBubbleDodger_Game/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
body {
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #000;
font-family: Arial, sans-serif;
}

#game-container {
position: relative;
}

#gameCanvas {
border: 2px solid #fff;
}

#score {
position: absolute;
top: 10px;
left: 10px;
color: #fff;
font-size: 20px;
}

#game-over {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: rgba(0, 0, 0, 0.8);
color: #fff;
padding: 20px;
text-align: center;
border-radius: 10px;
}

#restartButton {
margin-top: 10px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}

.hidden {
display: none;
}

.popup {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: rgba(0, 0, 0, 0.7);
color: white;
padding: 20px;
border-radius: 10px;
text-align: center;
opacity: 0;
transition: opacity 0.5s;
}

0 comments on commit 51e5f85

Please sign in to comment.