# Platform Jumper Game

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

This recipe demonstrates RxJS implementation of Platform Jumper game.

### Example Code

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

![Platform Jumper](https://drive.google.com/uc?export=view\&id=1poxaFwTtypxoTzOU13KcvE5fIcnzXog-)

#### index.ts

```js
// RxJS v6+
import { interval, of, fromEvent, combineLatest } from 'rxjs';
import { scan, tap, startWith, pluck, switchMap, takeWhile } from 'rxjs/operators';
import { gameSize } from './constants';
import { render } from './html-renderer';
import { Player, Platform } from './interfaces';
import { updatePlayer, updatePlatforms, initialPlatforms, initialPlayer, handleCollisions, handleKeypresses } from './game';

const gameSpeed = 500;

const platforms$ = interval(gameSpeed)
  .pipe(
    scan<number, Platform[]>(updatePlatforms, initialPlatforms)
  );

const keys$ = (initialPlayer: Player) => fromEvent(document, 'keydown')
  .pipe(
    startWith({ key: '' }),
    pluck('key'),
    scan<string, Player>(
      (plyr: Player, key: string) => handleKeypresses(plyr, key),
      initialPlayer
    )
  );

const player$ = of(initialPlayer())
  .pipe(
    switchMap(p => combineLatest(interval(gameSpeed / 4), keys$(p))
      .pipe(
        scan<[number, Player], Player>((_, [__, player]) => updatePlayer(player))
      )),
  );

combineLatest(player$, platforms$)
  .pipe(
    scan<[Player, Platform[]], [Player, Platform[]]>(
      (_, [player, platforms]) => handleCollisions([player, platforms])),
    tap(render),
    takeWhile(([player, platforms]) => player.lives > 0)
  )
  .subscribe()
```

#### game.ts

```js
import { Player, Platform } from './interfaces';
import { gameSize } from './constants';

const newPlatform = (x, y): Platform => ({ x, y, scored: false });
const newPlayer = (x, y, jumpValue, score, lives): Player => ({
  x,
  y,
  jumpValue,
  canJump: false,
  score: score,
  lives: lives
});
const startingY = 4;
export const initialPlayer = (): Player => newPlayer(0, startingY, 0, 0, 3);
export const initialPlatforms = [newPlatform(gameSize / 2, startingY)];

const random = y => {
  let min = Math.ceil(y - 4);
  let max = Math.floor(y + 4);
  min = min < 0 ? 0 : min;
  max = max > gameSize - 1 ? gameSize - 1 : max;

  return Math.floor(Math.random() * (max - min + 1)) + min;
};

export const updatePlatforms = (platforms: Platform[]): Platform[] => (
  platforms[platforms.length - 1].x > gameSize / 5
    ? platforms.push(newPlatform(1, random(platforms[platforms.length - 1].y)))
    : () => {},
  platforms.filter(e => e.x < gameSize - 1).map(e => newPlatform(e.x + 1, e.y))
);

export const handleKeypresses = (player: Player, key: string) =>
  key === 'ArrowRight'
    ? newPlayer(
        player.x,
        player.y + (player.y < gameSize - 1 ? 1 : 0),
        player.jumpValue,
        player.score,
        player.lives
      )
    : key === 'ArrowLeft'
    ? newPlayer(
        player.x,
        player.y - (player.y > 0 ? 1 : 0),
        player.jumpValue,
        player.score,
        player.lives
      )
    : key === 'ArrowUp'
    ? newPlayer(
        player.x,
        player.y,
        player.x === gameSize - 1 || player.canJump ? 6 : 0,
        player.score,
        player.lives
      )
    : player;

export const updatePlayer = (player: Player): Player => (
  (player.jumpValue -= player.jumpValue > 0 ? 1 : 0),
  (player.x -= player.x - 3 > 0 ? player.jumpValue : 0),
  (player.x += player.x < gameSize - 1 ? 1 : 0),
  player.x === gameSize - 1 ? ((player.lives -= 1), (player.x = 1)) : () => {},
  player
);

const handleCollidingPlatform = (
  collidingPlatform: Platform,
  player: Player
) => {
  if (player.canJump) {
    return;
  }

  if (!collidingPlatform) {
    player.canJump = false;
    return;
  }

  if (!collidingPlatform.scored) {
    player.score += 1;
  }
  collidingPlatform.scored = true;

  player.canJump = true;
};

export const handleCollisions = ([player, platforms]: [Player, Platform[]]): [
  Player,
  Platform[]
] => (
  handleCollidingPlatform(
    platforms.find(p => p.x - 1 === player.x && p.y === player.y),
    player
  ),
  (player.x = player.canJump
    ? (collidingPlatforms =>
        collidingPlatforms.length
          ? (platform => platform.x - 1)(
              collidingPlatforms[collidingPlatforms.length - 1]
            )
          : player.x)(
        platforms.filter(p => p.y === player.y && p.x >= player.x)
      )
    : player.x),
  [player, platforms]
);
```

#### interfaces.ts

```js
export interface Player {
  x: number;
  y: number;
  jumpValue: number;
  canJump: boolean;
  score: number;
  lives: number;
}

export interface Platform {
  x: number;
  y: number;
  scored: boolean;
}
```

#### constants.ts

```js
export const gameSize = 20;
```

#### html-renderer.ts

```js
import { gameSize } from './constants';
import { Player, Platform } from './interfaces';

export const render = ([player, platforms]: [Player, Platform[]]) => {
  document.body.innerHTML = `Lives: ${player.lives} Score: ${player.score} </br>`;

  const game = Array(gameSize)
    .fill(0)
    .map(_ => Array(gameSize).fill(0));
  game[player.x][player.y] = '*';
  platforms.forEach(p => (game[p.x][p.y] = '_'));

  game.forEach(r => {
    r.forEach(c => (document.body.innerHTML += c === 0 ? '...' : c));
    document.body.innerHTML += '<br/>';
  });
};
```

### Operators Used

* [combineLatest](/learn-rxjs/operators/combination/combinelatest.md)
* [fromEvent](/learn-rxjs/operators/creation/fromevent.md)
* [interval](/learn-rxjs/operators/creation/interval.md)
* [of](/learn-rxjs/operators/creation/of.md)
* [pluck](/learn-rxjs/operators/transformation/pluck.md)
* [scan](/learn-rxjs/operators/transformation/scan.md)
* [startWith](/learn-rxjs/operators/combination/startwith.md)
* [switchMap](/learn-rxjs/operators/transformation/switchmap.md)
* [takeWhile](/learn-rxjs/operators/filtering/takewhile.md)
* [tap](/learn-rxjs/operators/utility/do.md)


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.learnrxjs.io/learn-rxjs/recipes/platform-jumper-game.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
