Changed around line 1
+ class GalagaGame {
+ constructor() {
+ this.canvas = document.getElementById('gameCanvas');
+ this.ctx = this.canvas.getContext('2d');
+ this.setupCanvas();
+ this.player = {
+ x: this.canvas.width / 2,
+ y: this.canvas.height - 50,
+ width: 30,
+ height: 30,
+ speed: 5
+ };
+ this.bullets = [];
+ this.enemies = [];
+ this.isGameRunning = false;
+ this.score = 0;
+ this.keys = {};
+
+ this.bindEvents();
+ this.createEnemies();
+ }
+
+ setupCanvas() {
+ this.canvas.width = 800;
+ this.canvas.height = 600;
+ }
+
+ bindEvents() {
+ document.addEventListener('keydown', (e) => {
+ this.keys[e.key] = true;
+ if (e.key === 'F4') {
+ e.preventDefault();
+ this.startGame();
+ }
+ if (e.key === 'z' || e.key === 'Z') {
+ this.shoot();
+ }
+ });
+
+ document.addEventListener('keyup', (e) => {
+ this.keys[e.key] = false;
+ });
+ }
+
+ createEnemies() {
+ for (let i = 0; i < 4; i++) {
+ for (let j = 0; j < 8; j++) {
+ this.enemies.push({
+ x: 80 + j * 80,
+ y: 50 + i * 60,
+ width: 30,
+ height: 30,
+ direction: 1
+ });
+ }
+ }
+ }
+
+ shoot() {
+ if (!this.isGameRunning) return;
+ this.bullets.push({
+ x: this.player.x + this.player.width / 2,
+ y: this.player.y,
+ width: 3,
+ height: 15,
+ speed: 7
+ });
+ }
+
+ update() {
+ if (!this.isGameRunning) return;
+
+ // Player movement
+ if (this.keys['ArrowLeft']) {
+ this.player.x = Math.max(0, this.player.x - this.player.speed);
+ }
+ if (this.keys['ArrowRight']) {
+ this.player.x = Math.min(this.canvas.width - this.player.width, this.player.x + this.player.speed);
+ }
+
+ // Update bullets
+ this.bullets.forEach((bullet, index) => {
+ bullet.y -= bullet.speed;
+ if (bullet.y < 0) {
+ this.bullets.splice(index, 1);
+ }
+ });
+
+ // Update enemies
+ let enemyDirection = 1;
+ this.enemies.forEach(enemy => {
+ enemy.x += enemy.direction * 2;
+ if (enemy.x <= 0 || enemy.x >= this.canvas.width - enemy.width) {
+ enemyDirection = -enemy.direction;
+ }
+ });
+
+ if (enemyDirection !== 1) {
+ this.enemies.forEach(enemy => {
+ enemy.direction = enemyDirection;
+ enemy.y += 20;
+ });
+ }
+
+ // Collision detection
+ this.bullets.forEach((bullet, bulletIndex) => {
+ this.enemies.forEach((enemy, enemyIndex) => {
+ if (this.checkCollision(bullet, enemy)) {
+ this.bullets.splice(bulletIndex, 1);
+ this.enemies.splice(enemyIndex, 1);
+ this.score += 100;
+ }
+ });
+ });
+
+ this.draw();
+ requestAnimationFrame(() => this.update());
+ }
+
+ checkCollision(rect1, rect2) {
+ return rect1.x < rect2.x + rect2.width &&
+ rect1.x + rect1.width > rect2.x &&
+ rect1.y < rect2.y + rect2.height &&
+ rect1.y + rect1.height > rect2.y;
+ }
+
+ draw() {
+ this.ctx.fillStyle = '#000';
+ this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
+
+ // Draw player
+ this.ctx.fillStyle = '#fff';
+ this.ctx.fillRect(this.player.x, this.player.y, this.player.width, this.player.height);
+
+ // Draw bullets
+ this.ctx.fillStyle = '#ff0';
+ this.bullets.forEach(bullet => {
+ this.ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
+ });
+
+ // Draw enemies
+ this.ctx.fillStyle = '#f00';
+ this.enemies.forEach(enemy => {
+ this.ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
+ });
+
+ // Draw score
+ this.ctx.fillStyle = '#fff';
+ this.ctx.font = '20px "Press Start 2P"';
+ this.ctx.fillText(`Score: ${this.score}`, 10, 30);
+ }
+
+ startGame() {
+ if (!this.isGameRunning) {
+ this.isGameRunning = true;
+ this.score = 0;
+ this.enemies = [];
+ this.bullets = [];
+ this.createEnemies();
+ this.update();
+ }
+ }
+ }
+
+ window.addEventListener('load', () => {
+ const game = new GalagaGame();
+ });