From 5200007d603aefeb313050699d3354bad03a9084 Mon Sep 17 00:00:00 2001 From: Koen van Gilst Date: Mon, 29 Jan 2024 21:17:43 +0100 Subject: [PATCH] attempt to fix infinite loops --- index.html | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/index.html b/index.html index 37672ab..31800af 100644 --- a/index.html +++ b/index.html @@ -208,6 +208,16 @@ return { dx: dx, dy: dy }; } + // clears squares directly adjacent to the ball + function clearAroundBall(x, y) { + let i = Math.floor(x / SQUARE_SIZE); + let j = Math.floor(y / SQUARE_SIZE); + + if (i >= 0 && i < numSquaresX && j >= 0 && j < numSquaresY) { + squares[i][j] = DAY_COLOR; + } + } + function draw() { ctx.clearRect(0, 0, canvas.width, canvas.height); drawSquares(); @@ -235,6 +245,12 @@ x2 += dx2; y2 += dy2; + // Check for collision + if (Math.abs(x1 - x2) < SQUARE_SIZE && Math.abs(y1 - y2) < SQUARE_SIZE) { + clearAroundBall(x1, y1); + clearAroundBall(x2, y2); + } + iteration++; if (iteration % 1_000 === 0) console.log("interation", iteration);