Last active
March 6, 2019 15:40
-
-
Save unstoppablecarl/2dc6f7f9f5402e1f657f3c1106b54ce0 to your computer and use it in GitHub Desktop.
Revisions
-
unstoppablecarl revised this gist
Mar 6, 2019 . 3 changed files with 62 additions and 35 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1 +1 @@ This is a [SCRIPT-8](https://script-8.github.io) cassette. Click [here](https://script-8.github.io/?id=2dc6f7f9f5402e1f657f3c1106b54ce0) to boot it. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,9 @@ const ROW = 10; const COL = 10; const SQ = 7; const VACANT = -1; const FILL = 2; // const DEBUG = 2; let board = []; for(let r = 0; r <ROW; r++){ @@ -16,14 +17,7 @@ initialState = { board: board, count: 0, tick: 0, piece: false } const drawSquare = (x, y, type) => { @@ -36,11 +30,13 @@ const drawSquare = (x, y, type) => { if(type == VACANT){ color = 6; rectStroke(x, y, width, height, color) return } color = type; rectFill(x, y, width, height, color) rectStroke(x, y, width, height, color+1) } function drawBoard(board){ @@ -55,6 +51,12 @@ const piece = makePieceHandler(); update = (state, input, elapsed) => { if(!state.piece){ state.piece = makePiece(); log(state.piece); } // state.board[4][6] = FILL; state.tick += elapsed; const updateInterval = 250; if(state.tick < updateInterval){ @@ -70,6 +72,20 @@ draw = (state) => { clear() drawBoard(state.board); } function makePiece(){ log('z'); return { x: 4, y: 0, color: Math.floor(Math.random() * 6), shape: [ [0,1,0], [1,1,1] ] } } function makePieceHandler(){ let piece; @@ -79,29 +95,32 @@ function makePieceHandler(){ update(state){ piece = state.piece; board = state.board; if(hasCollision()){ state.piece = makePiece(); } else{ movePiece(); } } } function hasCollision(){ if(piece.y == 9) { return true;; } let collision = false; eachCollisionCoord((x, y) => { if(board[x][y] !== VACANT){ collision = true; } }); return collision; } function eachCoord(callback){ piece.shape.forEach((row, y) => { row.forEach((val, x) => { callback(x, y, val); @@ -110,21 +129,29 @@ function makePieceHandler(){ } function fillPiece(color){ eachCoord((x, y, val) => { if(val){ board[piece.x + x][piece.y + y] = color; } }); } function eachCollisionCoord(callback){ let bottomRowIndex = piece.shape.length - 1; let bottomRow = piece.shape[bottomRowIndex]; let checkY = piece.y + bottomRowIndex + 1; bottomRow.forEach((val, x) => { if(val){ callback(piece.x + x, checkY); } }); } function movePiece() { fillPiece(VACANT); piece.y += 1; fillPiece(piece.color); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ { "lines": [ 74, 83, 0, 0, 0, -
unstoppablecarl created this gist
Mar 6, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ This is a [SCRIPT-8](https://script-8.github.io) cassette. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ {} This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,130 @@ const ROW = 10; const COL = 10; const SQ = 7; const VACANT = 0; const FILL = 1; let board = []; for(let r = 0; r <ROW; r++){ board[r] = []; for(let c = 0; c < COL; c++){ board[r][c] = VACANT; } } initialState = { board: board, count: 0, tick: 0, piece: { x: 4, y: 0, shape: [ [0,1,0], [1,1,1] ] } } const drawSquare = (x, y, type) => { let color; x *= SQ; y *= SQ; let width = SQ - 1; let height = SQ - 1; if(type == VACANT){ color = 6; rectStroke(x, y, width, height, color) } if(type == FILL){ color = 2; rectFill(x, y, width, height, color) } } function drawBoard(board){ range(ROW).forEach((r) => { range(COL).forEach((c) => { drawSquare(c, r, board[c][r]) }) }); } const piece = makePieceHandler(); update = (state, input, elapsed) => { state.tick += elapsed; const updateInterval = 250; if(state.tick < updateInterval){ return } state.tick = 0; piece.update(state, input); } draw = (state) => { clear() drawBoard(state.board); } function makePieceHandler(){ let piece; let board; return { update(state){ piece = state.piece; board = state.board; if(piece.y == 9) { return; } if(!hasCollision()){ movePiece(); } } } function hasCollision(){ let collision = false; eachPieceCoord((x, y, val) => { if(board[x][y + 1] == FILL){ collision = true; } }); return collision; } function eachPieceCoord(callback){ piece.shape.forEach((row, y) => { row.forEach((val, x) => { callback(x, y, val); }) }) } function fillPiece(color){ eachPieceCoord((x, y, val) => { if(val){ board[piece.x + x][piece.y + y] = color; } }); } function movePiece() { fillPiece(VACANT); piece.y += 1; fillPiece(FILL); } }
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ [] This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ { "lines": [ 72, 58, 0, 0, 0, 0, 0, 0 ] } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ {} This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ {} This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ {}