Changed around line 1
+ const canvas = document.getElementById('gameCanvas');
+ const ctx = canvas.getContext('2d');
+
+ const player = {
+ x: canvas.width / 2 - 25,
+ y: canvas.height - 60,
+ width: 50,
+ height: 50,
+ color: '#00ff00',
+ speed: 5,
+ isMovingLeft: false,
+ isMovingRight: false
+ };
+
+ const bullets = [];
+ const enemies = [];
+ let gameStarted = false;
+ let gameOver = false;
+
+ function drawPlayer() {
+ ctx.fillStyle = player.color;
+ ctx.fillRect(player.x, player.y, player.width, player.height);
+ }
+
+ function movePlayer() {
+ if (player.isMovingLeft && player.x > 0) {
+ player.x -= player.speed;
+ }
+ if (player.isMovingRight && player.x < canvas.width - player.width) {
+ player.x += player.speed;
+ }
+ }
+
+ function drawBullets() {
+ bullets.forEach((bullet, index) => {
+ ctx.fillStyle = '#ff0000';
+ ctx.fillRect(bullet.x, bullet.y, 5, 15);
+ bullet.y -= 10;
+
+ if (bullet.y < 0) {
+ bullets.splice(index, 1);
+ }
+ });
+ }
+
+ function spawnEnemies() {
+ if (Math.random() < 0.02) {
+ const enemy = {
+ x: Math.random() * (canvas.width - 40),
+ y: -40,
+ width: 40,
+ height: 40,
+ color: '#ffff00'
+ };
+ enemies.push(enemy);
+ }
+ }
+
+ function drawEnemies() {
+ enemies.forEach((enemy, index) => {
+ ctx.fillStyle = enemy.color;
+ ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
+ enemy.y += 2;
+
+ if (enemy.y > canvas.height) {
+ enemies.splice(index, 1);
+ }
+ });
+ }
+
+ function checkCollisions() {
+ bullets.forEach((bullet, bIndex) => {
+ enemies.forEach((enemy, eIndex) => {
+ if (bullet.x < enemy.x + enemy.width &&
+ bullet.x + 5 > enemy.x &&
+ bullet.y < enemy.y + enemy.height &&
+ bullet.y + 15 > enemy.y) {
+ bullets.splice(bIndex, 1);
+ enemies.splice(eIndex, 1);
+ }
+ });
+ });
+ }
+
+ function gameLoop() {
+ if (!gameStarted || gameOver) return;
+
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
+
+ movePlayer();
+ drawPlayer();
+ drawBullets();
+ spawnEnemies();
+ drawEnemies();
+ checkCollisions();
+
+ requestAnimationFrame(gameLoop);
+ }
+
+ document.addEventListener('keydown', (e) => {
+ if (e.code === 'F4') {
+ gameStarted = true;
+ gameOver = false;
+ gameLoop();
+ }
+ if (e.code === 'ArrowLeft') player.isMovingLeft = true;
+ if (e.code === 'ArrowRight') player.isMovingRight = true;
+ if (e.code === 'KeyZ') {
+ bullets.push({
+ x: player.x + player.width / 2 - 2.5,
+ y: player.y,
+ width: 5,
+ height: 15
+ });
+ }
+ });
+
+ document.addEventListener('keyup', (e) => {
+ if (e.code === 'ArrowLeft') player.isMovingLeft = false;
+ if (e.code === 'ArrowRight') player.isMovingRight = false;
+ });
+
+ // Initialize game
+ ctx.fillStyle = '#000';
+ ctx.fillRect(0, 0, canvas.width, canvas.height);