Responsive

This commit is contained in:
sageTheDM 2024-12-17 14:42:17 +01:00
parent 418c2a30e8
commit 3051b3f3d9
7 changed files with 204 additions and 25 deletions

View file

@ -1,3 +1,4 @@
"use strict";
// Canvas setup
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
@ -45,7 +46,36 @@ let rainbowSphereCooldown = 0;
// Sphere types
const sphereTypes = ['blue', 'yellow', 'green', 'rainbow'];
// Controls
/// Control for left button press and release
function btnMoveLeft(isPressed) {
if (isPressed) {
player.dx = -player.speed; // Start moving left
} else {
player.dx = 0; // Stop moving when button is released
}
}
// Control for shoot button click (simulates spacebar press)
function btnShoot() {
if (canShoot && !isGameOver) {
shootBullet();
canShoot = false; // Prevent shooting until the button is "released"
}
}
// Control for right button press and release
function btnMoveRight(isPressed) {
if (isPressed) {
player.dx = player.speed; // Start moving right
} else {
player.dx = 0; // Stop moving when button is released
}
}
document.getElementById('shootBtn').addEventListener('mouseup', () => {
canShoot = true; // Allow shooting again when button is released
});
window.addEventListener('keydown', (e) => {
if (e.key === 'ArrowLeft' || e.key === 'a') player.dx = -player.speed;
if (e.key === 'ArrowRight' || e.key === 'd') player.dx = player.speed;
@ -79,6 +109,8 @@ function shootBullet() {
lastBulletTime = now;
ammo--; // Decrease ammo
totalBulletsFired++; // Increment total bullets fired
if (rapidFireActive) {
// If rapid fire is active, fire bullets continuously with a short delay
for (let i = 0; i < 3; i++) {
@ -193,6 +225,7 @@ function updateItems() {
}
function applyItemEffect(type) {
let points = Math.floor(Math.random() * 5) + 1; // Random points between 1 and 5
if (type === 'blue') {
rapidFireActive = true;
setTimeout(() => rapidFireActive = false, 15000); // 15 seconds of rapid fire
@ -209,6 +242,7 @@ function applyItemEffect(type) {
shotgunActive = false;
}, 15000); // 15 seconds with all effects
}
score += points; // Add points when an item is collected
}
// Collision detection
@ -223,7 +257,9 @@ function checkCollisions() {
) {
bullets.splice(bIndex, 1);
asteroids.splice(aIndex, 1);
score++;
score += Math.floor(Math.random() * 5) + 1; // Add points when an asteroid is destroyed
// Visual feedback for destroyed asteroid
createExplosion(asteroid.x, asteroid.y);
}
});
});
@ -240,6 +276,15 @@ function checkCollisions() {
});
}
// Explosion effect
function createExplosion(x, y) {
ctx.fillStyle = 'yellow';
ctx.beginPath();
ctx.arc(x, y, 20, 0, Math.PI * 2);
ctx.fill();
setTimeout(() => ctx.clearRect(x - 20, y - 20, 40, 40), 200); // Clear explosion after 200ms
}
// Draw elements
function drawPlayer() {
ctx.fillStyle = player.color;
@ -320,23 +365,12 @@ function gameLoop() {
drawGameOver();
if (!isGameOver) {
if (Math.random() < 0.02) {
createAsteroid();
}
if (Math.random() < 0.02) {
createItem();
}
}
// Difficulty increase over time
if (score > 0 && score % 50 === 0) {
asteroidSpawnInterval -= difficultyIncreaseRate; // Increase spawn rate (faster asteroids)
asteroidSpeedMultiplier += difficultyIncreaseRate; // Increase speed multiplier
if (Math.random() < 0.01) createAsteroid(); // 1% chance every frame to spawn an asteroid
if (Math.random() < 0.005) createItem(); // 0.5% chance to spawn an item
}
requestAnimationFrame(gameLoop);
}
// Start the game
// Start game loop
gameLoop();