> For the complete documentation index, see [llms.txt](https://www.learnrxjs.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.learnrxjs.io/learn-rxjs/recipes/tank-battle-game.md).

# Tank Battle Game

*By* [*adamlubek*](https://github.com/adamlubek)

This recipe demonstrates RxJS implementation of Tank Battle like game.

### Example Code

( [StackBlitz](https://stackblitz.com/edit/rxjs-tank-battle?file=index.ts) )

![Tank Battle](https://drive.google.com/uc?export=view\&id=1-QeeMZCtd9rp60m9C9yphMW5vs0jQxYW)

#### index.ts

```js
// RxJS v6+
import { fromEvent, combineLatest, interval } from 'rxjs';
import { scan, tap, startWith } from 'rxjs/operators';
import { gameSize, down, up, right, left, p1Color, p2Color } from './constants';
import { State } from './interfaces';
import {
  updatePlayer,
  addShots,
  updateShots,
  checkCollisions,
  initialState
} from './game';
import { paint } from './html-renderer';

combineLatest(
  interval(100),
  fromEvent(document, 'keydown').pipe(startWith({ key: '' }))
)
  .pipe(
    scan < [number, KeyboardEvent],
    State >
      ((state, [_, event]) => (
        updatePlayer(state.players[0], event.key, 'w', 's', 'a', 'd'),
        updatePlayer(state.players[1], event.key, 'i', 'k', 'j', 'l'),
        addShots(state, event.key),
        (state.shots = updateShots(state.shots)),
        checkCollisions(state),
        state
      ),
      initialState),
    tap(paint)
  )
  .subscribe();
```

#### game.ts

```js
import {
  gameSize,
  down,
  up,
  right,
  left,
  p1Color,
  p2Color,
  p1Shot,
  p2Shot
} from './constants';
import { GameObject, State } from './interfaces';

const gameObject = (x, y, g, c): GameObject => ({ x, y, g, s: 0, c: c });
const noop = (): void => {};

export const updatePlayer = (
  p: GameObject,
  key: string,
  u,
  d,
  l,
  r
): GameObject => (
  key === d
    ? ((p.x += p.x < gameSize - 1 ? 1 : 0), (p.g = down))
    : key === u
    ? ((p.x += p.x > 0 ? -1 : 0), (p.g = up))
    : noop,
  key === r
    ? ((p.y += p.y < gameSize - 1 ? 1 : 0), (p.g = right))
    : key === l
    ? ((p.y += p.y > 0 ? -1 : 0), (p.g = left))
    : noop,
  p
);

export const addShot = (player: GameObject): GameObject => ({
  x: player.x,
  y: player.y,
  g: player.g
});

export const addShots = (state: State, key: string): void =>
  state.shots.push(
    key === p1Shot
      ? addShot(state.players[0])
      : key === p2Shot
      ? addShot(state.players[1])
      : []
  );

export const updateShots = (shots: GameObject[]): GameObject[] =>
  shots
    .filter(s => s.x > 0 && s.x < gameSize - 1 && s.y > 0 && s.y < gameSize)
    .map(
      s => (
        s.g === down
          ? (s.x += 1)
          : s.g === up
          ? (s.x += -1)
          : s.g === right
          ? (s.y += 1)
          : s.g === left
          ? (s.y += -1)
          : () => {},
        s
      )
    );

export const initialState: State = {
  players: [
    gameObject(1, 1, right, p1Color),
    gameObject(gameSize - 2, gameSize - 2, left, p2Color)
  ],
  shots: []
};

export const checkCollisions = (state: State): void =>
  state.players.forEach((p, i) => {
    const collidingShotIndex = state.shots.findIndex(
      s => s.x === p.x && s.y === p.y
    );
    if (collidingShotIndex > -1) {
      if (i === 0) {
        state.players[1].s += 1;
      } else {
        state.players[0].s += 1;
      }
      state.shots.splice(collidingShotIndex, 1);
    }
  });
```

#### interfaces.ts

```js
export interface GameObject {
  x: number;
  y: number;
  g: string;
  s: number;
  c: string;
}

export interface State {
  players: GameObject[];
  shots: GameObject[];
}
```

#### constants.ts

```js
export const gameSize = 20;
export const up = '^';
export const down = 'v';
export const left = '<';
export const right = '>';
export const empty = 0;
export const p1Color = 'DarkViolet';
export const p2Color = 'CornflowerBlue';
export const p1Shot = 'c';
export const p2Shot = 'n';
```

#### html-renderer.ts

```js
import { gameSize, empty, p1Color, p2Color } from './constants';
import { State, GameObject } from './interfaces';

const createElem = (gameObject: GameObject) => {
  const elem = document.createElement('div');
  elem.style.display = 'inline-block';
  elem.style.marginLeft = '10px';
  elem.style.height = '6px';
  elem.style.width = '6px';
  elem.style.color = gameObject.c;
  elem.innerText = gameObject === empty ? ' ' : gameObject.g;

  return elem;
};

const paintPlayerScore = (score: number, color: string) => {
  const scoreElem = document.createElement('span');
  scoreElem.innerHTML = `P1: ${score} `;
  scoreElem.style.color = color;
  document.body.appendChild(scoreElem);
};

const paintScores = (state: State) => {
  document.body.innerHTML = 'Scores: ';
  paintPlayerScore(state.players[0].s, p1Color);
  paintPlayerScore(state.players[1].s, p2Color);
};

const painInfo = () => {
  document.body.innerHTML += 'This game requires 2 players :)';
  document.body.innerHTML += '<br/>';
  document.body.innerHTML += 'Player 1 controls: wsad, fire: c';
  document.body.innerHTML += '<br/>';
  document.body.innerHTML += 'Player 2 controls: ikjl, fire: n';
};

const emptyGame = () =>
  Array(gameSize)
    .fill(empty)
    .map(_ => Array(gameSize).fill(empty));
const paintGame = (state: State) => {
  const game = emptyGame();
  state.players.forEach(p => (game[p.x][p.y] = { g: p.g, c: p.c }));
  state.shots.forEach(s => (game[s.x][s.y] = { g: '*', c: 'black' }));

  game.forEach(row => {
    const rowContainer = document.createElement('div');
    row.forEach(col => rowContainer.appendChild(createElem(col)));
    document.body.appendChild(rowContainer);
  });
};

export const paint = (state: State) => {
  paintScores(state);
  document.body.innerHTML += '<br/>';
  paintGame(state);
  painInfo();
};
```

### Operators Used

* [combineLatest](/learn-rxjs/operators/combination/combinelatest.md)
* [fromEvent](/learn-rxjs/operators/creation/fromevent.md)
* [interval](/learn-rxjs/operators/creation/interval.md)
* [scan](/learn-rxjs/operators/transformation/scan.md)
* [startWith](/learn-rxjs/operators/combination/startwith.md)
* [tap](/learn-rxjs/operators/utility/do.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.learnrxjs.io/learn-rxjs/recipes/tank-battle-game.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
