function fillBoard() { for (let y = 0; y < baseHeight; y++) { board.push([]) for (let x = 0; x < baseWidth; x++) { board[y][x] = { status: CellStatus.Empty } } } } fillBoard()
function drawCells() { // ctx.strokeStyle = 'white' for (let y = 0; y < baseHeight; y += 1) { for (let x = 0; x < baseWidth; x += 1) { if (board[y][x].status === CellStatus.Fill) { ctx.fillStyle = 'blue' ctx.fillRect( x * cellSize, y * cellSize, cellSize, cellSize ) }
if (board[y][x].status === CellStatus.Fixed) { ctx.fillStyle = 'gray' ctx.fillRect( x * cellSize, y * cellSize, cellSize, cellSize ) } } } }
function outOfRangeRight(tetris) { for (let cell of tetris) { let x = cell.x if (x >= baseWidth - 1) return true
if (board[cell.y][x + 1].status === CellStatus.Fixed) return true }
return false }
function outOfRangeLeft(tetris) { for (let cell of tetris) { let x = cell.x if (x <= 0) return true
if (board[cell.y][x - 1].status === CellStatus.Fixed) return true }
return false }
function right() { if (outOfRangeRight(current) === false) { //先清空格子 for (let cell of current) { let x = cell.x let y = cell.y board[y][x] = { status: CellStatus.Empty } } //然後才塞 for (let cell of current) { cell.x += 1 let x = cell.x let y = cell.y board[y][x] = { status: cell.status } }
drawBackground() drawCells() } }
function left() { if (outOfRangeLeft(current) === false) { //先清空格子 for (let cell of current) { let x = cell.x let y = cell.y board[y][x] = { status: CellStatus.Empty } } //然後才塞 for (let cell of current) { cell.x -= 1 let x = cell.x let y = cell.y board[y][x] = { status: cell.status } } drawBackground() drawCells() } }
function clearLine() { //從底下往上掃描看哪一行目前非空白 let lastIndex = undefined for (let y = baseHeight - 1; y >= 0; y--) { let row = board[y] let isFixed = row.every(c => c.status === CellStatus.Fixed) if (isFixed) { if (y === 0) lastIndex = 0 else lastIndex = y break } } if (lastIndex) { let row = board[lastIndex] //設定格子被清空 for (let cell of row) { cell.status = CellStatus.Empty }
function down() { if (outOfRangeY(current) === false) { //先清空格子 for (let cell of current) { let x = cell.x let y = cell.y board[y][x] = { status: CellStatus.Empty } } //然後才塞 for (let cell of current) { cell.y += 1 let x = cell.x let y = cell.y board[y][x] = { status: cell.status } }
//清除線 clearLine()
} else {
//固定方塊的動作 for (let cell of current) { let x = cell.x let y = cell.y board[y][x] = { status: CellStatus.Fixed } }
//清除線 clearLine()
//判斷是否已經 gg //沒 gg 的話才會產生新的方塊 if (isGoodGame() === false) {
function spaceDown() { while (outOfRangeY(current) === false) { //先清空格子 for (let cell of current) { let x = cell.x let y = cell.y board[y][x] = { status: CellStatus.Empty } } //然後才塞 for (let cell of current) { cell.y += 1 let x = cell.x let y = cell.y board[y][x] = { status: cell.status } }
//清除線 clearLine() }
//超過範圍了
//固定方塊的動作 for (let cell of current) { let x = cell.x let y = cell.y board[y][x] = { status: CellStatus.Fixed } }
//清除線 clearLine()
//判斷是否已經 gg //沒 gg 的話才會產生新的方塊 if (isGoodGame() === false) {
//產生新的方塊 current = genNewTetris() } }
GG 判定
GG 判定比較簡單 , 只要取得最上面的 row 看看是否有 Fixed 即可
1 2 3 4 5
function isGoodGame() { //取得最上面那條的是否固定了 let result = board[0].some(c => c.status === CellStatus.Fixed) return result }