|
| 1 | +let canvas = document.getElementById("game"), |
| 2 | + ctx = canvas.getContext("2d"), |
| 3 | + ballRadius = 9, |
| 4 | + x = canvas.width / (Math.floor(Math.random() * Math.random() * 10) + 3), |
| 5 | + y = canvas.height - 40, |
| 6 | + dx = 2, |
| 7 | + dy = -2; |
| 8 | + |
| 9 | +let paddleHeight = 12, |
| 10 | + paddleWidth = 72; |
| 11 | + |
| 12 | +let paddleX = (canvas.width - paddleWidth) / 2; |
| 13 | + |
| 14 | +let rowCount = 5, |
| 15 | + columnCount = 9, |
| 16 | + brickWidth = 54, |
| 17 | + brickHeight = 18, |
| 18 | + brickPadding = 12, |
| 19 | + topOffset = 40, |
| 20 | + leftOffset = 33, |
| 21 | + score = 0; |
| 22 | + |
| 23 | +let bricks = []; |
| 24 | +for (let c = 0; c < columnCount; c++) { |
| 25 | + bricks[c] = []; |
| 26 | + for (let r = 0; r < rowCount; r++) { |
| 27 | + bricks[c][r] = { x: 0, y: 0, status: 1 }; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +document.addEventListener("mousemove", mouseMoveHandler, false); |
| 32 | + |
| 33 | +function mouseMoveHandler(e) { |
| 34 | + var relativeX = e.clientX - canvas.offsetLeft; |
| 35 | + if (relativeX > 0 && relativeX < canvas.width) { |
| 36 | + paddleX = relativeX - paddleWidth / 2; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +function drawPaddle() { |
| 41 | + ctx.beginPath(); |
| 42 | + ctx.roundRect( |
| 43 | + paddleX, |
| 44 | + canvas.height - paddleHeight, |
| 45 | + paddleWidth, |
| 46 | + paddleHeight, |
| 47 | + 30 |
| 48 | + ); |
| 49 | + ctx.fillStyle = "#ad886a"; |
| 50 | + ctx.fill(); |
| 51 | + ctx.closePath(); |
| 52 | +} |
| 53 | + |
| 54 | +function drawBall() { |
| 55 | + ctx.beginPath(); |
| 56 | + ctx.arc(x, y, ballRadius, 0, Math.PI * 2); |
| 57 | + ctx.fillStyle = "#fff"; |
| 58 | + ctx.fill(); |
| 59 | + ctx.closePath(); |
| 60 | +} |
| 61 | + |
| 62 | +function drawBricks() { |
| 63 | + for (let c = 0; c < columnCount; c++) { |
| 64 | + for (let r = 0; r < rowCount; r++) { |
| 65 | + if (bricks[c][r].status === 1) { |
| 66 | + let brickX = c * (brickWidth + brickPadding) + leftOffset; |
| 67 | + let brickY = r * (brickHeight + brickPadding) + topOffset; |
| 68 | + bricks[c][r].x = brickX; |
| 69 | + bricks[c][r].y = brickY; |
| 70 | + ctx.beginPath(); |
| 71 | + ctx.roundRect(brickX, brickY, brickWidth, brickHeight, 30); |
| 72 | + ctx.fillStyle = "#ad886e"; |
| 73 | + ctx.fill(); |
| 74 | + ctx.closePath(); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +function trackScore() { |
| 81 | + ctx.font = "bold 16px sans-serif"; |
| 82 | + ctx.fillStyle = "#fff"; |
| 83 | + ctx.fillText("Score : " + score, 8, 24); |
| 84 | +} |
| 85 | + |
| 86 | +function hitDetection() { |
| 87 | + for (let c = 0; c < columnCount; c++) { |
| 88 | + for (let r = 0; r < rowCount; r++) { |
| 89 | + let b = bricks[c][r]; |
| 90 | + if (b.status === 1) { |
| 91 | + if ( |
| 92 | + x > b.x && |
| 93 | + x < b.x + brickWidth && |
| 94 | + y > b.y && |
| 95 | + y < b.y + brickHeight |
| 96 | + ) { |
| 97 | + dy = -dy; |
| 98 | + b.status = 0; |
| 99 | + score++; |
| 100 | + |
| 101 | + if (score === rowCount * columnCount) { |
| 102 | + alert("You WIN !"); |
| 103 | + document.location.reload(); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +function init() { |
| 112 | + ctx.clearRect(0, 0, canvas.width, canvas.height); |
| 113 | + trackScore(); |
| 114 | + drawBricks(); |
| 115 | + drawBall(); |
| 116 | + drawPaddle(); |
| 117 | + hitDetection(); |
| 118 | + |
| 119 | + if (x + dx > canvas.width - ballRadius || x + dx < ballRadius) { |
| 120 | + dx = -dx; |
| 121 | + } |
| 122 | + |
| 123 | + if (y + dy < ballRadius) { |
| 124 | + dy = -dy; |
| 125 | + } else if (y + dy > canvas.height - ballRadius) { |
| 126 | + if (x > paddleX && x < paddleX + paddleWidth) { |
| 127 | + dy = -dy; |
| 128 | + } else { |
| 129 | + alert("Game Over ! "); |
| 130 | + document.location.reload(); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + if (y + dy > canvas.height - ballRadius || y + dy < ballRadius) { |
| 135 | + dy = -dy; |
| 136 | + } |
| 137 | + |
| 138 | + x += dx; |
| 139 | + y += dy; |
| 140 | +} |
| 141 | + |
| 142 | +setInterval(init, 10); |
0 commit comments