Changed around line 1
+ const canvas = document.getElementById('gameCanvas');
+ const ctx = canvas.getContext('2d');
+ const startButton = document.getElementById('startButton');
+ const pauseButton = document.getElementById('pauseButton');
+ const scoreDisplay = document.getElementById('scoreDisplay');
+
+ let ship, asteroids, bullets, score, gameOver, paused;
+
+ const SHIP_SIZE = 30;
+ const ASTEROID_SIZE = 50;
+ const BULLET_SPEED = 5;
+ const ASTEROID_SPEED = 2;
+
+ class Ship {
+ constructor() {
+ this.x = canvas.width / 2;
+ this.y = canvas.height / 2;
+ this.angle = 0;
+ this.speed = 0;
+ this.rotation = 0;
+ }
+
+ draw() {
+ ctx.save();
+ ctx.translate(this.x, this.y);
+ ctx.rotate(this.angle);
+ ctx.beginPath();
+ ctx.moveTo(SHIP_SIZE, 0);
+ ctx.lineTo(-SHIP_SIZE, -SHIP_SIZE / 2);
+ ctx.lineTo(-SHIP_SIZE, SHIP_SIZE / 2);
+ ctx.closePath();
+ ctx.strokeStyle = 'white';
+ ctx.stroke();
+ ctx.restore();
+ }
+
+ update() {
+ this.angle += this.rotation;
+ this.x += Math.cos(this.angle) * this.speed;
+ this.y += Math.sin(this.angle) * this.speed;
+
+ if (this.x < 0) this.x = canvas.width;
+ if (this.x > canvas.width) this.x = 0;
+ if (this.y < 0) this.y = canvas.height;
+ if (this.y > canvas.height) this.y = 0;
+ }
+ }
+
+ class Asteroid {
+ constructor() {
+ this.x = Math.random() < 0.5 ? 0 : canvas.width;
+ this.y = Math.random() * canvas.height;
+ this.angle = Math.atan2(ship.y - this.y, ship.x - this.x);
+ this.speed = ASTEROID_SPEED;
+ this.size = ASTEROID_SIZE;
+ }
+
+ draw() {
+ ctx.beginPath();
+ ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
+ ctx.strokeStyle = 'red';
+ ctx.stroke();
+ }
+
+ update() {
+ this.x += Math.cos(this.angle) * this.speed;
+ this.y += Math.sin(this.angle) * this.speed;
+ }
+ }
+
+ class Bullet {
+ constructor(x, y, angle) {
+ this.x = x;
+ this.y = y;
+ this.angle = angle;
+ this.speed = BULLET_SPEED;
+ }
+
+ draw() {
+ ctx.beginPath();
+ ctx.arc(this.x, this.y, 2, 0, Math.PI * 2);
+ ctx.fillStyle = 'white';
+ ctx.fill();
+ }
+
+ update() {
+ this.x += Math.cos(this.angle) * this.speed;
+ this.y += Math.sin(this.angle) * this.speed;
+ }
+ }
+
+ function init() {
+ ship = new Ship();
+ asteroids = [];
+ bullets = [];
+ score = 0;
+ gameOver = false;
+ paused = false;
+ scoreDisplay.textContent = `Score: ${score}`;
+ }
+
+ function update() {
+ if (paused || gameOver) return;
+
+ ship.update();
+ asteroids.forEach(asteroid => asteroid.update());
+ bullets.forEach(bullet => bullet.update());
+
+ // Check for collisions
+ asteroids.forEach((asteroid, aIndex) => {
+ bullets.forEach((bullet, bIndex) => {
+ if (Math.hypot(bullet.x - asteroid.x, bullet.y - asteroid.y) < asteroid.size) {
+ asteroids.splice(aIndex, 1);
+ bullets.splice(bIndex, 1);
+ score++;
+ scoreDisplay.textContent = `Score: ${score}`;
+ }
+ });
+
+ if (Math.hypot(ship.x - asteroid.x, ship.y - asteroid.y) < asteroid.size + SHIP_SIZE) {
+ gameOver = true;
+ }
+ });
+
+ // Spawn new asteroids
+ if (Math.random() < 0.02) {
+ asteroids.push(new Asteroid());
+ }
+ }
+
+ function draw() {
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
+ ship.draw();
+ asteroids.forEach(asteroid => asteroid.draw());
+ bullets.forEach(bullet => bullet.draw());
+
+ if (gameOver) {
+ ctx.fillStyle = 'white';
+ ctx.font = '30px Arial';
+ ctx.fillText('Game Over!', canvas.width / 2 - 70, canvas.height / 2);
+ }
+ }
+
+ function gameLoop() {
+ update();
+ draw();
+ if (!gameOver) requestAnimationFrame(gameLoop);
+ }
+
+ function handleKeyDown(e) {
+ if (e.key === 'ArrowLeft') ship.rotation = -0.1;
+ if (e.key === 'ArrowRight') ship.rotation = 0.1;
+ if (e.key === 'ArrowUp') ship.speed = 2;
+ if (e.key === ' ') {
+ bullets.push(new Bullet(ship.x, ship.y, ship.angle));
+ }
+ }
+
+ function handleKeyUp(e) {
+ if (e.key === 'ArrowLeft' || e.key === 'ArrowRight') ship.rotation = 0;
+ if (e.key === 'ArrowUp') ship.speed = 0;
+ }
+
+ startButton.addEventListener('click', () => {
+ init();
+ gameLoop();
+ });
+
+ pauseButton.addEventListener('click', () => {
+ paused = !paused;
+ pauseButton.textContent = paused ? 'Resume' : 'Pause';
+ });
+
+ window.addEventListener('keydown', handleKeyDown);
+ window.addEventListener('keyup', handleKeyUp);
+
+ init();