{"id":16125500,"url":"https://github.com/semibran/tetrion","last_synced_at":"2025-04-02T10:25:46.292Z","repository":{"id":130492160,"uuid":"81764905","full_name":"semibran/tetrion","owner":"semibran","description":":hammer_and_wrench: Tetris game engine","archived":false,"fork":false,"pushed_at":"2017-06-29T18:30:34.000Z","size":21,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-18T14:53:55.328Z","etag":null,"topics":["engine","game","tetris"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/semibran.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-02-12T23:10:52.000Z","updated_at":"2024-02-11T13:42:29.000Z","dependencies_parsed_at":null,"dependency_job_id":"7dad6d11-837d-46b6-a3b9-bc8084ca1f0e","html_url":"https://github.com/semibran/tetrion","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/semibran%2Ftetrion","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/semibran%2Ftetrion/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/semibran%2Ftetrion/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/semibran%2Ftetrion/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/semibran","download_url":"https://codeload.github.com/semibran/tetrion/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246795787,"owners_count":20835269,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["engine","game","tetris"],"created_at":"2024-10-09T21:29:50.556Z","updated_at":"2025-04-02T10:25:46.267Z","avatar_url":"https://github.com/semibran.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# tetrion\nThis module contains a fully-featured set of utility functions for designing [Tetris](https://en.wikipedia.org/wiki/Tetris) clones.\n\n## usage\n[![NPM](https://nodei.co/npm/tetrion.png?mini)](https://www.npmjs.com/package/tetrion)\n\nComplex as it may seem, a game of Tetris can be represented as a single data structure.\n```js\nvar game = {\n  over: false,\n  gravity: 1 / 8,\n  steps: 0,\n  piece: {\n    type: 'J',\n    position: [3, 0],\n    rotation: 0\n  },\n  matrix: {\n    size: [10, 22],\n    pieces: [[[8, 20], [7, 21], [8, 21], [9, 21]]]\n  }\n}\n```\n- `over`: whether or not the player has [\"topped out\"](http://tetris.wikia.com/wiki/Top_out)\n- `gravity`: the rate at which pieces fall; added to `game.steps` every `update`\n- `steps`: the number of cells `game.piece` will move down on the next update; note that pieces can only move by whole cells\n- `piece`: the current [tetromino](http://tetris.wikia.com/wiki/Tetromino) being dropped\n  - `type`: a single character denoting the tetromino type; can be one of `['I', 'J', 'L', 'O', 'S', 'T', 'Z']`\n  - `position`: a vector (`Array`) of the form `[x, y]` indicating the tetromino's position\n  - `rotation`: a rotation index corresponding to a tetromino [rotation state](http://tetris.wikia.com/wiki/SRS)\n- `matrix`: the [playfield](http://tetris.wikia.com/wiki/Playfield); the grid into which tetrominos fall\n  - `size`: a vector of the form `[width, height]` indicating the boundaries of the matrix\n  - `pieces`: a list of the pieces inside the matrix. Note that there is a clear distinction between pieces in this array and `game.piece` - these ones are just lists of `[x, y]` pairs denoting the location of each block of a piece, as generated by [`manifest`](#piecemanifestpiece).\n\nWe can modify the game state using utility functions called \"actions\". Let's begin a tour of each action by first defining the structure of this module:\n```js\n\u003e require('tetrion')\n{\n  game: {\n    actions: {\n      move: [Function: move],\n      rotate: [Function: rotate],\n      update: [Function: update]\n    }\n  },\n  matrix: {\n    actions: {\n      clear: [Function: clear]\n    },\n    contains: [Function: contains],\n    occupant: [Function: occupant]\n  },\n  piece: {\n    actions: {\n      move: [Function: move],\n      rotate: [Function: rotate]\n    },\n    manifest: [Function: manifest],\n    states: {\n      I: [Array],\n      J: [Array],\n      L: [Array],\n      O: [Array],\n      S: [Array],\n      T: [Array],\n      Z: [Array]\n    },\n    types: ['I', 'J', 'L', 'O', 'S', 'T', 'Z']\n  }\n}\n```\nNotice how closely this data structure matches the actual structure of its corresponding file tree. As a result, we can import certain parts of this tree individually as necessary:\n```js\n\u003e require('tetrion/game/actions/update')\n[Function: update]\n\n\u003e const { move, rotate } = require('tetrion/piece/actions')\n```\nWith that in mind, let's go over each element of this tree in detail.\n\n## API\n\n### `game.actions.move(game, direction)`\nMoves `game.piece` in `direction` (`'left'`, `'right'`, or `'down'`) and returns `true` if successful, otherwise `false`.\n\n### `game.actions.rotate(game, direction)`\nRotates `game.piece` in `direction` (`'left'` or `'right'`) and returns `true` if successful, otherwise `false`.\n\n### `game.actions.update(game)`\nUpdates the game state by handling gravity, line clearing, piece spawning, and top outs.\n\n### `matrix.actions.clear(matrix, line?)`\nClears all blocks at the `y`-position denoted by `line`, or the entirety of `matrix` if not provided.\n\n### `matrix.contains(matrix, cell)`\nDetermines whether or not the given `[x, y]` pair lies inside `matrix`.\n```js\n\u003e matrix = {\n    size: [10, 22],\n    pieces: []\n  }\n\n\u003e contains(matrix, [5, 11])\ntrue\n\n\u003e contains(matrix, [10, 22])\nfalse\n```\n\n### `matrix.occupant(matrix, cell)`\nFinds the piece (list of `[x, y]` pairs) occupying the given `cell` inside `matrix` if it exists, otherwise `undefined`.\n\n### `piece.actions.move(piece, delta, matrix)`\nMoves the specified `piece` by `delta` and returns `true` if successful, otherwise `false`.\n\n### `piece.actions.rotate(piece, delta, matrix)`\nRotates the specified `piece` by `delta * 90deg` and returns `true` if successful, otherwise `false`.\n\n### `piece.manifest(piece)`\nFinds all the cells occupied by `piece` and returns them as a list of `[x, y]` pairs.\n```js\n\u003e var piece = {\n    type: 'J',\n    position: [7, 20],\n    rotation: 0\n  }\n\n\u003e manifest(piece)\n[[8, 20], [7, 21], [8, 21], [9, 21]]\n```\n\n### `piece.states[type][rotation]`\nThe locations of each cell of a tetromino with a given type and rotation, listed in compliance with the [SRS](http://tetris.wikia.com/wiki/SRS).\n```js\n\u003e piece.states.J[0]\n[[0, 1], [1, 1], [2, 1], [2, 0]]\n```\n\n### `piece.types[7]`\nThe list of valid tetromino types. Especially useful for determining what kind of tetromino to spawn next.\n```js\n\u003e types = require('tetrion/piece/types')\n['I', 'J', 'L', 'O', 'S', 'T', 'Z']\n\n\u003e types[Math.floor(Math.random() * types.length)]\n'S'\n```\n\n## see also\n- [`semibran/tetromino`](https://github.com/semibran/tetromino) - `tetrion/piece` implemented minimally as a separate module\n\n## license\n[MIT](https://opensource.org/licenses/MIT) © [Brandon Semilla](https://git.io/semibran)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsemibran%2Ftetrion","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsemibran%2Ftetrion","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsemibran%2Ftetrion/lists"}