|
150 | 150 | }
|
151 | 151 |
|
152 | 152 | function updateSquareAndBounce(x, y, dx, dy, color) {
|
153 |
| - let updatedDx = dx; |
154 |
| - let updatedDy = dy; |
155 |
| - |
156 |
| - // Check multiple points around the ball's circumference |
157 |
| - for (let angle = 0; angle < Math.PI * 2; angle += Math.PI / 4) { |
158 |
| - let checkX = x + Math.cos(angle) * (SQUARE_SIZE / 2); |
159 |
| - let checkY = y + Math.sin(angle) * (SQUARE_SIZE / 2); |
160 |
| - |
161 |
| - let i = Math.floor(checkX / SQUARE_SIZE); |
162 |
| - let j = Math.floor(checkY / SQUARE_SIZE); |
163 |
| - |
164 |
| - if (i >= 0 && i < numSquaresX && j >= 0 && j < numSquaresY) { |
165 |
| - if (squares[i][j] !== color) { |
166 |
| - squares[i][j] = color; |
167 |
| - |
168 |
| - // Determine bounce direction based on the angle |
169 |
| - if (Math.abs(Math.cos(angle)) > Math.abs(Math.sin(angle))) { |
170 |
| - updatedDx = -updatedDx; |
171 |
| - } else { |
172 |
| - updatedDy = -updatedDy; |
173 |
| - } |
174 |
| - } |
| 153 | + let checkX = x + dx; |
| 154 | + let checkY = y + dy; |
| 155 | + |
| 156 | + let i = Math.floor(x / SQUARE_SIZE); |
| 157 | + let j = Math.floor(y / SQUARE_SIZE); |
| 158 | + let ci = Math.floor(checkX / SQUARE_SIZE); |
| 159 | + let cj = Math.floor(checkY / SQUARE_SIZE); |
| 160 | + |
| 161 | + if (ci >= 0 && ci < numSquaresX && cj >= 0 && cj < numSquaresY) { |
| 162 | + if (squares[ci][j] !== color) { |
| 163 | + squares[ci][j] = color; |
| 164 | + dx = -dx; |
| 165 | + } |
| 166 | + if (squares[i][cj] !== color) { |
| 167 | + squares[i][cj] = color; |
| 168 | + dy = -dy; |
175 | 169 | }
|
176 | 170 | }
|
177 | 171 |
|
178 |
| - return { dx: updatedDx, dy: updatedDy }; |
| 172 | + return { dx, dy }; |
179 | 173 | }
|
180 | 174 |
|
181 | 175 | function updateScoreElement() {
|
|
0 commit comments