robertrypula / simple-tetris

This package provides API that allows you to easily create clones of the Tetris game. It handles game core - your role is to write the UI. Library was inspired by Redux and was written from scratch in TypeScript.
MIT License
13 stars 3 forks source link
ascii-tetris game nodejs redux-tetris simple-game simple-tetris terminal-game terminal-tetris tetrimino tetriminos tetris tetris-core tetris-game tetris-game-core tetris-logic tetris-typescript text-game text-tetris

Simple Tetris

Update from 2023.02.08 - standalone WORKING even simpler tetris

Initial project (Redux-like & TypeScript) started in 2018 and it was never finished as I lost interest in it. In the meantime I wanted to create yet another Tetris version that is totally platform independent & programming language independent. Somewhere in 2022 this finally happen but I never published it... until now! Below you will find link to fully working game:

Play online

Source Code

Check the source code of that HTML. Everything is inside one file.

Few highlights:

If you will find the code usefull please Star the project on GitHub - this really gives me a kick for making more stuff like that. Thanks in advance! :)

Below you will find old docs for the Redux-like & TypeScript version that was never finished:

Unfinished project (development stopped 2018.12.02)

npm version Build Status Coverage Status dependencies Status devDependencies Status

This package provides API that allows you to easily create clones of the Tetris game. It handles game core - your role is to write the UI. Library was inspired by Redux and was written from scratch in TypeScript.

NOTE: This project is still not finished. More details in the TODO section below.

Play online - use WSAD keys on Desktop or on-screen buttons on Mobile devices.

Api client browser

Simplest code example:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Simple Tetris - Simplest example</title>
  <style>
    #game-root {
      height: 480px;
      margin-bottom: 8px;
      width: 240px;
    }
    #game-root > div {
      background-color: lightgray;
      border: 1px solid white;
      box-sizing: border-box;
      float: left;
      height: 24px;
      width: 24px;
    }
    #game-root > div.filled { background-color: gray; }
  </style>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body onLoad="run()">
  <div id="game-root"></div>

  <button onClick="handleEvent(SimpleTetris.KEY_CODE_LEFT);">left</button>
  <button onClick="handleEvent(SimpleTetris.KEY_CODE_RIGHT);">right</button>
  <button onClick="handleEvent(SimpleTetris.KEY_CODE_HARD_DROP);">down</button>
  <button onClick="handleEvent(SimpleTetris.KEY_CODE_ROTATE);">rotate</button>

  <script>
    var store;

    function handleEvent(keyCode) {
      SimpleTetris.gameLoopIteration(store, 0.0, keyCode);
      render();
    }

    function render() {
      var matrixBlocksToRender = SimpleTetris.matrixBlocksToRenderSelector(store.getState());
      var html = '';

      for (var i = 0; i < matrixBlocksToRender.length; i++) {
        html += matrixBlocksToRender[i] ? '<div class="filled"></div>' : '<div></div>';
      }
      document.getElementById('game-root').innerHTML = html;
    }

    function run() {
      store = new SimpleTetris.createStore();
      render();
    }
  </script>
  <script src="https://unpkg.com/simple-tetris"></script>
</body>
</html>

Interactive code examples:

Installation

npm install simple-tetris

Ascii Runner alternative

If you just want to play the game and don't want to use any API you can simply use built-in Ascii Runner.

Play online - use Arrows on Desktop or on-screen buttons on Mobile devices.

<pre id="root"></pre>
<script src="https://unpkg.com/simple-tetris"></script>
<script>
  var asciiRunner = new SimpleTetris.AsciiRunner();
</script>

Ascii Runner browser

It works in terminal as well:

npm install simple-tetris && node node_modules/simple-tetris/dist/ascii-runner-node.js

Ascii Runner node

Content of the ascii-runner-node.js is as simple as:

const SimpleTetris = require('simple-tetris');
const asciiRunner = new SimpleTetris.AsciiRunner();

ASCII runner uses my other library (terminal-game-io) that simplifies basic input and output of the text based games.

Changelog

Still TODO

1.3.0 - ?? ?? 2018

1.2.0 - 25 November 2018

1.1.1 - 18 November 2018

1.1.0 - 13 September 2018

1.0.0 - 06 August 2018

Want to check this project in development mode?

Development mode is using Ascii Runner supported by terminal-game-io library.

git clone https://github.com/robertrypula/simple-tetris.git
cd simple-tetris
npm install

npm run dev-browser

You can develop and play Tetris directly in the terminal. It works even via the SSH connections.

npm run dev-node

Licence

The MIT License (MIT)

Copyright (c) 2018 Robert Rypuła - https://github.com/robertrypula/simple-tetris

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.