This commit is contained in:
Patrick 2025-01-04 22:16:28 +01:00
parent ca6b00a6d4
commit 7a51b5a94a

View file

@ -2,9 +2,13 @@
// Canvas setup
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const targetFPS = 60;
const targetFrameTime = 1000 / targetFPS;
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let lastFrameTime = performance.now();
// Game elements
const player = {
x: canvas.width / 2,
@ -346,6 +350,11 @@ function restartGame() {
// Main game loop
function gameLoop() {
const currentTime = performance.now();
const elapsedTime = currentTime - lastFrameTime;
if (elapsedTime >= targetFrameTime) {
lastFrameTime = currentTime - (elapsedTime % targetFrameTime);
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (!isGameOver) {
@ -368,9 +377,10 @@ function gameLoop() {
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 game loop
gameLoop();
requestAnimationFrame(gameLoop);