-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
205 lines (176 loc) · 5.27 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const restartButton = document.getElementById('restartButton');
const bubbleRadius = 20;
const colors = ['red', 'green', 'blue', 'yellow', 'purple'];
let bubbles = [];
let shooterBubble = createBubble(canvas.width / 2, canvas.height - bubbleRadius, getRandomColor());
let shooterDirection = { x: 0, y: 0 };
let gameOver = false;
let gameWon = false;
function createBubble(x, y, color) {
return { x, y, color };
}
function getRandomColor() {
return colors[Math.floor(Math.random() * colors.length)];
}
function drawBubble(bubble) {
ctx.beginPath();
ctx.arc(bubble.x, bubble.y, bubbleRadius, 0, Math.PI * 2);
ctx.fillStyle = bubble.color;
ctx.fill();
ctx.closePath();
}
function drawBubbles() {
bubbles.forEach(drawBubble);
drawBubble(shooterBubble);
}
function moveShooterBubble() {
shooterBubble.x += shooterDirection.x;
shooterBubble.y += shooterDirection.y;
if (shooterBubble.x < bubbleRadius || shooterBubble.x > canvas.width - bubbleRadius) {
shooterDirection.x = -shooterDirection.x;
}
if (shooterBubble.y < bubbleRadius) {
shooterDirection.y = -shooterDirection.y;
}
}
function detectCollision() {
for (let i = 0; i < bubbles.length; i++) {
const bubble = bubbles[i];
const dx = bubble.x - shooterBubble.x;
const dy = bubble.y - shooterBubble.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < bubbleRadius * 2) {
bubbles.push(shooterBubble);
checkForMatches(shooterBubble);
shooterBubble = createBubble(canvas.width / 2, canvas.height - bubbleRadius, getRandomColor());
shooterDirection = { x: 0, y: 0 };
break;
}
}
}
function checkForMatches(bubble) {
const matches = [];
const visited = new Set();
function floodFill(bubble) {
if (visited.has(bubble)) return;
visited.add(bubble);
matches.push(bubble);
const neighbors = getNeighbors(bubble);
for (const neighbor of neighbors) {
if (neighbor.color === bubble.color) {
floodFill(neighbor);
}
}
}
floodFill(bubble);
if (matches.length >= 3) {
removeMatches(matches);
}
}
function getNeighbors(bubble) {
const neighbors = [];
for (const otherBubble of bubbles) {
const dx = otherBubble.x - bubble.x;
const dy = otherBubble.y - bubble.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < bubbleRadius * 2.1) {
neighbors.push(otherBubble);
}
}
return neighbors;
}
function removeMatches(matches) {
bubbles = bubbles.filter(bubble => !matches.includes(bubble));
}
function initializeBubbles() {
bubbles = [];
const rows = 5;
const cols = Math.floor(canvas.width / (bubbleRadius * 2));
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
const x = col * bubbleRadius * 2 + bubbleRadius;
const y = row * bubbleRadius * 2 + bubbleRadius;
const color = getRandomColor();
bubbles.push(createBubble(x, y, color));
}
}
}
function checkGameOver() {
for (let i = 0; i < bubbles.length; i++) {
if (bubbles[i].y + bubbleRadius >= canvas.height) {
gameOver = true;
return true;
}
}
return false;
}
function checkGameWon() {
if (bubbles.length === 0) {
gameWon = true;
return true;
}
return false;
}
function displayGameOver() {
ctx.fillStyle = 'white';
ctx.font = '48px serif';
ctx.textAlign = 'center';
ctx.fillText('Game Over', canvas.width / 2, canvas.height / 2);
restartButton.style.display = 'block';
}
function displayGameWon() {
ctx.fillStyle = 'white';
ctx.font = '48px serif';
ctx.textAlign = 'center';
ctx.fillText('You Win!', canvas.width / 2, canvas.height / 2);
restartButton.style.display = 'block';
}
function gameLoop() {
if (gameOver) {
displayGameOver();
return;
}
if (gameWon) {
displayGameWon();
return;
}
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBubbles();
moveShooterBubble();
detectCollision();
if (checkGameOver()) {
displayGameOver();
return;
}
if (checkGameWon()) {
displayGameWon();
return;
}
requestAnimationFrame(gameLoop);
}
canvas.addEventListener('click', (event) => {
if (gameOver || gameWon) return;
const rect = canvas.getBoundingClientRect();
const mouseX = event.clientX - rect.left;
const mouseY = event.clientY - rect.top;
const dx = mouseX - shooterBubble.x;
const dy = mouseY - shooterBubble.y;
const distance = Math.sqrt(dx * dx + dy * dy);
shooterDirection = {
x: (dx / distance) * 5,
y: (dy / distance) * 5
};
});
restartButton.addEventListener('click', () => {
gameOver = false;
gameWon = false;
shooterBubble = createBubble(canvas.width / 2, canvas.height - bubbleRadius, getRandomColor());
shooterDirection = { x: 0, y: 0 };
initializeBubbles();
restartButton.style.display = 'none';
gameLoop();
});
initializeBubbles();
gameLoop();