Compare commits

..

No commits in common. "61fba348579643ec67536d5f2f75d401587b2bb3" and "2fab0a38ea9f281499e856e6cb5fb7c7789b6e6c" have entirely different histories.

View file

@ -2,13 +2,9 @@
// Canvas setup // Canvas setup
const canvas = document.getElementById('gameCanvas'); const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d'); const ctx = canvas.getContext('2d');
const targetFPS = 60;
const targetFrameTime = 1000 / targetFPS;
canvas.width = window.innerWidth; canvas.width = window.innerWidth;
canvas.height = window.innerHeight; canvas.height = window.innerHeight;
let lastFrameTime = performance.now();
// Game elements // Game elements
const player = { const player = {
x: canvas.width / 2, x: canvas.width / 2,
@ -350,37 +346,31 @@ function restartGame() {
// Main game loop // Main game loop
function gameLoop() { function gameLoop() {
const currentTime = performance.now(); ctx.clearRect(0, 0, canvas.width, canvas.height);
const elapsedTime = currentTime - lastFrameTime;
if (elapsedTime >= targetFrameTime) { if (!isGameOver) {
lastFrameTime = currentTime - (elapsedTime % targetFrameTime); updatePlayer();
ctx.clearRect(0, 0, canvas.width, canvas.height); updateBullets();
updateAsteroids();
updateItems();
checkCollisions();
}
if (!isGameOver) { drawPlayer();
updatePlayer(); drawBullets();
updateBullets(); drawAsteroids();
updateAsteroids(); drawItems();
updateItems(); drawScore();
checkCollisions(); drawAmmo();
} drawGameOver();
drawPlayer(); if (!isGameOver) {
drawBullets(); if (Math.random() < 0.01) createAsteroid(); // 1% chance every frame to spawn an asteroid
drawAsteroids(); if (Math.random() < 0.005) createItem(); // 0.5% chance to spawn an item
drawItems();
drawScore();
drawAmmo();
drawGameOver();
if (!isGameOver) {
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); requestAnimationFrame(gameLoop);
} }
// Start game loop // Start game loop
requestAnimationFrame(gameLoop); gameLoop();