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:
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:
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.
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:
npm install simple-tetris
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>
It works in terminal as well:
npm install simple-tetris && node node_modules/simple-tetris/dist/ascii-runner-node.js
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.
terminal-game-io
from typescript definitions of src/lib/ascii-runner.ts file and move this lib back to devDeps (breaking change, probably in 2.0.0)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
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.