Changed around line 1
+ class Game {
+ constructor() {
+ this.canvas = document.getElementById('gameCanvas');
+ this.ctx = this.canvas.getContext('2d');
+ this.score = 0;
+ this.apples = [];
+ this.bullets = [];
+ this.isRunning = false;
+ this.dailyHighScore = this.loadDailyHighScore();
+
+ this.setupEventListeners();
+ this.updateHighScore();
+ }
+
+ setupEventListeners() {
+ document.getElementById('startButton').addEventListener('click', () => this.startGame());
+ document.getElementById('resetButton').addEventListener('click', () => this.resetGame());
+ this.canvas.addEventListener('click', (e) => this.shoot(e));
+ }
+
+ startGame() {
+ if (this.isRunning) return;
+ this.isRunning = true;
+ this.generateApples();
+ this.gameLoop();
+ }
+
+ resetGame() {
+ this.isRunning = false;
+ this.score = 0;
+ this.apples = [];
+ this.bullets = [];
+ this.updateScore();
+ this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
+ }
+
+ generateApples() {
+ for (let i = 0; i < 10; i++) {
+ this.apples.push({
+ x: Math.random() * (this.canvas.width - 30),
+ y: Math.random() * (this.canvas.height - 30),
+ size: 30,
+ isHit: false
+ });
+ }
+ }
+
+ shoot(e) {
+ if (!this.isRunning) return;
+
+ const rect = this.canvas.getBoundingClientRect();
+ const x = e.clientX - rect.left;
+ const y = e.clientY - rect.top;
+
+ this.bullets.push({x, y, size: 5});
+ this.checkCollisions();
+ }
+
+ checkCollisions() {
+ this.bullets.forEach(bullet => {
+ this.apples.forEach(apple => {
+ if (!apple.isHit && this.detectCollision(bullet, apple)) {
+ apple.isHit = true;
+ this.score += 100;
+ this.updateScore();
+ }
+ });
+ });
+ }
+
+ detectCollision(bullet, apple) {
+ const dx = bullet.x - (apple.x + apple.size/2);
+ const dy = bullet.y - (apple.y + apple.size/2);
+ return Math.sqrt(dx*dx + dy*dy) < (apple.size/2 + bullet.size/2);
+ }
+
+ updateScore() {
+ document.getElementById('currentScore').textContent = this.score;
+ if (this.score > this.dailyHighScore) {
+ this.dailyHighScore = this.score;
+ this.saveDailyHighScore();
+ this.updateHighScore();
+ }
+ }
+
+ loadDailyHighScore() {
+ const today = new Date().toDateString();
+ const saved = localStorage.getItem('dailyHighScore');
+ if (saved) {
+ const {date, score} = JSON.parse(saved);
+ if (date === today) return score;
+ }
+ return 0;
+ }
+
+ saveDailyHighScore() {
+ const today = new Date().toDateString();
+ localStorage.setItem('dailyHighScore', JSON.stringify({
+ date: today,
+ score: this.dailyHighScore
+ }));
+ }
+
+ updateHighScore() {
+ document.getElementById('dailyHighScore').textContent = this.dailyHighScore;
+ }
+
+ draw() {
+ this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
+
+ // Draw apples
+ this.apples.forEach(apple => {
+ if (!apple.isHit) {
+ this.ctx.fillStyle = '#ff6b6b';
+ this.ctx.beginPath();
+ this.ctx.arc(apple.x + apple.size/2, apple.y + apple.size/2, apple.size/2, 0, Math.PI * 2);
+ this.ctx.fill();
+ }
+ });
+
+ // Draw bullets
+ this.bullets.forEach(bullet => {
+ this.ctx.fillStyle = '#4ecdc4';
+ this.ctx.beginPath();
+ this.ctx.arc(bullet.x, bullet.y, bullet.size, 0, Math.PI * 2);
+ this.ctx.fill();
+ });
+ }
+
+ gameLoop() {
+ if (!this.isRunning) return;
+
+ this.draw();
+
+ if (this.apples.every(apple => apple.isHit)) {
+ this.isRunning = false;
+ alert(`Game Over! Final Score: ${this.score}`);
+ return;
+ }
+
+ requestAnimationFrame(() => this.gameLoop());
+ }
+ }
+
+ window.addEventListener('load', () => {
+ new Game();
+ });