0.1.2 • Published 5 years ago

tetris-core v0.1.2

Weekly downloads
2
License
ISC
Repository
github
Last release
5 years ago

tetris-core npm

A tetris module only with core data layer.

Installing

$ npm install tetris-core

Demo (using hyperapp)

$ git clone https://github.com/goldenthumb/tetris-core.git
$ cd tetris-core
$ npm install
$ npm run dev

Now open this URL in your browser: http://localhost:3000/

Usage

import Tetris from 'tetris-core';

const tetris = new Tetris();

tetris.on('render', data => {
  const { stage, score, speed, nextBlock, display } = data;
  // stage ==> number
  // score ==> number
  // speed ==> number
  // nextBlock ==> two dimensional array (0 or block hex color code)
  // display ==> two dimensional array (0 or block hex color code)
});

tetris.on('end', () => {
  // game over
});

tetris.start();

Options

// default options
const options = {
  display: {
    rows: 20, 
    cols: 11,
    startPoint: { x: 4, y: -1 },
  },
  stage: {
    maxStage: 10,
    speed: { min: 500, max: 100 },
    scoreRate: 40
  },
  blocks: [
    {
      color: '#00a9eb',
      types: {
        0: [
          [1, 1, 1, 1]
        ],
        1: [
          [1],
          [1],
          [1],
          [1]
        ],
        2: [
          [1, 1, 1, 1]
        ],
        3: [
          [1],
          [1],
          [1],
          [1]
        ]
      }
    },
    {
      color: '#fedf38',
      types: {
        0: [
          [0, 1, 0],
          [1, 1, 1]
        ],
        1: [
          [1, 0],
          [1, 1],
          [1, 0]
        ],
        2: [
          [1, 1, 1],
          [0, 1, 0]
        ],
        3: [
          [0, 1],
          [1, 1],
          [0, 1]
        ]
      }
    },
    {
      color: '#88bf39',
      types: {
        0: [
          [1, 0, 0],
          [1, 1, 1]
        ],
        1: [
          [1, 1],
          [1, 0],
          [1, 0]
        ],
        2: [
          [1, 1, 1],
          [0, 0, 1]
        ],
        3: [
          [0, 1],
          [0, 1],
          [1, 1]
        ]
      }
    },
    {
      color: '#fd2239',
      types: {
        0: [
          [0, 0, 1],
          [1, 1, 1]
        ],
        1: [
          [1, 0],
          [1, 0],
          [1, 1]
        ],
        2: [
          [1, 1, 1],
          [1, 0, 0]
        ],
        3: [
          [1, 1],
          [0, 1],
          [0, 1]
        ]
      }
    },
    {
      color: '#4bc6b0',
      types: {
        0: [
          [0, 1, 1],
          [1, 1, 0]
        ],
        1: [
          [1, 0],
          [1, 1],
          [0, 1]
        ],
        2: [
          [0, 1, 1],
          [1, 1, 0]
        ],
        3: [
          [1, 0],
          [1, 1],
          [0, 1]
        ]
      }
    },
    {
      color: '#c655e7',
      types: {
        0: [
          [1, 1, 0],
          [0, 1, 1]
        ],
        1: [
          [0, 1],
          [1, 1],
          [1, 0]
        ],
        2: [
          [1, 1, 0],
          [0, 1, 1]
        ],
        3: [
          [0, 1],
          [1, 1],
          [1, 0]
        ]
      }
    },
    {
      color: '#ec7e48',
      types: {
        0: [
          [1, 1],
          [1, 1]
        ]
      }
    }
  ]
};

const tetris = new Tetris(options);

Events

NameDescription
rendertriggers when game need repaint
endtriggers when game over

Methods

NameDescription
startstart the game
getStaterender data (stage, score, speed, nextBlock, display)
moveDownmove the block down
moveLeftmove the block left
moveRightmove the block right
rotaterotate the block

License

MIT