-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
145 lines (122 loc) · 3.65 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const startScreen = document.getElementById('startScreen');
const gameScreen = document.getElementById('gameScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const startButton = document.getElementById('startButton');
const restartButton = document.getElementById('restartButton');
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('scoreValue');
const finalScoreElement = document.getElementById('finalScore');
canvas.width = 800;
canvas.height = 600;
let score = 0;
let fruits = [];
let bombs = [];
let gameLoop;
let gameActive = false;
const fruitTypes = [
{ name: 'apple', color: 'red', radius: 30, points: 1 },
{ name: 'orange', color: 'orange', radius: 35, points: 2 },
{ name: 'watermelon', color: 'green', radius: 40, points: 3 },
];
function createFruit() {
const type = fruitTypes[Math.floor(Math.random() * fruitTypes.length)];
return {
x: Math.random() * canvas.width,
y: canvas.height + type.radius,
dy: -Math.random() * 3 - 2,
...type
};
}
function createBomb() {
return {
x: Math.random() * canvas.width,
y: canvas.height + 20,
dy: -Math.random() * 3 - 2,
radius: 20,
color: 'black'
};
}
function update() {
if (!gameActive) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (Math.random() < 0.02) {
fruits.push(createFruit());
}
if (Math.random() < 0.005) {
bombs.push(createBomb());
}
fruits = fruits.filter(fruit => {
fruit.y += fruit.dy;
drawFruit(fruit);
return fruit.y + fruit.radius > 0;
});
bombs = bombs.filter(bomb => {
bomb.y += bomb.dy;
drawBomb(bomb);
return bomb.y + bomb.radius > 0;
});
gameLoop = requestAnimationFrame(update);
}
function drawFruit(fruit) {
ctx.beginPath();
ctx.arc(fruit.x, fruit.y, fruit.radius, 0, Math.PI * 2);
ctx.fillStyle = fruit.color;
ctx.fill();
ctx.closePath();
}
function drawBomb(bomb) {
ctx.beginPath();
ctx.arc(bomb.x, bomb.y, bomb.radius, 0, Math.PI * 2);
ctx.fillStyle = bomb.color;
ctx.fill();
ctx.closePath();
}
function sliceObject(objects, x, y) {
for (let i = objects.length - 1; i >= 0; i--) {
const obj = objects[i];
const dx = x - obj.x;
const dy = y - obj.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < obj.radius) {
objects.splice(i, 1);
return obj;
}
}
return null;
}
function gameOver() {
gameActive = false;
cancelAnimationFrame(gameLoop);
gameScreen.classList.add('hidden');
gameOverScreen.classList.remove('hidden');
finalScoreElement.textContent = score;
}
canvas.addEventListener('mousemove', (event) => {
if (!gameActive) return;
const rect = canvas.getBoundingClientRect();
const mouseX = event.clientX - rect.left;
const mouseY = event.clientY - rect.top;
const slicedFruit = sliceObject(fruits, mouseX, mouseY);
if (slicedFruit) {
score += slicedFruit.points;
scoreElement.textContent = score;
}
const slicedBomb = sliceObject(bombs, mouseX, mouseY);
if (slicedBomb) {
gameOver();
}
});
function startGame() {
score = 0;
fruits = [];
bombs = [];
scoreElement.textContent = score;
startScreen.classList.add('hidden');
gameOverScreen.classList.add('hidden');
gameScreen.classList.remove('hidden');
gameActive = true;
update();
}
startButton.addEventListener('click', startGame);
restartButton.addEventListener('click', startGame);