Compare commits

..

2 commits

Author SHA1 Message Date
61fba34857 Merge branch 'main' into main 2025-01-04 22:32:07 +01:00
7a51b5a94a fps fix 2025-01-04 22:16:28 +01:00

View file

@ -2,9 +2,13 @@
// 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,
@ -346,31 +350,37 @@ function restartGame() {
// Main game loop // Main game loop
function gameLoop() { function gameLoop() {
ctx.clearRect(0, 0, canvas.width, canvas.height); const currentTime = performance.now();
const elapsedTime = currentTime - lastFrameTime;
if (!isGameOver) { if (elapsedTime >= targetFrameTime) {
updatePlayer(); lastFrameTime = currentTime - (elapsedTime % targetFrameTime);
updateBullets(); ctx.clearRect(0, 0, canvas.width, canvas.height);
updateAsteroids();
updateItems();
checkCollisions();
}
drawPlayer(); if (!isGameOver) {
drawBullets(); updatePlayer();
drawAsteroids(); updateBullets();
drawItems(); updateAsteroids();
drawScore(); updateItems();
drawAmmo(); checkCollisions();
drawGameOver(); }
if (!isGameOver) { drawPlayer();
if (Math.random() < 0.01) createAsteroid(); // 1% chance every frame to spawn an asteroid drawBullets();
if (Math.random() < 0.005) createItem(); // 0.5% chance to spawn an item drawAsteroids();
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
gameLoop(); requestAnimationFrame(gameLoop);