npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

terminal-game-io

v3.1.0

Published

It has never been easier to start writing ASCII games in NodeJs or browser. This package handles for you basic input (keyboard events) and output (ASCII 'frame').

Downloads

87

Readme

Terminal Game IO

npm version Build Status Coverage Status dependencies Status devDependencies Status

It has never been easier to start writing ASCII games in NodeJs or browser. This package handles for you basic input (keyboard events) and output (ASCII 'frame').

NodeJs terminal example:

Terminal example

Browser example:

Terminal example

You can check the browser example here

Interactive code examples:

  • Clean JavaScript simplest example here
  • Clean JavaScript full example with mobile device support here
  • Angular 6 example here

Project that uses terminal-game-io lib:

Installation

npm install terminal-game-io

Changelog

Still TODO

  • small refactor of dev/demo files structure as currently it's not clear which file is used in particular development mode
  • render only part that really changed (more complex performance fix)
  • fix ssh connection double keypress bug
  • think about prevent default of keyboard event
  • use requestAnimationFrame
  • probably instance parameter is not required at keypressHandler and frameHandler - I don't remember why I added it ;)
  • write unit tests

v3.1.0 - 03 November 2018

  • [bugfix] blank first frame after initialization
  • render the frame only if it's different than previous (easy performance fix)
  • remove WrapperWebpackPlugin

v3.0.0 - 30 September 2018

  • remove externals ('process' and 'readline')
  • split main class into two (one for node, one for browser)
  • normalize keyNames, currently there is a mismatch between node and browser
  • remove write method
  • fix mobile buttons on demo apps
  • add keyName constants

v1.x.x/v2.x.x - August/September 2018

  • create project core
  • export env utils (isBrowser, isNode)
  • add ability to change DOM element id
  • add ability to trigger keypress handler from other sources (mouse click, swipe event)
  • fix error with node environment detection

Simplest example - NodeJs, clean JavaScript

Follow the installation instruction and create test.js file with the content below. At the end execute the node test.js command.

const TerminalGameIo = require('terminal-game-io');

const Key = TerminalGameIo.Key;

const FPS = 1;
const BOARD_WIDTH = 40;
const BOARD_HEIGHT = 12;

let terminalGameIo;
let posX = Math.round(BOARD_WIDTH / 2);
let posY = Math.round(BOARD_HEIGHT / 2);

const frameHandler = (instance) => {
  let frameData = '';

  for (let y = 0; y < BOARD_HEIGHT; y++) {
    for (let x = 0; x < BOARD_WIDTH; x++) {
      frameData += (posX === x && posY === y) ? '@' : '.';
    }
  }
  instance.drawFrame(frameData, BOARD_WIDTH, BOARD_HEIGHT);
};

const keypressHandler = (instance, keyName) => {
  switch (keyName) {
    case Key.ArrowDown:
      posY = (posY + 1) % BOARD_HEIGHT;
      break;
    case Key.ArrowUp:
      posY = posY === 0 ? BOARD_HEIGHT - 1 : posY - 1;
      break;
    case Key.ArrowLeft:
      posX = posX === 0 ? BOARD_WIDTH - 1 : posX - 1;
      break;
    case Key.ArrowRight:
      posX = (posX + 1) % BOARD_WIDTH;
      break;
    case Key.Escape:
      instance.exit();
      break;
  }

  frameHandler(instance);
};

terminalGameIo = TerminalGameIo.createTerminalGameIo({
  fps: FPS,
  frameHandler,
  keypressHandler
});

Use Arrows to move and Escape to exit. For more examples see section at very top of this readme.

Simplest example - browser, clean JavaScript

Running in browser is also easy. Just create index.html with the content below.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Terminal Game IO</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body onLoad="run()">
  <pre id="root"></pre>

  <script>
    var FPS = 1;
    var BOARD_WIDTH = 40;
    var BOARD_HEIGHT = 12;

    var terminalGameIo;
    var posX = Math.round(BOARD_WIDTH / 2);
    var posY = Math.round(BOARD_HEIGHT / 2);

    function frameHandler(instance) {
      var frameData = '';

      for (var y = 0; y < BOARD_HEIGHT; y++) {
        for (var x = 0; x < BOARD_WIDTH; x++) {
          frameData += (posX === x && posY === y) ? '@' : '.';
        }
      }
      instance.drawFrame(frameData, BOARD_WIDTH, BOARD_HEIGHT);
    }

    function keypressHandler(instance, keyName) {
      frameHandler(instance);

      switch (keyName) {
        case TerminalGameIo.Key.ArrowDown:
          posY = (posY + 1) % BOARD_HEIGHT;
          break;
        case TerminalGameIo.Key.ArrowUp:
          posY = posY === 0 ? BOARD_HEIGHT - 1 : posY - 1;
          break;
        case TerminalGameIo.Key.ArrowLeft:
          posX = posX === 0 ? BOARD_WIDTH - 1 : posX - 1;
          break;
        case TerminalGameIo.Key.ArrowRight:
          posX = (posX + 1) % BOARD_WIDTH;
          break;
        case TerminalGameIo.Key.Escape:
          instance.exit();
          break;
      }
    }

    function run() {
      terminalGameIo = TerminalGameIo.createTerminalGameIo({
        // domElementId: 'my-custom-id',     // default: 'root'
        fps: FPS,
        frameHandler: frameHandler,
        keypressHandler: keypressHandler
      });
    }
  </script>
  <script src="https://unpkg.com/terminal-game-io"></script>
</html>

Similar as in NodeJs example you can use Arrows to move and Escape to stop. For more examples see section at very top of this readme.

Want to check this project in development mode?

git clone https://github.com/robertrypula/terminal-game-io.git
cd terminal-game-io
npm install

npm run dev-node

This library can run also in the browser:

npm run dev-browser

Licence

The MIT License (MIT)

Copyright (c) 2018 Robert Rypuła - https://github.com/robertrypula/terminal-game-io

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.