@pbuk/game-loop
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

game-loop

A game loop for browser.

Quick start

Include game-loop from CDN.

<script src="https://cdn.jsdelivr.net/npm/@pbuk/game-loop@1"></script>

Create the callbacks that define the game.

const config = {
  // Things that stay the same.
};

const getInitialState = (config) => {
  return {
    // Things that change.
  };
};

const update = ({ config, state, frameLength, runTime, gameState }) => {
  // Update state (don't change anything else).
};

const render = ({ config, state, frameLength, runTime, gameState }) => {
  // Update all displays (don't change anything).
};

Create and manage the game loop.

// Set up the game and call `update` and `render` once with gameState = 'notStarted'.
const { start, stop, reset } = GameLoop.createGame({ config, getInitialState, update, render });

// Starts the game running, or resumes after `stop()`.
start();
// Stops the game (can be resumed with `start()`).
stop();
// Stops the game and resets the state to the initial state.
reset();

/@pbuk/game-loop/

    Package Sidebar

    Install

    npm i @pbuk/game-loop

    Weekly Downloads

    0

    Version

    1.0.0

    License

    MIT

    Unpacked Size

    16.5 kB

    Total Files

    9

    Last publish

    Collaborators

    • pbuk