|
| 1 | +document.addEventListener("DOMContentLoaded", () => { |
| 2 | + const grid = document.querySelector(".grid"); |
| 3 | + let squares = Array.from(document.querySelectorAll(".grid div")); |
| 4 | + const scoreDisplay = document.querySelector("#score"); |
| 5 | + const startBtn = document.querySelector("#start-button"); |
| 6 | + const width = 10; |
| 7 | + let nextRandom = 0; |
| 8 | + let timerId; |
| 9 | + let score = 0; |
| 10 | + const colors = ["orange", "red", "purple", "green", "blue"]; |
| 11 | + |
| 12 | + //The Tetrominoes |
| 13 | + const lTetromino = [ |
| 14 | + [1, width + 1, width * 2 + 1, 2], |
| 15 | + [width, width + 1, width + 2, width * 2 + 2], |
| 16 | + [1, width + 1, width * 2 + 1, width * 2], |
| 17 | + [width, width * 2, width * 2 + 1, width * 2 + 2], |
| 18 | + ]; |
| 19 | + |
| 20 | + const zTetromino = [ |
| 21 | + [0, width, width + 1, width * 2 + 1], |
| 22 | + [width + 1, width + 2, width * 2, width * 2 + 1], |
| 23 | + [0, width, width + 1, width * 2 + 1], |
| 24 | + [width + 1, width + 2, width * 2, width * 2 + 1], |
| 25 | + ]; |
| 26 | + |
| 27 | + const tTetromino = [ |
| 28 | + [1, width, width + 1, width + 2], |
| 29 | + [1, width + 1, width + 2, width * 2 + 1], |
| 30 | + [width, width + 1, width + 2, width * 2 + 1], |
| 31 | + [1, width, width + 1, width * 2 + 1], |
| 32 | + ]; |
| 33 | + |
| 34 | + const oTetromino = [ |
| 35 | + [0, 1, width, width + 1], |
| 36 | + [0, 1, width, width + 1], |
| 37 | + [0, 1, width, width + 1], |
| 38 | + [0, 1, width, width + 1], |
| 39 | + ]; |
| 40 | + |
| 41 | + const iTetromino = [ |
| 42 | + [1, width + 1, width * 2 + 1, width * 3 + 1], |
| 43 | + [width, width + 1, width + 2, width + 3], |
| 44 | + [1, width + 1, width * 2 + 1, width * 3 + 1], |
| 45 | + [width, width + 1, width + 2, width + 3], |
| 46 | + ]; |
| 47 | + |
| 48 | + const theTetrominoes = [ |
| 49 | + lTetromino, |
| 50 | + zTetromino, |
| 51 | + tTetromino, |
| 52 | + oTetromino, |
| 53 | + iTetromino, |
| 54 | + ]; |
| 55 | + |
| 56 | + let currentPosition = 4; |
| 57 | + let currentRotation = 0; |
| 58 | + |
| 59 | + console.log(theTetrominoes[0][0]); |
| 60 | + |
| 61 | + //randomly select a Tetromino and its first rotation |
| 62 | + let random = Math.floor(Math.random() * theTetrominoes.length); |
| 63 | + let current = theTetrominoes[random][currentRotation]; |
| 64 | + |
| 65 | + //draw the Tetromino |
| 66 | + function draw() { |
| 67 | + current.forEach((index) => { |
| 68 | + squares[currentPosition + index].classList.add("tetromino"); |
| 69 | + squares[currentPosition + index].style.backgroundColor = colors[random]; |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + //undraw the Tetromino |
| 74 | + function undraw() { |
| 75 | + current.forEach((index) => { |
| 76 | + squares[currentPosition + index].classList.remove("tetromino"); |
| 77 | + squares[currentPosition + index].style.backgroundColor = ""; |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + //assign functions to keyCodes |
| 82 | + function control(e) { |
| 83 | + if (e.keyCode === 37) { |
| 84 | + moveLeft(); |
| 85 | + } else if (e.keyCode === 38) { |
| 86 | + rotate(); |
| 87 | + } else if (e.keyCode === 39) { |
| 88 | + moveRight(); |
| 89 | + } else if (e.keyCode === 40) { |
| 90 | + moveDown(); |
| 91 | + } |
| 92 | + } |
| 93 | + document.addEventListener("keyup", control); |
| 94 | + |
| 95 | + //move down function |
| 96 | + function moveDown() { |
| 97 | + undraw(); |
| 98 | + currentPosition += width; |
| 99 | + draw(); |
| 100 | + freeze(); |
| 101 | + } |
| 102 | + |
| 103 | + //freeze function |
| 104 | + function freeze() { |
| 105 | + if ( |
| 106 | + current.some((index) => |
| 107 | + squares[currentPosition + index + width].classList.contains("taken") |
| 108 | + ) |
| 109 | + ) { |
| 110 | + current.forEach((index) => |
| 111 | + squares[currentPosition + index].classList.add("taken") |
| 112 | + ); |
| 113 | + //start a new tetromino falling |
| 114 | + random = nextRandom; |
| 115 | + nextRandom = Math.floor(Math.random() * theTetrominoes.length); |
| 116 | + current = theTetrominoes[random][currentRotation]; |
| 117 | + currentPosition = 4; |
| 118 | + draw(); |
| 119 | + displayShape(); |
| 120 | + addScore(); |
| 121 | + gameOver(); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + //move the tetromino left, unless is at the edge or there is a blockage |
| 126 | + function moveLeft() { |
| 127 | + undraw(); |
| 128 | + const isAtLeftEdge = current.some( |
| 129 | + (index) => (currentPosition + index) % width === 0 |
| 130 | + ); |
| 131 | + if (!isAtLeftEdge) currentPosition -= 1; |
| 132 | + if ( |
| 133 | + current.some((index) => |
| 134 | + squares[currentPosition + index].classList.contains("taken") |
| 135 | + ) |
| 136 | + ) { |
| 137 | + currentPosition += 1; |
| 138 | + } |
| 139 | + draw(); |
| 140 | + } |
| 141 | + |
| 142 | + //move the tetromino right, unless is at the edge or there is a blockage |
| 143 | + function moveRight() { |
| 144 | + undraw(); |
| 145 | + const isAtRightEdge = current.some( |
| 146 | + (index) => (currentPosition + index) % width === width - 1 |
| 147 | + ); |
| 148 | + if (!isAtRightEdge) currentPosition += 1; |
| 149 | + if ( |
| 150 | + current.some((index) => |
| 151 | + squares[currentPosition + index].classList.contains("taken") |
| 152 | + ) |
| 153 | + ) { |
| 154 | + currentPosition -= 1; |
| 155 | + } |
| 156 | + draw(); |
| 157 | + } |
| 158 | + |
| 159 | + //rotate the tetromino |
| 160 | + function rotate() { |
| 161 | + undraw(); |
| 162 | + currentRotation++; |
| 163 | + if (currentRotation === current.length) { |
| 164 | + //if the current roation gets to 4, make it go back to 0 |
| 165 | + currentRotation = 0; |
| 166 | + } |
| 167 | + current = theTetrominoes[random][currentRotation]; |
| 168 | + draw(); |
| 169 | + } |
| 170 | + |
| 171 | + //show up-next tetromino in mini-grid display |
| 172 | + const displaySquares = document.querySelectorAll(".mini-grid div"); |
| 173 | + const displayWidth = 4; |
| 174 | + const displayIndex = 0; |
| 175 | + |
| 176 | + //the Tetrominos without rotations |
| 177 | + const upNextTetrominoes = [ |
| 178 | + [1, displayWidth + 1, displayWidth * 2 + 1, 2], //lTetromino |
| 179 | + [0, displayWidth, displayWidth + 1, displayWidth * 2 + 1], //zTetromino |
| 180 | + [1, displayWidth, displayWidth + 1, displayWidth + 2], //tTetromino |
| 181 | + [0, 1, displayWidth, displayWidth + 1], //oTetromino |
| 182 | + [1, displayWidth + 1, displayWidth * 2 + 1, displayWidth * 3 + 1], //iTetromino |
| 183 | + ]; |
| 184 | + |
| 185 | + //display the shape in the mini-grid display |
| 186 | + function displayShape() { |
| 187 | + //remove any trace of a tetromino form the entire grid |
| 188 | + displaySquares.forEach((square) => { |
| 189 | + square.classList.remove("tetromino"); |
| 190 | + square.style.backgroundColor = ""; |
| 191 | + }); |
| 192 | + upNextTetrominoes[nextRandom].forEach((index) => { |
| 193 | + displaySquares[displayIndex + index].classList.add("tetromino"); |
| 194 | + displaySquares[displayIndex + index].style.backgroundColor = |
| 195 | + colors[nextRandom]; |
| 196 | + }); |
| 197 | + } |
| 198 | + |
| 199 | + //add functionality to the button |
| 200 | + startBtn.addEventListener("click", () => { |
| 201 | + if (timerId) { |
| 202 | + clearInterval(timerId); |
| 203 | + timerId = null; |
| 204 | + } else { |
| 205 | + draw(); |
| 206 | + timerId = setInterval(moveDown, 1000); |
| 207 | + nextRandom = Math.floor(Math.random() * theTetrominoes.length); |
| 208 | + displayShape(); |
| 209 | + } |
| 210 | + }); |
| 211 | + |
| 212 | + //add score |
| 213 | + function addScore() { |
| 214 | + for (let i = 0; i < 199; i += width) { |
| 215 | + const row = [ |
| 216 | + i, |
| 217 | + i + 1, |
| 218 | + i + 2, |
| 219 | + i + 3, |
| 220 | + i + 4, |
| 221 | + i + 5, |
| 222 | + i + 6, |
| 223 | + i + 7, |
| 224 | + i + 8, |
| 225 | + i + 9, |
| 226 | + ]; |
| 227 | + |
| 228 | + if (row.every((index) => squares[index].classList.contains("taken"))) { |
| 229 | + score += 10; |
| 230 | + scoreDisplay.innerHTML = score; |
| 231 | + row.forEach((index) => { |
| 232 | + squares[index].classList.remove("taken"); |
| 233 | + squares[index].classList.remove("tetromino"); |
| 234 | + squares[index].style.backgroundColor = ""; |
| 235 | + }); |
| 236 | + const squaresRemoved = squares.splice(i, width); |
| 237 | + squares = squaresRemoved.concat(squares); |
| 238 | + squares.forEach((cell) => grid.appendChild(cell)); |
| 239 | + } |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + //game over |
| 244 | + function gameOver() { |
| 245 | + if ( |
| 246 | + current.some((index) => |
| 247 | + squares[currentPosition + index].classList.contains("taken") |
| 248 | + ) |
| 249 | + ) { |
| 250 | + scoreDisplay.innerHTML = "end"; |
| 251 | + clearInterval(timerId); |
| 252 | + } |
| 253 | + } |
| 254 | +}); |
0 commit comments