|
| 1 | +var config = { |
| 2 | + width: 300, |
| 3 | + height: 600, |
| 4 | + rows: 6, |
| 5 | + cols: 4, |
| 6 | + speed: 5, |
| 7 | + interval: 20, |
| 8 | +}; |
| 9 | +config.height = 600 - 2; |
| 10 | +config.defaultSpeed = config.speed; |
| 11 | +var score = 0; |
| 12 | +var scoreElement; |
| 13 | +var startGameElement, endGameElement; |
| 14 | +var scoreElement; |
| 15 | +var gameLoop; |
| 16 | +var tileRows = []; |
| 17 | +var gameCanvas; |
| 18 | +var gameContext; |
| 19 | +var isGameStarted = false; |
| 20 | +document.addEventListener( |
| 21 | + "DOMContentLoaded", |
| 22 | + function () { |
| 23 | + console.log("Script Loaded"); |
| 24 | + gameCanvas = document.getElementById("gameCanvas"); |
| 25 | + scoreElement = document.getElementById("score"); |
| 26 | + startGameElement = document.getElementById("gameStart"); |
| 27 | + endGameElement = document.getElementById("gameEnd"); |
| 28 | + gameContext = gameCanvas.getContext("2d"); |
| 29 | + gameCanvas.style.width = config.width + "px"; |
| 30 | + gameCanvas.style.height = config.height + "px"; |
| 31 | + gameCanvas.setAttribute("width", config.width); |
| 32 | + gameCanvas.setAttribute("height", config.height); |
| 33 | + gameContext.lineWidth = 0.5; |
| 34 | + initGame(); |
| 35 | + }, |
| 36 | + null |
| 37 | +); |
| 38 | + |
| 39 | +function addRow() { |
| 40 | + var black_index = Math.floor(Math.random() * config.cols); |
| 41 | + var tile_width = config.width / config.cols; |
| 42 | + var tile_height = config.height / config.rows; |
| 43 | + var y = config.height; |
| 44 | + if (tileRows.length > 0) { |
| 45 | + var lastRow = tileRows[tileRows.length - 1]; |
| 46 | + y = lastRow.y + lastRow.height; |
| 47 | + } |
| 48 | + var row = { |
| 49 | + x: 0, |
| 50 | + y: y, |
| 51 | + width: config.width, |
| 52 | + height: config.height / config.rows, |
| 53 | + tileWidth: tile_width, |
| 54 | + tileHeight: tile_height, |
| 55 | + color: "#FFFFFF", |
| 56 | + black: { |
| 57 | + index: black_index, |
| 58 | + color: "#000000", |
| 59 | + }, |
| 60 | + increament: function () { |
| 61 | + if (this.y <= 0) { |
| 62 | + console.log(this.isValid); |
| 63 | + if (!this.isValid) { |
| 64 | + console.log("Game Over"); |
| 65 | + stopGameLoop(); |
| 66 | + this.y -= config.speed; |
| 67 | + displayWrongTile(this, this.black.index); |
| 68 | + return; |
| 69 | + } |
| 70 | + } |
| 71 | + this.y = this.y - config.speed; |
| 72 | + }, |
| 73 | + decreament: function () { |
| 74 | + this.y = this.y + config.speed; |
| 75 | + }, |
| 76 | + isValid: false, |
| 77 | + }; |
| 78 | + tileRows.push(row); |
| 79 | +} |
| 80 | + |
| 81 | +function displayRow(row) { |
| 82 | + gameContext.fillStyle = row.color; |
| 83 | + gameContext.fillRect(0, row.y, row.width, row.height); |
| 84 | + for (var i = 0; i < config.cols; i++) { |
| 85 | + gameContext.strokeRect( |
| 86 | + i * row.tileWidth, |
| 87 | + row.y, |
| 88 | + row.tileWidth, |
| 89 | + row.tileHeight |
| 90 | + ); |
| 91 | + |
| 92 | + if (row.black.index == i) { |
| 93 | + gameContext.fillStyle = row.black.color; |
| 94 | + gameContext.fillRect( |
| 95 | + i * row.tileWidth, |
| 96 | + row.y, |
| 97 | + row.tileWidth, |
| 98 | + row.tileHeight |
| 99 | + ); |
| 100 | + } |
| 101 | + } |
| 102 | + row.increament(); |
| 103 | +} |
| 104 | +function startGameLoop() { |
| 105 | + gameLoop = setInterval(function () { |
| 106 | + displayAllRow(); |
| 107 | + }, config.interval); |
| 108 | +} |
| 109 | +function displayAllRow() { |
| 110 | + gameContext.clearRect(0, 0, config.width, config.height); |
| 111 | + for (var i = 0; i < tileRows.length; i++) { |
| 112 | + displayRow(tileRows[i]); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +function stopGameLoop() { |
| 117 | + clearInterval(gameLoop); |
| 118 | + gameCanvas.removeEventListener("click", handleGameUserInput); |
| 119 | + endGameElement.style.display = "block"; |
| 120 | +} |
| 121 | + |
| 122 | +function handleGameUserInput(e) { |
| 123 | + if (!isGameStarted) { |
| 124 | + isGameStarted = true; |
| 125 | + startGameLoop(); |
| 126 | + } |
| 127 | + var tile_width = config.width / config.cols; |
| 128 | + var tile_height = config.height / config.rows; |
| 129 | + var x = e.clientX - gameCanvas.offsetLeft; |
| 130 | + var y = e.clientY - gameCanvas.offsetTop; |
| 131 | + var clicked_row = Math.ceil(y / tile_height) - 1; |
| 132 | + var clicked_col = Math.ceil(x / tile_width) - 1; |
| 133 | + for (var i = 0; i < tileRows.length; i++) { |
| 134 | + var row = tileRows[i]; |
| 135 | + if (row.y < y && row.y + row.height > y) { |
| 136 | + if (clicked_col === row.black.index) { |
| 137 | + if (!row.isValid) { |
| 138 | + row.isValid = true; |
| 139 | + row.black.color = "#AAAAAA"; |
| 140 | + score++; |
| 141 | + scoreElement.innerHTML = score; |
| 142 | + addRow(); |
| 143 | + } else { |
| 144 | + stopGameLoop(); |
| 145 | + displayWrongTile(row, clicked_col); |
| 146 | + } |
| 147 | + } else { |
| 148 | + stopGameLoop(); |
| 149 | + displayWrongTile(row, clicked_col); |
| 150 | + } |
| 151 | + break; |
| 152 | + } |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +function displayWrongTile(row, col_number) { |
| 157 | + gameContext.fillStyle = "#FF0000"; |
| 158 | + row.decreament(); |
| 159 | + gameContext.fillRect( |
| 160 | + col_number * row.tileWidth, |
| 161 | + row.y, |
| 162 | + row.tileWidth, |
| 163 | + row.tileHeight |
| 164 | + ); |
| 165 | +} |
| 166 | + |
| 167 | +function initGame() { |
| 168 | + gameContext.clearRect(0, 0, config.width, config.height); |
| 169 | + for (var i = 0; i < config.rows; i++) { |
| 170 | + addRow(); |
| 171 | + } |
| 172 | + for (var j = 0; j < 50; j++) { |
| 173 | + for (var i = 0; i < tileRows.length; i++) { |
| 174 | + tileRows[i].increament(); |
| 175 | + } |
| 176 | + } |
| 177 | + for (var i = 0; i < tileRows.length; i++) { |
| 178 | + displayRow(tileRows[i]); |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +document.getElementById("startBtn").addEventListener("click", startGame) |
| 183 | +function startGame() { |
| 184 | + endGameElement.style.display = "none"; |
| 185 | + startGameElement.style.display = "none"; |
| 186 | + gameCanvas.addEventListener("click", handleGameUserInput); |
| 187 | +} |
| 188 | + |
| 189 | +document.getElementById("restartBtn").addEventListener("click", restartGame); |
| 190 | + |
| 191 | +function restartGame() { |
| 192 | + tileRows = []; |
| 193 | + score = 0; |
| 194 | + isGameStarted = false; |
| 195 | + config.speed = config.defaultSpeed; |
| 196 | + scoreElement.innerHTML = score; |
| 197 | + endGameElement.style.display = "none"; |
| 198 | + initGame(); |
| 199 | + startGame(); |
| 200 | + // setTimeout(startGame,1000); |
| 201 | +} |
| 202 | + |
| 203 | + |
0 commit comments