Skip to content

Instantly share code, notes, and snippets.

@epidemian
Created March 4, 2020 04:01
Show Gist options
  • Save epidemian/1e7ba677d46807598297e45d064abcb6 to your computer and use it in GitHub Desktop.
Save epidemian/1e7ba677d46807598297e45d064abcb6 to your computer and use it in GitHub Desktop.
asd

The gameplay gif you can see on https://github.com/epidemian/snake is a lie. It was not played by a person, but recorded move-by-move.

The code on record-moves.js was used to first record "player" moves one by one and then the code on replay-moves.js was used to replay them on the game.

The readme-moves.json file contains the

// Moves per tick.
var moves = [[]]
// Override tickTime so game never advances on its own.
window.tickTime = () => Number.MAX_VALUE
// Advance world with space bar.
document.addEventListener('keydown', (e) => {
if (e.keyCode === 32) {
updateWorld()
drawWorld()
moves.push([])
}
})
// Override changeDirection() to record moves.
var originalChangeDirection = window.changeDirection
window.changeDirection = (dir) => {
moves[moves.length - 1].push(dir)
originalChangeDirection(dir)
}
// Override Math.random() so it always returns same pseudo-random values.
var LCG = seed => () => (seed = Math.imul(741103597, seed) >>> 0 ) / 2 ** 32
Math.random = LCG(0xF0000)
// Restart game to start recording.
startGame()
drawWorld()
// After recording for a while, print moves with:
// console.log(JSON.stringify(moves))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment