Skip to content

Instantly share code, notes, and snippets.

@unstoppablecarl
Last active March 6, 2019 15:40
Show Gist options
  • Select an option

  • Save unstoppablecarl/2dc6f7f9f5402e1f657f3c1106b54ce0 to your computer and use it in GitHub Desktop.

Select an option

Save unstoppablecarl/2dc6f7f9f5402e1f657f3c1106b54ce0 to your computer and use it in GitHub Desktop.

Revisions

  1. unstoppablecarl revised this gist Mar 6, 2019. 3 changed files with 62 additions and 35 deletions.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    This is a [SCRIPT-8](https://script-8.github.io) cassette.
    This is a [SCRIPT-8](https://script-8.github.io) cassette. Click [here](https://script-8.github.io/?id=2dc6f7f9f5402e1f657f3c1106b54ce0) to boot it.
    91 changes: 59 additions & 32 deletions code.js
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,9 @@
    const ROW = 10;
    const COL = 10;
    const SQ = 7;
    const VACANT = 0;
    const FILL = 1;
    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: {
    x: 4,
    y: 0,
    shape: [
    [0,1,0],
    [1,1,1]
    ]
    }
    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
    }
    if(type == FILL){
    color = 2;
    rectFill(x, y, width, height, color)
    }

    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(piece.y == 9) {
    return;
    }
    if(!hasCollision()){

    if(hasCollision()){
    state.piece = makePiece();
    }
    else{
    movePiece();
    }

    }
    }

    function hasCollision(){

    if(piece.y == 9) {
    return true;;
    }
    let collision = false;
    eachPieceCoord((x, y, val) => {
    if(board[x][y + 1] == FILL){
    collision = true;
    }
    eachCollisionCoord((x, y) => {
    if(board[x][y] !== VACANT){
    collision = true;
    }
    });

    return collision;
    }

    function eachPieceCoord(callback){
    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){
    eachPieceCoord((x, y, val) => {
    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(FILL);
    fillPiece(piece.color);
    }
    }





    }
    4 changes: 2 additions & 2 deletions misc.json
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    {
    "lines": [
    72,
    58,
    74,
    83,
    0,
    0,
    0,
  2. unstoppablecarl created this gist Mar 6, 2019.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    This is a [SCRIPT-8](https://script-8.github.io) cassette.
    1 change: 1 addition & 0 deletions chains.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    {}
    130 changes: 130 additions & 0 deletions code.js
    Original 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);
    }
    }





    1 change: 1 addition & 0 deletions map.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    []
    12 changes: 12 additions & 0 deletions misc.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    {
    "lines": [
    72,
    58,
    0,
    0,
    0,
    0,
    0,
    0
    ]
    }
    1 change: 1 addition & 0 deletions phrases.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    {}
    1 change: 1 addition & 0 deletions songs.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    {}
    1 change: 1 addition & 0 deletions sprites.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    {}